increase slider range
[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_OptimizeFlag = 1;
66         
67         Py_Initialize();
68         
69         init_enigma();
70 }
71
72 ePython::~ePython()
73 {
74         Py_Finalize();
75 }
76
77 int ePython::execute(const std::string &pythonfile, const std::string &funcname)
78 {
79         PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;
80         pName = PyString_FromString(pythonfile.c_str());
81
82         pModule = PyImport_Import(pName);
83         Py_DECREF(pName);
84         
85         if (pModule != NULL)
86         {
87                 pDict = PyModule_GetDict(pModule);
88                 
89                 pFunc = PyDict_GetItemString(pDict, funcname.c_str());
90                 
91                 if (pFunc && PyCallable_Check(pFunc))
92                 {
93                         pArgs = PyTuple_New(0);
94                                 // implement arguments..
95                         pValue = PyObject_CallObject(pFunc, pArgs);
96                         Py_DECREF(pArgs);
97                         if (pValue != NULL)
98                         {
99                                 printf("Result of call: %ld\n", PyInt_AsLong(pValue));
100                                 Py_DECREF(pValue);
101                         } else
102                         {
103                                 Py_DECREF(pModule);
104                                 PyErr_Print();
105                                 return 1;
106                         }
107                 }
108         } else
109         {
110                 if (PyErr_Occurred())
111                         PyErr_Print();
112                 return 1;
113         }
114         return 0;
115 }
116
117 int ePython::call(PyObject *pFunc, PyObject *pArgs)
118 {
119         int res = -1;
120         PyObject *pValue;
121         if (pFunc && PyCallable_Check(pFunc))
122         {
123                 pValue = PyObject_CallObject(pFunc, pArgs);
124                 if (pValue != NULL)
125                 {
126                         if (PyInt_Check(pValue))
127                                 res = PyInt_AsLong(pValue);
128                         else
129                                 res = 0;
130                         Py_DECREF(pValue);
131                 } else
132                 {
133                         PyErr_Print();
134                         bsodFatal();
135                 }
136         }
137         return res;
138 }
139
140 PyObject *ePython::resolve(const std::string &pythonfile, const std::string &funcname)
141 {
142         PyObject *pName, *pModule, *pDict, *pFunc;
143
144         pName = PyString_FromString(pythonfile.c_str());
145
146         pModule = PyImport_Import(pName);
147         Py_DECREF(pName);
148         
149         if (pModule != NULL)
150         {
151                 pDict = PyModule_GetDict(pModule);
152                 pFunc = PyDict_GetItemString(pDict, funcname.c_str());
153                 Py_XINCREF(pFunc);
154                 Py_DECREF(pModule);
155                 return pFunc;
156         } else
157         {
158                 if (PyErr_Occurred())
159                         PyErr_Print();
160                 return 0;
161         }
162 }