1 #ifndef __lib_python_python_class_h
4 #define __lib_python_python_class_h
9 #include <lib/base/object.h>
11 #if !defined(SKIP_PART1) && !defined(SWIG)
15 #ifdef PYTHON_REFCOUNT_DEBUG
17 int m_line, m_from, m_to;
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);
27 inline ePyObject(PyVarObject *ob);
28 inline ePyObject(PyDictObject *ob);
29 inline ePyObject(PyTupleObject *ob);
30 inline ePyObject(PyListObject *ob);
31 inline ePyObject(PyStringObject *ob);
32 operator bool() const { return !!m_ob; }
33 operator bool() { return !!m_ob; }
34 ePyObject &operator=(const ePyObject &);
35 ePyObject &operator=(PyObject *);
36 ePyObject &operator=(PyVarObject *ob) { return operator=((PyObject*)ob); }
37 ePyObject &operator=(PyDictObject *ob) { return operator=((PyObject*)ob); }
38 ePyObject &operator=(PyTupleObject *ob) { return operator=((PyObject*)ob); }
39 ePyObject &operator=(PyListObject *ob) { return operator=((PyObject*)ob); }
40 ePyObject &operator=(PyStringObject *ob) { return operator=((PyObject*)ob); }
42 operator PyVarObject*() { return (PyVarObject*)operator PyVarObject*(); }
43 operator PyTupleObject*() { return (PyTupleObject*)operator PyObject*(); }
44 operator PyListObject*() { return (PyListObject*)operator PyObject*(); }
45 operator PyStringObject*() { return (PyStringObject*)operator PyObject*(); }
46 operator PyDictObject*() { return (PyDictObject*)operator PyObject*(); }
47 PyObject *operator->() { return operator PyObject*(); }
48 #ifdef PYTHON_REFCOUNT_DEBUG
49 void incref(const char *file, int line);
50 void decref(const char *file, int line);
57 inline ePyObject::ePyObject()
59 #ifdef PYTHON_REFCOUNT_DEBUG
60 ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
65 inline ePyObject::ePyObject(const ePyObject &ob)
67 #ifdef PYTHON_REFCOUNT_DEBUG
68 ,m_file(ob.m_file), m_line(ob.m_line)
69 ,m_from(ob.m_from), m_to(ob.m_to), m_erased(ob.m_erased)
74 inline ePyObject::ePyObject(PyObject *ob)
76 #ifdef PYTHON_REFCOUNT_DEBUG
77 ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
82 #ifdef PYTHON_REFCOUNT_DEBUG
83 inline ePyObject::ePyObject(PyObject *ob, const char* file, int line)
85 ,m_file(file), m_line(line), m_from(ob->ob_refcnt), m_to(ob->ob_refcnt), m_erased(false)
90 inline ePyObject::ePyObject(PyVarObject *ob)
92 #ifdef PYTHON_REFCOUNT_DEBUG
93 ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
98 inline ePyObject::ePyObject(PyDictObject *ob)
100 #ifdef PYTHON_REFCOUNT_DEBUG
101 ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
106 inline ePyObject::ePyObject(PyTupleObject *ob)
108 #ifdef PYTHON_REFCOUNT_DEBUG
109 ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
114 inline ePyObject::ePyObject(PyListObject *ob)
116 #ifdef PYTHON_REFCOUNT_DEBUG
117 ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
122 inline ePyObject::ePyObject(PyStringObject *ob)
124 #ifdef PYTHON_REFCOUNT_DEBUG
125 ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
130 #ifndef PYTHON_REFCOUNT_DEBUG
131 inline ePyObject &ePyObject::operator=(PyObject *ob)
137 inline ePyObject &ePyObject::operator=(const ePyObject &ob)
143 inline ePyObject::operator PyObject*()
148 inline void ePyObject::incref()
153 inline void ePyObject::decref()
158 class ePyObjectWrapper
162 ePyObjectWrapper(const ePyObjectWrapper &wrapper)
163 :m_obj(wrapper.m_obj)
167 ePyObjectWrapper(const ePyObject &obj)
176 ePyObjectWrapper &operator=(const ePyObjectWrapper &wrapper)
179 m_obj = wrapper.m_obj;
193 #endif // ! PYTHON_REFCOUNT_DEBUG
195 #endif // !SWIG && !SKIP_PART1
199 #ifdef PYTHON_REFCOUNT_DEBUG
200 inline void Impl_Py_DECREF(const char* file, int line, const ePyObject &obj)
202 ((ePyObject*)(&obj))->decref(file, line);
205 inline void Impl_Py_INCREF(const char* file, int line, const ePyObject &obj)
207 ((ePyObject*)(&obj))->incref(file, line);
210 inline void Impl_Py_XDECREF(const char* file, int line, const ePyObject &obj)
213 ((ePyObject*)(&obj))->decref(file, line);
216 inline void Impl_Py_XINCREF(const char* file, int line, const ePyObject &obj)
219 ((ePyObject*)(&obj))->incref(file, line);
222 inline ePyObject Impl_PyTuple_New(const char* file, int line, int elements=0)
224 return ePyObject(PyTuple_New(elements), file, line);
227 inline ePyObject Impl_PyList_New(const char* file, int line, int elements=0)
229 return ePyObject(PyList_New(elements), file, line);
232 inline ePyObject Impl_PyDict_New(const char* file, int line)
234 return ePyObject(PyDict_New(), file, line);
237 inline ePyObject Impl_PyString_FromString(const char* file, int line, const char *str)
239 return ePyObject(PyString_FromString(str), file, line);
242 inline ePyObject Impl_PyString_FromFormat(const char* file, int line, const char *fmt, ...)
246 PyObject *ob = PyString_FromFormatV(fmt, ap);
248 return ePyObject(ob, file, line);
251 inline ePyObject Impl_PyInt_FromLong(const char* file, int line, long val)
253 return ePyObject(PyInt_FromLong(val), file, line);
256 inline ePyObject Impl_PyLong_FromLong(const char* file, int line, long val)
258 return ePyObject(PyLong_FromLong(val), file, line);
261 inline ePyObject Impl_PyLong_FromUnsignedLong(const char* file, int line, unsigned long val)
263 return ePyObject(PyLong_FromUnsignedLong(val), file, line);
266 inline ePyObject Impl_PyLong_FromLongLong(const char* file, int line, long long val)
268 return ePyObject(PyLong_FromLongLong(val), file, line);
271 inline ePyObject Impl_PyList_GET_ITEM(const char *file, int line, ePyObject list, unsigned int pos)
273 return ePyObject(PyList_GET_ITEM(list, pos), file, line);
276 inline ePyObject Impl_PyTuple_GET_ITEM(const char *file, int line, ePyObject list, unsigned int pos)
278 return ePyObject(PyTuple_GET_ITEM(list, pos), file, line);
281 inline void Impl_Py_DECREF(const ePyObject &obj)
283 ((ePyObject*)(&obj))->decref();
286 inline void Impl_Py_INCREF(const ePyObject &obj)
288 ((ePyObject*)(&obj))->incref();
291 inline void Impl_Py_XDECREF(const ePyObject &obj)
294 ((ePyObject*)(&obj))->decref();
297 inline void Impl_Py_XINCREF(const ePyObject &obj)
300 ((ePyObject*)(&obj))->incref();
303 inline ePyObject Impl_PyTuple_New(int elements=0)
305 return PyTuple_New(elements);
308 inline ePyObject Impl_PyList_New(int elements=0)
310 return PyList_New(elements);
313 inline ePyObject Impl_PyDict_New()
318 inline ePyObject Impl_PyString_FromString(const char *str)
320 return PyString_FromString(str);
323 inline ePyObject Impl_PyString_FromFormat(const char *fmt, ...)
327 PyObject *ob = PyString_FromFormatV(fmt, ap);
329 return ePyObject(ob);
332 inline ePyObject Impl_PyInt_FromLong(long val)
334 return PyInt_FromLong(val);
337 inline ePyObject Impl_PyLong_FromLong(long val)
339 return PyLong_FromLong(val);
342 inline ePyObject Impl_PyLong_FromUnsignedLong(unsigned long val)
344 return PyLong_FromUnsignedLong(val);
347 inline ePyObject Impl_PyLong_FromLongLong(long long val)
349 return PyLong_FromLongLong(val);
352 inline ePyObject Impl_PyList_GET_ITEM(ePyObject list, unsigned int pos)
354 return PyList_GET_ITEM(list, pos);
357 inline ePyObject Impl_PyTuple_GET_ITEM(ePyObject list, unsigned int pos)
359 return PyTuple_GET_ITEM(list, pos);
363 inline void Impl_INCREF(PyObject *ob)
368 inline void Impl_DECREF(PyObject *ob)
372 #define Org_Py_INCREF(obj) Impl_INCREF(obj)
373 #define Org_Py_DECREF(obj) Impl_DECREF(obj)
378 #undef PyList_GET_ITEM
379 #undef PyTuple_GET_ITEM
380 #ifdef PYTHON_REFCOUNT_DEBUG
381 #define Py_DECREF(obj) Impl_Py_DECREF(__FILE__, __LINE__, obj)
382 #define Py_XDECREF(obj) Impl_Py_XDECREF(__FILE__, __LINE__, obj)
383 #define Py_INCREF(obj) Impl_Py_INCREF(__FILE__, __LINE__, obj)
384 #define Py_XINCREF(obj) Impl_Py_XINCREF(__FILE__, __LINE__, obj)
385 #define PyList_New(args...) Impl_PyList_New(__FILE__, __LINE__, args)
386 #define PyTuple_New(args...) Impl_PyTuple_New(__FILE__, __LINE__, args)
387 #define PyDict_New(...) Impl_PyDict_New(__FILE__, __LINE__)
388 #define PyString_FromString(str) Impl_PyString_FromString(__FILE__, __LINE__, str)
389 #define PyString_FromFormat(str, args...) Impl_PyString_FromFormat(__FILE__, __LINE__, str, args)
390 #define PyInt_FromLong(val) Impl_PyInt_FromLong(__FILE__, __LINE__, val)
391 #define PyLong_FromLong(val) Impl_PyLong_FromLong(__FILE__, __LINE__, val)
392 #define PyLong_FromUnsignedLong(val) Impl_PyLong_FromUnsignedLong(__FILE__, __LINE__, val)
393 #define PyLong_FromLongLong(val) Impl_PyLong_FromLongLong(__FILE__, __LINE__, val)
394 #define PyList_GET_ITEM(list, pos) Impl_PyList_GET_ITEM(__FILE__, __LINE__, list, pos)
395 #define PyTuple_GET_ITEM(list, pos) Impl_PyTuple_GET_ITEM(__FILE__, __LINE__, list, pos)
397 #define Py_DECREF(obj) Impl_Py_DECREF(obj)
398 #define Py_XDECREF(obj) Impl_Py_XDECREF(obj)
399 #define Py_INCREF(obj) Impl_Py_INCREF(obj)
400 #define Py_XINCREF(obj) Impl_Py_XINCREF(obj)
401 #define PyList_New(args...) Impl_PyList_New(args)
402 #define PyTuple_New(args...) Impl_PyTuple_New(args)
403 #define PyDict_New(...) Impl_PyDict_New()
404 #define PyString_FromString(str) Impl_PyString_FromString(str)
405 #define PyString_FromFormat(str, args...) Impl_PyString_FromFormat(str, args)
406 #define PyInt_FromLong(val) Impl_PyInt_FromLong(val)
407 #define PyLong_FromLong(val) Impl_PyLong_FromLong(val)
408 #define PyLong_FromUnsignedLong(val) Impl_PyLong_FromUnsignedLong(val)
409 #define PyLong_FromLongLong(val) Impl_PyLong_FromLongLong(val)
410 #define PyList_GET_ITEM(list, pos) Impl_PyList_GET_ITEM(list, pos)
411 #define PyTuple_GET_ITEM(list, pos) Impl_PyTuple_GET_ITEM(list, pos)
419 int execFile(const char *file);
420 int execute(const std::string &pythonfile, const std::string &funcname);
421 static int call(ePyObject pFunc, ePyObject args);
422 static ePyObject resolve(const std::string &pythonfile, const std::string &funcname);
428 #endif // __lib_python_python_class_h