just scale by default
[enigma2.git] / lib / python / python.cpp
1 #include <lib/base/eerror.h>
2                 /* avoid warnigs :) */
3 #undef _POSIX_C_SOURCE
4 #define _POSIX_C_SOURCE 200112L
5 extern "C" void init_enigma();
6 extern "C" void eBaseInit(void);
7 extern void bsodFatal();
8
9 #define SKIP_PART2
10 #include <lib/python/python.h>
11 #undef SKIP_PART2
12
13 #ifdef PYTHON_REFCOUNT_DEBUG
14 ePyObject &ePyObject::operator=(PyObject *ob)
15 {
16         m_ob=ob;
17         m_file=0;
18         m_line=0;
19         m_from=m_to=0;
20         m_erased=false;
21         return *this;
22 }
23
24 ePyObject &ePyObject::operator=(const ePyObject &ob)
25 {
26         m_ob=ob.m_ob;
27         m_file=ob.m_file;
28         m_line=ob.m_line;
29         m_from=ob.m_from;
30         m_to=ob.m_to;
31         m_erased=ob.m_erased;
32         return *this;
33 }
34
35 ePyObject::operator PyObject*()
36 {
37         if (m_ob)
38         {
39                 if (!m_erased && m_ob->ob_refcnt > 0)
40                         return m_ob;
41                 eDebug("invalid access PyObject %s with refcount <= 0 %d",
42                         m_erased ? "deleted" : "undeleted", m_ob->ob_refcnt);
43                 if (m_file)
44                         eDebug("last modified in file %s line %d from %d to %d",
45                                 m_file, m_line, m_from, m_to);
46                 bsodFatal();
47         }
48         return 0;
49 }
50
51 void ePyObject::incref(const char *file, int line)
52 {
53         if (!m_ob)
54         {
55                 eDebug("invalid incref python object with null pointer %s %d!!!", file, line);
56                 if (m_file)
57                         eDebug("last modified in file %s line %d from %d to %d",
58                                 m_file, m_line, m_from, m_to);
59                 bsodFatal();
60         }
61         if (m_erased || m_ob->ob_refcnt <= 0)
62         {
63                 eDebug("invalid incref %s python object with refcounting value %d in file %s line %d!!!",
64                         m_erased ? "deleted" : "undeleted", m_ob->ob_refcnt, file, line);
65                 if (m_file)
66                         eDebug("last modified in file %s line %d from %d to %d",
67                                 m_file, m_line, m_from, m_to);
68                 bsodFatal();
69         }
70         if (m_ob->ob_refcnt == 0x7FFFFFFF)
71         {
72                 eDebug("invalid incref %s python object with refcounting value %d (MAX_INT!!!) in file %s line %d!!!",
73                         m_erased ? "deleted" : "undeleted", m_ob->ob_refcnt, file, line);
74                 if (m_file)
75                         eDebug("last modified in file %s line %d from %d to %d",
76                                 m_file, m_line, m_from, m_to);
77                 bsodFatal();
78         }
79         m_file = file;
80         m_line = line;
81         m_from = m_ob->ob_refcnt;
82         m_to = m_from+1;
83         Py_INCREF(m_ob);
84 }
85
86 void ePyObject::decref(const char *file, int line)
87 {
88         if (!m_ob)
89         {
90                 eDebug("invalid decref python object with null pointer %s %d!!!", file, line);
91                 if (m_file)
92                         eDebug("last modified in file %s line %d from %d to %d",
93                                 m_file, m_line, m_from, m_to);
94                 bsodFatal();
95         }
96         if (m_erased || m_ob->ob_refcnt <= 0)
97         {
98                 eDebug("invalid decref %s python object with refcounting value %d in file %s line %d!!!",
99                         m_erased ? "deleted" : "undeleted", m_ob->ob_refcnt, file, line);
100                 if (m_file)
101                         eDebug("last modified in file %s line %d from %d to %d",
102                                 m_file, m_line, m_from, m_to);
103                 bsodFatal();
104         }
105         m_file = file;
106         m_line = line;
107         m_from = m_ob->ob_refcnt;
108         m_to = m_from-1;
109         m_erased = !m_to;
110         Py_DECREF(m_ob);
111 }
112 #endif  // PYTHON_REFCOUNT_DEBUG
113
114 #define SKIP_PART1
115 #include <lib/python/python.h>
116 #undef SKIP_PART1
117
118 ePython::ePython()
119 {
120 //      Py_VerboseFlag = 1;
121         
122 //      Py_OptimizeFlag = 1;
123         
124         Py_Initialize();
125         PyEval_InitThreads();
126
127         init_enigma();
128         eBaseInit();
129 }
130
131 ePython::~ePython()
132 {
133         Py_Finalize();
134 }
135
136 int ePython::execute(const std::string &pythonfile, const std::string &funcname)
137 {
138         ePyObject pName, pModule, pDict, pFunc, pArgs, pValue;
139         pName = PyString_FromString(pythonfile.c_str());
140
141         pModule = PyImport_Import(pName);
142         Py_DECREF(pName);
143         
144         if (pModule)
145         {
146                 pDict = PyModule_GetDict(pModule);
147                 
148                 pFunc = PyDict_GetItemString(pDict, funcname.c_str());
149                 
150                 if (pFunc && PyCallable_Check(pFunc))
151                 {
152                         pArgs = PyTuple_New(0);
153                                 // implement arguments..
154                         pValue = PyObject_CallObject(pFunc, pArgs);
155                         Py_DECREF(pArgs);
156                         if (pValue)
157                         {
158                                 printf("Result of call: %ld\n", PyInt_AsLong(pValue));
159                                 Py_DECREF(pValue);
160                         } else
161                         {
162                                 Py_DECREF(pModule);
163                                 PyErr_Print();
164                                 return 1;
165                         }
166                 }
167         } else
168         {
169                 if (PyErr_Occurred())
170                         PyErr_Print();
171                 return 1;
172         }
173         return 0;
174 }
175
176 int ePython::call(ePyObject pFunc, ePyObject pArgs)
177 {
178         int res = -1;
179         ePyObject pValue;
180         if (pFunc && PyCallable_Check(pFunc))
181         {
182                 pValue = PyObject_CallObject(pFunc, pArgs);
183                 if (pValue)
184                 {
185                         if (PyInt_Check(pValue))
186                                 res = PyInt_AsLong(pValue);
187                         else
188                                 res = 0;
189                         Py_DECREF(pValue);
190                 } else
191                 {
192                         PyErr_Print();
193                         ePyObject FuncStr = PyObject_Str(pFunc);
194                         ePyObject ArgStr = PyObject_Str(pArgs);
195                         eDebug("(PyObject_CallObject(%s,%s) failed)", PyString_AS_STRING(FuncStr), PyString_AS_STRING(ArgStr));
196                         Py_DECREF(FuncStr);
197                         Py_DECREF(ArgStr);
198                         bsodFatal();
199                 }
200         }
201         return res;
202 }
203
204 ePyObject ePython::resolve(const std::string &pythonfile, const std::string &funcname)
205 {
206         ePyObject pName, pModule, pDict, pFunc;
207
208         pName = PyString_FromString(pythonfile.c_str());
209
210         pModule = PyImport_Import(pName);
211         Py_DECREF(pName);
212         
213         if (pModule)
214         {
215                 pDict = PyModule_GetDict(pModule);
216                 pFunc = PyDict_GetItemString(pDict, funcname.c_str());
217                 Py_XINCREF(pFunc);
218                 Py_DECREF(pModule);
219         } else if (PyErr_Occurred())
220                 PyErr_Print();
221         return pFunc;
222 }