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