42749c7e3bd926cd671ba0a1ca354bc9c3c19c5e
[enigma2.git] / lib / python / python.h
1 #ifndef __lib_python_python_class_h
2
3 #ifndef SKIP_PART2
4         #define __lib_python_python_class_h
5 #endif
6
7 #include <string>
8 #include <lib/base/object.h>
9 #include <Python.h>
10
11 #if !defined(SKIP_PART1) && !defined(SWIG)
12 class ePyObject
13 {
14         PyObject *m_ob;
15 #ifdef PYTHON_REFCOUNT_DEBUG
16         const char *m_file;
17         int m_line, m_from, m_to;
18         bool m_erased;
19 #endif
20 public:
21         inline ePyObject();
22         inline ePyObject(const ePyObject &ob);
23         inline ePyObject(PyObject *ob);
24 #ifdef PYTHON_REFCOUNT_DEBUG
25         inline ePyObject(PyObject *ob, const char *file, int line);
26 #endif
27         inline ePyObject(PyDictObject *ob);
28         inline ePyObject(PyTupleObject *ob);
29         inline ePyObject(PyListObject *ob);
30         inline ePyObject(PyStringObject *ob);
31         operator bool() const { return !!m_ob; }
32         operator bool() { return !!m_ob; }
33         ePyObject &operator=(const ePyObject &);
34         ePyObject &operator=(PyObject *);
35         ePyObject &operator=(PyDictObject *ob) { return operator=((PyObject*)ob); }
36         ePyObject &operator=(PyTupleObject *ob) { return operator=((PyObject*)ob); }
37         ePyObject &operator=(PyListObject *ob) { return operator=((PyObject*)ob); }
38         ePyObject &operator=(PyStringObject *ob) { return operator=((PyObject*)ob); }
39         operator PyObject*();
40         operator PyTupleObject*() { return (PyTupleObject*)operator PyObject*(); }
41         operator PyListObject*() { return (PyListObject*)operator PyObject*(); }
42         operator PyStringObject*() { return (PyStringObject*)operator PyObject*(); }
43         operator PyDictObject*() { return (PyDictObject*)operator PyObject*(); }
44         PyObject *operator->() { return operator PyObject*(); }
45 #ifdef PYTHON_REFCOUNT_DEBUG
46         void incref(const char *file, int line);
47         void decref(const char *file, int line);
48 #else
49         void incref();
50         void decref();
51 #endif
52 };
53
54 inline ePyObject::ePyObject()
55         :m_ob(0)
56 #ifdef PYTHON_REFCOUNT_DEBUG
57         ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
58 #endif
59 {
60 }
61
62 inline ePyObject::ePyObject(const ePyObject &ob)
63         :m_ob(ob.m_ob)
64 #ifdef PYTHON_REFCOUNT_DEBUG
65         ,m_file(ob.m_file), m_line(ob.m_line)
66         ,m_from(ob.m_from), m_to(ob.m_to), m_erased(ob.m_erased)
67 #endif
68 {
69 }
70
71 inline ePyObject::ePyObject(PyObject *ob)
72         :m_ob(ob)
73 #ifdef PYTHON_REFCOUNT_DEBUG
74         ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
75 #endif
76 {
77 }
78
79 #ifdef PYTHON_REFCOUNT_DEBUG
80 inline ePyObject::ePyObject(PyObject *ob, const char* file, int line)
81         :m_ob(ob)
82         ,m_file(file), m_line(line), m_from(ob->ob_refcnt), m_to(ob->ob_refcnt), m_erased(false)
83 {
84 }
85 #endif
86
87 inline ePyObject::ePyObject(PyDictObject *ob)
88         :m_ob((PyObject*)ob)
89 #ifdef PYTHON_REFCOUNT_DEBUG
90         ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
91 #endif
92 {
93 }
94
95 inline ePyObject::ePyObject(PyTupleObject *ob)
96         :m_ob((PyObject*)ob)
97 #ifdef PYTHON_REFCOUNT_DEBUG
98         ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
99 #endif
100 {
101 }
102
103 inline ePyObject::ePyObject(PyListObject *ob)
104         :m_ob((PyObject*)ob)
105 #ifdef PYTHON_REFCOUNT_DEBUG
106         ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
107 #endif
108 {
109 }
110
111 inline ePyObject::ePyObject(PyStringObject *ob)
112         :m_ob((PyObject*)ob)
113 #ifdef PYTHON_REFCOUNT_DEBUG
114         ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
115 #endif
116 {
117 }
118
119 #ifndef PYTHON_REFCOUNT_DEBUG
120 inline ePyObject &ePyObject::operator=(PyObject *ob)
121 {
122         m_ob=ob;
123         return *this;
124 }
125
126 inline ePyObject &ePyObject::operator=(const ePyObject &ob)
127 {
128         m_ob=ob.m_ob;
129         return *this;
130 }
131
132 inline ePyObject::operator PyObject*()
133 {
134         return m_ob;
135 }
136
137 inline void ePyObject::incref()
138 {
139         Py_INCREF(m_ob);
140 }
141
142 inline void ePyObject::decref()
143 {
144         Py_DECREF(m_ob);
145 }
146
147 #endif // ! PYTHON_REFCOUNT_DEBUG
148
149 #endif  // !SWIG && !SKIP_PART1
150
151 #ifndef SKIP_PART2
152
153 class TestObj
154 {
155 DECLARE_REF(TestObj);
156 public:
157         TestObj();
158         ~TestObj();
159 };
160 TEMPLATE_TYPEDEF(ePtr<TestObj>, TestObjPtr);
161
162 #ifndef SWIG
163 extern PyObject *New_TestObj();
164 #ifdef PYTHON_REFCOUNT_DEBUG
165 inline void Impl_Py_DECREF(const char* file, int line, const ePyObject &obj)
166 {
167         ((ePyObject*)(&obj))->decref(file, line);
168 }
169
170 inline void Impl_Py_INCREF(const char* file, int line, const ePyObject &obj)
171 {
172         ((ePyObject*)(&obj))->incref(file, line);
173 }
174
175 inline void Impl_Py_XDECREF(const char* file, int line, const ePyObject &obj)
176 {
177         if (obj)
178                 ((ePyObject*)(&obj))->decref(file, line);
179 }
180
181 inline void Impl_Py_XINCREF(const char* file, int line, const ePyObject &obj)
182 {
183         if (obj)
184                 ((ePyObject*)(&obj))->incref(file, line);
185 }
186
187 inline ePyObject Impl_PyTuple_New(const char* file, int line, int elements=0)
188 {
189         return ePyObject(PyTuple_New(elements), file, line);
190 }
191
192 inline ePyObject Impl_PyList_New(const char* file, int line, int elements=0)
193 {
194         return ePyObject(PyList_New(elements), file, line);
195 }
196
197 inline ePyObject Impl_PyDict_New(const char* file, int line)
198 {
199         return ePyObject(PyDict_New(), file, line);
200 }
201
202 inline ePyObject Impl_PyString_FromString(const char* file, int line, const char *str)
203 {
204         return ePyObject(PyString_FromString(str), file, line);
205 }
206
207 inline ePyObject Impl_PyInt_FromLong(const char* file, int line, long val)
208 {
209         return ePyObject(PyInt_FromLong(val), file, line);
210 }
211
212 inline ePyObject Impl_PyLong_FromLong(const char* file, int line, long val)
213 {
214         return ePyObject(PyLong_FromLong(val), file, line);
215 }
216
217 inline ePyObject Impl_PyLong_FromUnsignedLong(const char* file, int line, unsigned long val)
218 {
219         return ePyObject(PyLong_FromUnsignedLong(val), file, line);
220 }
221
222 inline ePyObject Impl_PyLong_FromLongLong(const char* file, int line, long long val)
223 {
224         return ePyObject(PyLong_FromLongLong(val), file, line);
225 }
226
227 inline ePyObject Impl_New_TestObj(const char* file, int line)
228 {
229         return ePyObject(New_TestObj(), file, line);
230 }
231
232 inline ePyObject Impl_PyList_GET_ITEM(const char *file, int line, ePyObject list, unsigned int pos)
233 {
234         return ePyObject(PyList_GET_ITEM(list, pos), file, line);
235 }
236
237 inline ePyObject Impl_PyTuple_GET_ITEM(const char *file, int line, ePyObject list, unsigned int pos)
238 {
239         return ePyObject(PyTuple_GET_ITEM(list, pos), file, line);
240 }
241 #else
242 inline void Impl_Py_DECREF(const ePyObject &obj)
243 {
244         ((ePyObject*)(&obj))->decref();
245 }
246
247 inline void Impl_Py_INCREF(const ePyObject &obj)
248 {
249         ((ePyObject*)(&obj))->incref();
250 }
251
252 inline void Impl_Py_XDECREF(const ePyObject &obj)
253 {
254         if (obj)
255                 ((ePyObject*)(&obj))->decref();
256 }
257
258 inline void Impl_Py_XINCREF(const ePyObject &obj)
259 {
260         if (obj)
261                 ((ePyObject*)(&obj))->incref();
262 }
263
264 inline ePyObject Impl_PyTuple_New(int elements=0)
265 {
266         return PyTuple_New(elements);
267 }
268
269 inline ePyObject Impl_PyList_New(int elements=0)
270 {
271         return PyList_New(elements);
272 }
273
274 inline ePyObject Impl_PyDict_New()
275 {
276         return PyDict_New();
277 }
278
279 inline ePyObject Impl_PyString_FromString(const char *str)
280 {
281         return PyString_FromString(str);
282 }
283
284 inline ePyObject Impl_PyInt_FromLong(long val)
285 {
286         return PyInt_FromLong(val);
287 }
288
289 inline ePyObject Impl_PyLong_FromLong(long val)
290 {
291         return PyLong_FromLong(val);
292 }
293
294 inline ePyObject Impl_PyLong_FromUnsignedLong(unsigned long val)
295 {
296         return PyLong_FromUnsignedLong(val);
297 }
298
299 inline ePyObject Impl_PyLong_FromLongLong(long long val)
300 {
301         return PyLong_FromLongLong(val);
302 }
303
304 inline ePyObject Impl_New_TestObj()
305 {
306         return New_TestObj();
307 }
308
309 inline ePyObject Impl_PyList_GET_ITEM(ePyObject list, unsigned int pos)
310 {
311         return PyList_GET_ITEM(list, pos);
312 }
313
314 inline ePyObject Impl_PyTuple_GET_ITEM(ePyObject list, unsigned int pos)
315 {
316         return PyTuple_GET_ITEM(list, pos);
317 }
318 #endif
319
320 inline void Impl_DECREF(PyObject *ob)
321 {
322         Py_DECREF(ob);
323 }
324 #define Org_Py_DECREF(obj) Impl_DECREF(obj)
325 #undef Py_DECREF
326 #undef Py_XDECREF
327 #undef Py_INCREF
328 #undef Py_XINCREF
329 #undef PyList_GET_ITEM
330 #undef PyTuple_GET_ITEM
331 #ifdef PYTHON_REFCOUNT_DEBUG
332 #define Py_DECREF(obj) Impl_Py_DECREF(__FILE__, __LINE__, obj)
333 #define Py_XDECREF(obj) Impl_Py_XDECREF(__FILE__, __LINE__, obj)
334 #define Py_INCREF(obj) Impl_Py_INCREF(__FILE__, __LINE__, obj)
335 #define Py_XINCREF(obj) Impl_Py_XINCREF(__FILE__, __LINE__, obj)
336 #define PyList_New(args...) Impl_PyList_New(__FILE__, __LINE__, args)
337 #define PyTuple_New(args...) Impl_PyTuple_New(__FILE__, __LINE__, args)
338 #define PyDict_New(...) Impl_PyDict_New(__FILE__, __LINE__)
339 #define PyString_FromString(str) Impl_PyString_FromString(__FILE__, __LINE__, str)
340 #define PyInt_FromLong(val) Impl_PyInt_FromLong(__FILE__, __LINE__, val)
341 #define PyLong_FromLong(val) Impl_PyLong_FromLong(__FILE__, __LINE__, val)
342 #define PyLong_FromUnsignedLong(val) Impl_PyLong_FromUnsignedLong(__FILE__, __LINE__, val)
343 #define PyLong_FromLongLong(val) Impl_PyLong_FromLongLong(__FILE__, __LINE__, val)
344 #define NEW_TestObj(...) Impl_New_TestObj(__FILE__, __LINE__)
345 #define PyList_GET_ITEM(list, pos) Impl_PyList_GET_ITEM(__FILE__, __LINE__, list, pos)
346 #define PyTuple_GET_ITEM(list, pos) Impl_PyTuple_GET_ITEM(__FILE__, __LINE__, list, pos)
347 #else
348 #define Py_DECREF(obj) Impl_Py_DECREF(obj)
349 #define Py_XDECREF(obj) Impl_Py_XDECREF(obj)
350 #define Py_INCREF(obj) Impl_Py_INCREF(obj)
351 #define Py_XINCREF(obj) Impl_Py_XINCREF(obj)
352 #define PyList_New(args...) Impl_PyList_New(args)
353 #define PyTuple_New(args...) Impl_PyTuple_New(args)
354 #define PyDict_New(...) Impl_PyDict_New()
355 #define PyString_FromString(str) Impl_PyString_FromString(str)
356 #define PyInt_FromLong(val) Impl_PyInt_FromLong(val)
357 #define PyLong_FromLong(val) Impl_PyLong_FromLong(val)
358 #define PyLong_FromUnsignedLong(val) Impl_PyLong_FromUnsignedLong(val)
359 #define PyLong_FromLongLong(val) Impl_PyLong_FromLongLong(val)
360 #define NEW_TestObj(...) Impl_New_TestObj()
361 #define PyList_GET_ITEM(list, pos) Impl_PyList_GET_ITEM(list, pos)
362 #define PyTuple_GET_ITEM(list, pos) Impl_PyTuple_GET_ITEM(list, pos)
363 #endif
364
365 class ePython
366 {
367 public:
368         ePython();
369         ~ePython();
370         int execute(const std::string &pythonfile, const std::string &funcname);
371         static int call(ePyObject pFunc, ePyObject args);
372         static ePyObject resolve(const std::string &pythonfile, const std::string &funcname);
373 private:
374 };
375
376 #endif // SWIG
377 #endif // SKIP_PART2
378
379 #endif // __lib_python_python_class_h