8947bb3ca12826f63e0a4d811809c560ca705c63
[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 DEFINE_REF(TestObj);
118
119 TestObj::TestObj()
120 {
121         eDebug("create %p", this);
122 }
123
124 TestObj::~TestObj()
125 {
126         eDebug("destroy %p", this);
127 }
128
129 ePython::ePython()
130 {
131 //      Py_VerboseFlag = 1;
132         
133 //      Py_OptimizeFlag = 1;
134         
135         Py_Initialize();
136         
137         init_enigma();
138 }
139
140 ePython::~ePython()
141 {
142         Py_Finalize();
143 }
144
145 int ePython::execute(const std::string &pythonfile, const std::string &funcname)
146 {
147         ePyObject pName, pModule, pDict, pFunc, pArgs, pValue;
148         pName = PyString_FromString(pythonfile.c_str());
149
150         pModule = PyImport_Import(pName);
151         Py_DECREF(pName);
152         
153         if (pModule)
154         {
155                 pDict = PyModule_GetDict(pModule);
156                 
157                 pFunc = PyDict_GetItemString(pDict, funcname.c_str());
158                 
159                 if (pFunc && PyCallable_Check(pFunc))
160                 {
161                         pArgs = PyTuple_New(0);
162                                 // implement arguments..
163                         pValue = PyObject_CallObject(pFunc, pArgs);
164                         Py_DECREF(pArgs);
165                         if (pValue)
166                         {
167                                 printf("Result of call: %ld\n", PyInt_AsLong(pValue));
168                                 Py_DECREF(pValue);
169                         } else
170                         {
171                                 Py_DECREF(pModule);
172                                 PyErr_Print();
173                                 return 1;
174                         }
175                 }
176         } else
177         {
178                 if (PyErr_Occurred())
179                         PyErr_Print();
180                 return 1;
181         }
182         return 0;
183 }
184
185 int ePython::call(ePyObject pFunc, ePyObject pArgs)
186 {
187         int res = -1;
188         ePyObject pValue;
189         if (pFunc && PyCallable_Check(pFunc))
190         {
191                 pValue = PyObject_CallObject(pFunc, pArgs);
192                 if (pValue)
193                 {
194                         if (PyInt_Check(pValue))
195                                 res = PyInt_AsLong(pValue);
196                         else
197                                 res = 0;
198                         Py_DECREF(pValue);
199                 } else
200                 {
201                         PyErr_Print();
202                         bsodFatal();
203                 }
204         }
205         return res;
206 }
207
208 ePyObject ePython::resolve(const std::string &pythonfile, const std::string &funcname)
209 {
210         ePyObject pName, pModule, pDict, pFunc;
211
212         pName = PyString_FromString(pythonfile.c_str());
213
214         pModule = PyImport_Import(pName);
215         Py_DECREF(pName);
216         
217         if (pModule)
218         {
219                 pDict = PyModule_GetDict(pModule);
220                 pFunc = PyDict_GetItemString(pDict, funcname.c_str());
221                 Py_XINCREF(pFunc);
222                 Py_DECREF(pModule);
223         } else if (PyErr_Occurred())
224                 PyErr_Print();
225         return pFunc;
226 }