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