fix imports
[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 #ifndef SWIG
153 #ifdef PYTHON_REFCOUNT_DEBUG
154 inline void Impl_Py_DECREF(const char* file, int line, const ePyObject &obj)
155 {
156         ((ePyObject*)(&obj))->decref(file, line);
157 }
158
159 inline void Impl_Py_INCREF(const char* file, int line, const ePyObject &obj)
160 {
161         ((ePyObject*)(&obj))->incref(file, line);
162 }
163
164 inline void Impl_Py_XDECREF(const char* file, int line, const ePyObject &obj)
165 {
166         if (obj)
167                 ((ePyObject*)(&obj))->decref(file, line);
168 }
169
170 inline void Impl_Py_XINCREF(const char* file, int line, const ePyObject &obj)
171 {
172         if (obj)
173                 ((ePyObject*)(&obj))->incref(file, line);
174 }
175
176 inline ePyObject Impl_PyTuple_New(const char* file, int line, int elements=0)
177 {
178         return ePyObject(PyTuple_New(elements), file, line);
179 }
180
181 inline ePyObject Impl_PyList_New(const char* file, int line, int elements=0)
182 {
183         return ePyObject(PyList_New(elements), file, line);
184 }
185
186 inline ePyObject Impl_PyDict_New(const char* file, int line)
187 {
188         return ePyObject(PyDict_New(), file, line);
189 }
190
191 inline ePyObject Impl_PyString_FromString(const char* file, int line, const char *str)
192 {
193         return ePyObject(PyString_FromString(str), file, line);
194 }
195
196 inline ePyObject Impl_PyString_FromFormat(const char* file, int line, const char *fmt, ...)
197 {
198         va_list ap;
199         va_start(ap, fmt);
200         PyObject *ob = PyString_FromFormatV(fmt, ap);
201         va_end(ap);
202         return ePyObject(ob, file, line);
203 }
204
205 inline ePyObject Impl_PyInt_FromLong(const char* file, int line, long val)
206 {
207         return ePyObject(PyInt_FromLong(val), file, line);
208 }
209
210 inline ePyObject Impl_PyLong_FromLong(const char* file, int line, long val)
211 {
212         return ePyObject(PyLong_FromLong(val), file, line);
213 }
214
215 inline ePyObject Impl_PyLong_FromUnsignedLong(const char* file, int line, unsigned long val)
216 {
217         return ePyObject(PyLong_FromUnsignedLong(val), file, line);
218 }
219
220 inline ePyObject Impl_PyLong_FromLongLong(const char* file, int line, long long val)
221 {
222         return ePyObject(PyLong_FromLongLong(val), file, line);
223 }
224
225 inline ePyObject Impl_PyList_GET_ITEM(const char *file, int line, ePyObject list, unsigned int pos)
226 {
227         return ePyObject(PyList_GET_ITEM(list, pos), file, line);
228 }
229
230 inline ePyObject Impl_PyTuple_GET_ITEM(const char *file, int line, ePyObject list, unsigned int pos)
231 {
232         return ePyObject(PyTuple_GET_ITEM(list, pos), file, line);
233 }
234 #else
235 inline void Impl_Py_DECREF(const ePyObject &obj)
236 {
237         ((ePyObject*)(&obj))->decref();
238 }
239
240 inline void Impl_Py_INCREF(const ePyObject &obj)
241 {
242         ((ePyObject*)(&obj))->incref();
243 }
244
245 inline void Impl_Py_XDECREF(const ePyObject &obj)
246 {
247         if (obj)
248                 ((ePyObject*)(&obj))->decref();
249 }
250
251 inline void Impl_Py_XINCREF(const ePyObject &obj)
252 {
253         if (obj)
254                 ((ePyObject*)(&obj))->incref();
255 }
256
257 inline ePyObject Impl_PyTuple_New(int elements=0)
258 {
259         return PyTuple_New(elements);
260 }
261
262 inline ePyObject Impl_PyList_New(int elements=0)
263 {
264         return PyList_New(elements);
265 }
266
267 inline ePyObject Impl_PyDict_New()
268 {
269         return PyDict_New();
270 }
271
272 inline ePyObject Impl_PyString_FromString(const char *str)
273 {
274         return PyString_FromString(str);
275 }
276
277 inline ePyObject Impl_PyString_FromFormat(const char *fmt, ...)
278 {
279         va_list ap;
280         va_start(ap, fmt);
281         PyObject *ob = PyString_FromFormatV(fmt, ap);
282         va_end(ap);
283         return ePyObject(ob);
284 }
285
286 inline ePyObject Impl_PyInt_FromLong(long val)
287 {
288         return PyInt_FromLong(val);
289 }
290
291 inline ePyObject Impl_PyLong_FromLong(long val)
292 {
293         return PyLong_FromLong(val);
294 }
295
296 inline ePyObject Impl_PyLong_FromUnsignedLong(unsigned long val)
297 {
298         return PyLong_FromUnsignedLong(val);
299 }
300
301 inline ePyObject Impl_PyLong_FromLongLong(long long val)
302 {
303         return PyLong_FromLongLong(val);
304 }
305
306 inline ePyObject Impl_PyList_GET_ITEM(ePyObject list, unsigned int pos)
307 {
308         return PyList_GET_ITEM(list, pos);
309 }
310
311 inline ePyObject Impl_PyTuple_GET_ITEM(ePyObject list, unsigned int pos)
312 {
313         return PyTuple_GET_ITEM(list, pos);
314 }
315 #endif
316
317 inline void Impl_DECREF(PyObject *ob)
318 {
319         Py_DECREF(ob);
320 }
321 #define Org_Py_DECREF(obj) Impl_DECREF(obj)
322 #undef Py_DECREF
323 #undef Py_XDECREF
324 #undef Py_INCREF
325 #undef Py_XINCREF
326 #undef PyList_GET_ITEM
327 #undef PyTuple_GET_ITEM
328 #ifdef PYTHON_REFCOUNT_DEBUG
329 #define Py_DECREF(obj) Impl_Py_DECREF(__FILE__, __LINE__, obj)
330 #define Py_XDECREF(obj) Impl_Py_XDECREF(__FILE__, __LINE__, obj)
331 #define Py_INCREF(obj) Impl_Py_INCREF(__FILE__, __LINE__, obj)
332 #define Py_XINCREF(obj) Impl_Py_XINCREF(__FILE__, __LINE__, obj)
333 #define PyList_New(args...) Impl_PyList_New(__FILE__, __LINE__, args)
334 #define PyTuple_New(args...) Impl_PyTuple_New(__FILE__, __LINE__, args)
335 #define PyDict_New(...) Impl_PyDict_New(__FILE__, __LINE__)
336 #define PyString_FromString(str) Impl_PyString_FromString(__FILE__, __LINE__, str)
337 #define PyString_FromFormat(str, args...) Impl_PyString_FromFormat(__FILE__, __LINE__, str, args)
338 #define PyInt_FromLong(val) Impl_PyInt_FromLong(__FILE__, __LINE__, val)
339 #define PyLong_FromLong(val) Impl_PyLong_FromLong(__FILE__, __LINE__, val)
340 #define PyLong_FromUnsignedLong(val) Impl_PyLong_FromUnsignedLong(__FILE__, __LINE__, val)
341 #define PyLong_FromLongLong(val) Impl_PyLong_FromLongLong(__FILE__, __LINE__, val)
342 #define PyList_GET_ITEM(list, pos) Impl_PyList_GET_ITEM(__FILE__, __LINE__, list, pos)
343 #define PyTuple_GET_ITEM(list, pos) Impl_PyTuple_GET_ITEM(__FILE__, __LINE__, list, pos)
344 #else
345 #define Py_DECREF(obj) Impl_Py_DECREF(obj)
346 #define Py_XDECREF(obj) Impl_Py_XDECREF(obj)
347 #define Py_INCREF(obj) Impl_Py_INCREF(obj)
348 #define Py_XINCREF(obj) Impl_Py_XINCREF(obj)
349 #define PyList_New(args...) Impl_PyList_New(args)
350 #define PyTuple_New(args...) Impl_PyTuple_New(args)
351 #define PyDict_New(...) Impl_PyDict_New()
352 #define PyString_FromString(str) Impl_PyString_FromString(str)
353 #define PyString_FromFormat(str, args...) Impl_PyString_FromFormat(str, args)
354 #define PyInt_FromLong(val) Impl_PyInt_FromLong(val)
355 #define PyLong_FromLong(val) Impl_PyLong_FromLong(val)
356 #define PyLong_FromUnsignedLong(val) Impl_PyLong_FromUnsignedLong(val)
357 #define PyLong_FromLongLong(val) Impl_PyLong_FromLongLong(val)
358 #define PyList_GET_ITEM(list, pos) Impl_PyList_GET_ITEM(list, pos)
359 #define PyTuple_GET_ITEM(list, pos) Impl_PyTuple_GET_ITEM(list, pos)
360 #endif
361
362 class ePython
363 {
364 public:
365         ePython();
366         ~ePython();
367         int execute(const std::string &pythonfile, const std::string &funcname);
368         static int call(ePyObject pFunc, ePyObject args);
369         static ePyObject resolve(const std::string &pythonfile, const std::string &funcname);
370 private:
371 };
372
373 #endif // SWIG
374 #endif // SKIP_PART2
375
376 #endif // __lib_python_python_class_h