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(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); }
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);
54 inline ePyObject::ePyObject()
56 #ifdef PYTHON_REFCOUNT_DEBUG
57 ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
62 inline ePyObject::ePyObject(const ePyObject &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)
71 inline ePyObject::ePyObject(PyObject *ob)
73 #ifdef PYTHON_REFCOUNT_DEBUG
74 ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
79 #ifdef PYTHON_REFCOUNT_DEBUG
80 inline ePyObject::ePyObject(PyObject *ob, const char* file, int line)
82 ,m_file(file), m_line(line), m_from(ob->ob_refcnt), m_to(ob->ob_refcnt), m_erased(false)
87 inline ePyObject::ePyObject(PyDictObject *ob)
89 #ifdef PYTHON_REFCOUNT_DEBUG
90 ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
95 inline ePyObject::ePyObject(PyTupleObject *ob)
97 #ifdef PYTHON_REFCOUNT_DEBUG
98 ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
103 inline ePyObject::ePyObject(PyListObject *ob)
105 #ifdef PYTHON_REFCOUNT_DEBUG
106 ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
111 inline ePyObject::ePyObject(PyStringObject *ob)
113 #ifdef PYTHON_REFCOUNT_DEBUG
114 ,m_file(0), m_line(0), m_from(0), m_to(0), m_erased(false)
119 #ifndef PYTHON_REFCOUNT_DEBUG
120 inline ePyObject &ePyObject::operator=(PyObject *ob)
126 inline ePyObject &ePyObject::operator=(const ePyObject &ob)
132 inline ePyObject::operator PyObject*()
137 inline void ePyObject::incref()
142 inline void ePyObject::decref()
147 #endif // ! PYTHON_REFCOUNT_DEBUG
149 #endif // !SWIG && !SKIP_PART1
153 #ifdef PYTHON_REFCOUNT_DEBUG
154 inline void Impl_Py_DECREF(const char* file, int line, const ePyObject &obj)
156 ((ePyObject*)(&obj))->decref(file, line);
159 inline void Impl_Py_INCREF(const char* file, int line, const ePyObject &obj)
161 ((ePyObject*)(&obj))->incref(file, line);
164 inline void Impl_Py_XDECREF(const char* file, int line, const ePyObject &obj)
167 ((ePyObject*)(&obj))->decref(file, line);
170 inline void Impl_Py_XINCREF(const char* file, int line, const ePyObject &obj)
173 ((ePyObject*)(&obj))->incref(file, line);
176 inline ePyObject Impl_PyTuple_New(const char* file, int line, int elements=0)
178 return ePyObject(PyTuple_New(elements), file, line);
181 inline ePyObject Impl_PyList_New(const char* file, int line, int elements=0)
183 return ePyObject(PyList_New(elements), file, line);
186 inline ePyObject Impl_PyDict_New(const char* file, int line)
188 return ePyObject(PyDict_New(), file, line);
191 inline ePyObject Impl_PyString_FromString(const char* file, int line, const char *str)
193 return ePyObject(PyString_FromString(str), file, line);
196 inline ePyObject Impl_PyString_FromFormat(const char* file, int line, const char *fmt, ...)
200 PyObject *ob = PyString_FromFormatV(fmt, ap);
202 return ePyObject(ob, file, line);
205 inline ePyObject Impl_PyInt_FromLong(const char* file, int line, long val)
207 return ePyObject(PyInt_FromLong(val), file, line);
210 inline ePyObject Impl_PyLong_FromLong(const char* file, int line, long val)
212 return ePyObject(PyLong_FromLong(val), file, line);
215 inline ePyObject Impl_PyLong_FromUnsignedLong(const char* file, int line, unsigned long val)
217 return ePyObject(PyLong_FromUnsignedLong(val), file, line);
220 inline ePyObject Impl_PyLong_FromLongLong(const char* file, int line, long long val)
222 return ePyObject(PyLong_FromLongLong(val), file, line);
225 inline ePyObject Impl_PyList_GET_ITEM(const char *file, int line, ePyObject list, unsigned int pos)
227 return ePyObject(PyList_GET_ITEM(list, pos), file, line);
230 inline ePyObject Impl_PyTuple_GET_ITEM(const char *file, int line, ePyObject list, unsigned int pos)
232 return ePyObject(PyTuple_GET_ITEM(list, pos), file, line);
235 inline void Impl_Py_DECREF(const ePyObject &obj)
237 ((ePyObject*)(&obj))->decref();
240 inline void Impl_Py_INCREF(const ePyObject &obj)
242 ((ePyObject*)(&obj))->incref();
245 inline void Impl_Py_XDECREF(const ePyObject &obj)
248 ((ePyObject*)(&obj))->decref();
251 inline void Impl_Py_XINCREF(const ePyObject &obj)
254 ((ePyObject*)(&obj))->incref();
257 inline ePyObject Impl_PyTuple_New(int elements=0)
259 return PyTuple_New(elements);
262 inline ePyObject Impl_PyList_New(int elements=0)
264 return PyList_New(elements);
267 inline ePyObject Impl_PyDict_New()
272 inline ePyObject Impl_PyString_FromString(const char *str)
274 return PyString_FromString(str);
277 inline ePyObject Impl_PyString_FromFormat(const char *fmt, ...)
281 PyObject *ob = PyString_FromFormatV(fmt, ap);
283 return ePyObject(ob);
286 inline ePyObject Impl_PyInt_FromLong(long val)
288 return PyInt_FromLong(val);
291 inline ePyObject Impl_PyLong_FromLong(long val)
293 return PyLong_FromLong(val);
296 inline ePyObject Impl_PyLong_FromUnsignedLong(unsigned long val)
298 return PyLong_FromUnsignedLong(val);
301 inline ePyObject Impl_PyLong_FromLongLong(long long val)
303 return PyLong_FromLongLong(val);
306 inline ePyObject Impl_PyList_GET_ITEM(ePyObject list, unsigned int pos)
308 return PyList_GET_ITEM(list, pos);
311 inline ePyObject Impl_PyTuple_GET_ITEM(ePyObject list, unsigned int pos)
313 return PyTuple_GET_ITEM(list, pos);
317 inline void Impl_INCREF(PyObject *ob)
322 inline void Impl_DECREF(PyObject *ob)
326 #define Org_Py_INCREF(obj) Impl_INCREF(obj)
327 #define Org_Py_DECREF(obj) Impl_DECREF(obj)
332 #undef PyList_GET_ITEM
333 #undef PyTuple_GET_ITEM
334 #ifdef PYTHON_REFCOUNT_DEBUG
335 #define Py_DECREF(obj) Impl_Py_DECREF(__FILE__, __LINE__, obj)
336 #define Py_XDECREF(obj) Impl_Py_XDECREF(__FILE__, __LINE__, obj)
337 #define Py_INCREF(obj) Impl_Py_INCREF(__FILE__, __LINE__, obj)
338 #define Py_XINCREF(obj) Impl_Py_XINCREF(__FILE__, __LINE__, obj)
339 #define PyList_New(args...) Impl_PyList_New(__FILE__, __LINE__, args)
340 #define PyTuple_New(args...) Impl_PyTuple_New(__FILE__, __LINE__, args)
341 #define PyDict_New(...) Impl_PyDict_New(__FILE__, __LINE__)
342 #define PyString_FromString(str) Impl_PyString_FromString(__FILE__, __LINE__, str)
343 #define PyString_FromFormat(str, args...) Impl_PyString_FromFormat(__FILE__, __LINE__, str, args)
344 #define PyInt_FromLong(val) Impl_PyInt_FromLong(__FILE__, __LINE__, val)
345 #define PyLong_FromLong(val) Impl_PyLong_FromLong(__FILE__, __LINE__, val)
346 #define PyLong_FromUnsignedLong(val) Impl_PyLong_FromUnsignedLong(__FILE__, __LINE__, val)
347 #define PyLong_FromLongLong(val) Impl_PyLong_FromLongLong(__FILE__, __LINE__, val)
348 #define PyList_GET_ITEM(list, pos) Impl_PyList_GET_ITEM(__FILE__, __LINE__, list, pos)
349 #define PyTuple_GET_ITEM(list, pos) Impl_PyTuple_GET_ITEM(__FILE__, __LINE__, list, pos)
351 #define Py_DECREF(obj) Impl_Py_DECREF(obj)
352 #define Py_XDECREF(obj) Impl_Py_XDECREF(obj)
353 #define Py_INCREF(obj) Impl_Py_INCREF(obj)
354 #define Py_XINCREF(obj) Impl_Py_XINCREF(obj)
355 #define PyList_New(args...) Impl_PyList_New(args)
356 #define PyTuple_New(args...) Impl_PyTuple_New(args)
357 #define PyDict_New(...) Impl_PyDict_New()
358 #define PyString_FromString(str) Impl_PyString_FromString(str)
359 #define PyString_FromFormat(str, args...) Impl_PyString_FromFormat(str, args)
360 #define PyInt_FromLong(val) Impl_PyInt_FromLong(val)
361 #define PyLong_FromLong(val) Impl_PyLong_FromLong(val)
362 #define PyLong_FromUnsignedLong(val) Impl_PyLong_FromUnsignedLong(val)
363 #define PyLong_FromLongLong(val) Impl_PyLong_FromLongLong(val)
364 #define PyList_GET_ITEM(list, pos) Impl_PyList_GET_ITEM(list, pos)
365 #define PyTuple_GET_ITEM(list, pos) Impl_PyTuple_GET_ITEM(list, pos)
373 int execFile(const char *file);
374 int execute(const std::string &pythonfile, const std::string &funcname);
375 static int call(ePyObject pFunc, ePyObject args);
376 static ePyObject resolve(const std::string &pythonfile, const std::string &funcname);
383 #endif // __lib_python_python_class_h