follow changes
[enigma2.git] / lib / python / python.cpp
1 #include <lib/python/python.h>
2 #include <lib/base/eerror.h>
3                 /* avoid warnigs :) */
4 #undef _POSIX_C_SOURCE
5 #define _POSIX_C_SOURCE 200112L
6 #include <Python.h>
7
8 extern "C" void init_enigma();
9 extern void bsodFatal();
10
11 DEFINE_REF(TestObj);
12
13 TestObj::TestObj()
14 {
15         eDebug("create %p", this);
16 }
17
18 TestObj::~TestObj()
19 {
20         eDebug("destroy %p", this);
21 }
22
23 #if 0
24 ePyObject::ePyObject(void *ptr): m_object(ptr)
25 {
26         Py_XINCREF((PyObject*)ptr);
27 }
28
29 ePyObject::ePyObject(ePyObject &p)
30 {
31         m_object = p.m_object;
32         Py_XINCREF((PyObject*)m_object);
33 }
34
35 ePyObject::ePyObject(): m_object(0)
36 {
37 }
38
39 ePyObject::~ePyObject()
40 {
41         Py_XDECREF((PyObject*)m_object);
42 }
43
44 ePyObject &ePyObject::operator=(ePyObject &p)
45 {
46         Py_XDECREF((PyObject*)m_object);
47         m_object = p.m_object;
48         Py_XINCREF((PyObject*)m_object);
49         return *this;
50 }
51
52 ePyObject &ePyObject::operator=(void *object)
53 {
54         Py_XDECREF((PyObject*)m_object);
55         m_object = object;
56         Py_XINCREF((PyObject*)m_object);
57         return *this;
58 }
59 #endif
60
61 ePython::ePython()
62 {
63 //      Py_VerboseFlag = 1;
64         
65         Py_Initialize();
66         
67         init_enigma();
68 }
69
70 ePython::~ePython()
71 {
72         Py_Finalize();
73 }
74
75 int ePython::execute(const std::string &pythonfile, const std::string &funcname)
76 {
77         PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;
78         
79         pName = PyString_FromString(pythonfile.c_str());
80         
81         pModule = PyImport_Import(pName);
82         Py_DECREF(pName);
83         
84         if (pModule != NULL)
85         {
86                 pDict = PyModule_GetDict(pModule);
87                 
88                 pFunc = PyDict_GetItemString(pDict, funcname.c_str());
89                 
90                 if (pFunc && PyCallable_Check(pFunc))
91                 {
92                         pArgs = PyTuple_New(0);
93                                 // implement arguments..
94                         pValue = PyObject_CallObject(pFunc, pArgs);
95                         Py_DECREF(pArgs);
96                         if (pValue != NULL)
97                         {
98                                 printf("Result of call: %ld\n", PyInt_AsLong(pValue));
99                                 Py_DECREF(pValue);
100                         } else
101                         {
102                                 Py_DECREF(pModule);
103                                 PyErr_Print();
104                                 return 1;
105                         }
106                 }
107         } else
108         {
109                 if (PyErr_Occurred())
110                         PyErr_Print();
111                 return 1;
112         }
113         return 0;
114 }
115
116 int ePython::call(PyObject *pFunc, PyObject *pArgs)
117 {
118         int res = -1;
119         PyObject *pValue;
120         if (pFunc && PyCallable_Check(pFunc))
121         {
122                 pValue = PyObject_CallObject(pFunc, pArgs);
123                 if (pValue != NULL)
124                 {
125                         if (PyInt_Check(pValue))
126                                 res = PyInt_AsLong(pValue);
127                         else
128                                 res = 0;
129                         Py_DECREF(pValue);
130                 } else
131                 {
132                         PyErr_Print();
133                         bsodFatal();
134                 }
135         }
136         return res;
137 }
138
139 PyObject *ePython::resolve(const std::string &pythonfile, const std::string &funcname)
140 {
141         PyObject *pName, *pModule, *pDict, *pFunc;
142
143         pName = PyString_FromString(pythonfile.c_str());
144
145         pModule = PyImport_Import(pName);
146         Py_DECREF(pName);
147         
148         if (pModule != NULL)
149         {
150                 pDict = PyModule_GetDict(pModule);
151                 pFunc = PyDict_GetItemString(pDict, funcname.c_str());
152                 Py_XINCREF(pFunc);
153                 Py_DECREF(pModule);
154                 return pFunc;
155         } else
156         {
157                 if (PyErr_Occurred())
158                         PyErr_Print();
159                 return 0;
160         }
161 }