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 #endif // ! PYTHON_REFCOUNT_DEBUG
160 #endif // !SWIG && !SKIP_PART1
164 #ifdef PYTHON_REFCOUNT_DEBUG
165 inline void Impl_Py_DECREF(const char* file, int line, const ePyObject &obj)
167 ((ePyObject*)(&obj))->decref(file, line);
170 inline void Impl_Py_INCREF(const char* file, int line, const ePyObject &obj)
172 ((ePyObject*)(&obj))->incref(file, line);
175 inline void Impl_Py_XDECREF(const char* file, int line, const ePyObject &obj)
178 ((ePyObject*)(&obj))->decref(file, line);
181 inline void Impl_Py_XINCREF(const char* file, int line, const ePyObject &obj)
184 ((ePyObject*)(&obj))->incref(file, line);
187 inline ePyObject Impl_PyTuple_New(const char* file, int line, int elements=0)
189 return ePyObject(PyTuple_New(elements), file, line);
192 inline ePyObject Impl_PyList_New(const char* file, int line, int elements=0)
194 return ePyObject(PyList_New(elements), file, line);
197 inline ePyObject Impl_PyDict_New(const char* file, int line)
199 return ePyObject(PyDict_New(), file, line);
202 inline ePyObject Impl_PyString_FromString(const char* file, int line, const char *str)
204 return ePyObject(PyString_FromString(str), file, line);
207 inline ePyObject Impl_PyString_FromFormat(const char* file, int line, const char *fmt, ...)
211 PyObject *ob = PyString_FromFormatV(fmt, ap);
213 return ePyObject(ob, file, line);
216 inline ePyObject Impl_PyInt_FromLong(const char* file, int line, long val)
218 return ePyObject(PyInt_FromLong(val), file, line);
221 inline ePyObject Impl_PyLong_FromLong(const char* file, int line, long val)
223 return ePyObject(PyLong_FromLong(val), file, line);
226 inline ePyObject Impl_PyLong_FromUnsignedLong(const char* file, int line, unsigned long val)
228 return ePyObject(PyLong_FromUnsignedLong(val), file, line);
231 inline ePyObject Impl_PyLong_FromLongLong(const char* file, int line, long long val)
233 return ePyObject(PyLong_FromLongLong(val), file, line);
236 inline ePyObject Impl_PyList_GET_ITEM(const char *file, int line, ePyObject list, unsigned int pos)
238 return ePyObject(PyList_GET_ITEM(list, pos), file, line);
241 inline ePyObject Impl_PyTuple_GET_ITEM(const char *file, int line, ePyObject list, unsigned int pos)
243 return ePyObject(PyTuple_GET_ITEM(list, pos), file, line);
246 inline void Impl_Py_DECREF(const ePyObject &obj)
248 ((ePyObject*)(&obj))->decref();
251 inline void Impl_Py_INCREF(const ePyObject &obj)
253 ((ePyObject*)(&obj))->incref();
256 inline void Impl_Py_XDECREF(const ePyObject &obj)
259 ((ePyObject*)(&obj))->decref();
262 inline void Impl_Py_XINCREF(const ePyObject &obj)
265 ((ePyObject*)(&obj))->incref();
268 inline ePyObject Impl_PyTuple_New(int elements=0)
270 return PyTuple_New(elements);
273 inline ePyObject Impl_PyList_New(int elements=0)
275 return PyList_New(elements);
278 inline ePyObject Impl_PyDict_New()
283 inline ePyObject Impl_PyString_FromString(const char *str)
285 return PyString_FromString(str);
288 inline ePyObject Impl_PyString_FromFormat(const char *fmt, ...)
292 PyObject *ob = PyString_FromFormatV(fmt, ap);
294 return ePyObject(ob);
297 inline ePyObject Impl_PyInt_FromLong(long val)
299 return PyInt_FromLong(val);
302 inline ePyObject Impl_PyLong_FromLong(long val)
304 return PyLong_FromLong(val);
307 inline ePyObject Impl_PyLong_FromUnsignedLong(unsigned long val)
309 return PyLong_FromUnsignedLong(val);
312 inline ePyObject Impl_PyLong_FromLongLong(long long val)
314 return PyLong_FromLongLong(val);
317 inline ePyObject Impl_PyList_GET_ITEM(ePyObject list, unsigned int pos)
319 return PyList_GET_ITEM(list, pos);
322 inline ePyObject Impl_PyTuple_GET_ITEM(ePyObject list, unsigned int pos)
324 return PyTuple_GET_ITEM(list, pos);
328 inline void Impl_INCREF(PyObject *ob)
333 inline void Impl_DECREF(PyObject *ob)
337 #define Org_Py_INCREF(obj) Impl_INCREF(obj)
338 #define Org_Py_DECREF(obj) Impl_DECREF(obj)
343 #undef PyList_GET_ITEM
344 #undef PyTuple_GET_ITEM
345 #ifdef PYTHON_REFCOUNT_DEBUG
346 #define Py_DECREF(obj) Impl_Py_DECREF(__FILE__, __LINE__, obj)
347 #define Py_XDECREF(obj) Impl_Py_XDECREF(__FILE__, __LINE__, obj)
348 #define Py_INCREF(obj) Impl_Py_INCREF(__FILE__, __LINE__, obj)
349 #define Py_XINCREF(obj) Impl_Py_XINCREF(__FILE__, __LINE__, obj)
350 #define PyList_New(args...) Impl_PyList_New(__FILE__, __LINE__, args)
351 #define PyTuple_New(args...) Impl_PyTuple_New(__FILE__, __LINE__, args)
352 #define PyDict_New(...) Impl_PyDict_New(__FILE__, __LINE__)
353 #define PyString_FromString(str) Impl_PyString_FromString(__FILE__, __LINE__, str)
354 #define PyString_FromFormat(str, args...) Impl_PyString_FromFormat(__FILE__, __LINE__, str, args)
355 #define PyInt_FromLong(val) Impl_PyInt_FromLong(__FILE__, __LINE__, val)
356 #define PyLong_FromLong(val) Impl_PyLong_FromLong(__FILE__, __LINE__, val)
357 #define PyLong_FromUnsignedLong(val) Impl_PyLong_FromUnsignedLong(__FILE__, __LINE__, val)
358 #define PyLong_FromLongLong(val) Impl_PyLong_FromLongLong(__FILE__, __LINE__, val)
359 #define PyList_GET_ITEM(list, pos) Impl_PyList_GET_ITEM(__FILE__, __LINE__, list, pos)
360 #define PyTuple_GET_ITEM(list, pos) Impl_PyTuple_GET_ITEM(__FILE__, __LINE__, list, pos)
362 #define Py_DECREF(obj) Impl_Py_DECREF(obj)
363 #define Py_XDECREF(obj) Impl_Py_XDECREF(obj)
364 #define Py_INCREF(obj) Impl_Py_INCREF(obj)
365 #define Py_XINCREF(obj) Impl_Py_XINCREF(obj)
366 #define PyList_New(args...) Impl_PyList_New(args)
367 #define PyTuple_New(args...) Impl_PyTuple_New(args)
368 #define PyDict_New(...) Impl_PyDict_New()
369 #define PyString_FromString(str) Impl_PyString_FromString(str)
370 #define PyString_FromFormat(str, args...) Impl_PyString_FromFormat(str, args)
371 #define PyInt_FromLong(val) Impl_PyInt_FromLong(val)
372 #define PyLong_FromLong(val) Impl_PyLong_FromLong(val)
373 #define PyLong_FromUnsignedLong(val) Impl_PyLong_FromUnsignedLong(val)
374 #define PyLong_FromLongLong(val) Impl_PyLong_FromLongLong(val)
375 #define PyList_GET_ITEM(list, pos) Impl_PyList_GET_ITEM(list, pos)
376 #define PyTuple_GET_ITEM(list, pos) Impl_PyTuple_GET_ITEM(list, pos)
384 int execFile(const char *file);
385 int execute(const std::string &pythonfile, const std::string &funcname);
386 static int call(ePyObject pFunc, ePyObject args);
387 static ePyObject resolve(const std::string &pythonfile, const std::string &funcname);
394 #endif // __lib_python_python_class_h