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