1 #include <lib/base/eerror.h>
4 #define _POSIX_C_SOURCE 200112L
5 extern "C" void init_enigma();
6 extern "C" void eBaseInit(void);
7 extern "C" void eConsoleInit(void);
8 extern void bsodFatal(const char *component);
11 #include <lib/python/python.h>
14 #ifdef PYTHON_REFCOUNT_DEBUG
15 ePyObject &ePyObject::operator=(PyObject *ob)
25 ePyObject &ePyObject::operator=(const ePyObject &ob)
36 ePyObject::operator PyObject*()
40 if (!m_erased && m_ob->ob_refcnt > 0)
42 eDebug("invalid access PyObject %s with refcount <= 0 %d",
43 m_erased ? "deleted" : "undeleted", m_ob->ob_refcnt);
45 eDebug("last modified in file %s line %d from %d to %d",
46 m_file, m_line, m_from, m_to);
47 bsodFatal("enigma2, refcnt");
52 void ePyObject::incref(const char *file, int line)
56 eDebug("invalid incref python object with null pointer %s %d!!!", file, line);
58 eDebug("last modified in file %s line %d from %d to %d",
59 m_file, m_line, m_from, m_to);
60 bsodFatal("enigma2, refcnt");
62 if (m_erased || m_ob->ob_refcnt <= 0)
64 eDebug("invalid incref %s python object with refcounting value %d in file %s line %d!!!",
65 m_erased ? "deleted" : "undeleted", m_ob->ob_refcnt, file, line);
67 eDebug("last modified in file %s line %d from %d to %d",
68 m_file, m_line, m_from, m_to);
69 bsodFatal("enigma2, refcnt");
71 if (m_ob->ob_refcnt == 0x7FFFFFFF)
73 eDebug("invalid incref %s python object with refcounting value %d (MAX_INT!!!) in file %s line %d!!!",
74 m_erased ? "deleted" : "undeleted", m_ob->ob_refcnt, file, line);
76 eDebug("last modified in file %s line %d from %d to %d",
77 m_file, m_line, m_from, m_to);
78 bsodFatal("enigma2, refcnt");
82 m_from = m_ob->ob_refcnt;
87 void ePyObject::decref(const char *file, int line)
91 eDebug("invalid decref python object with null pointer %s %d!!!", file, line);
93 eDebug("last modified in file %s line %d from %d to %d",
94 m_file, m_line, m_from, m_to);
95 bsodFatal("enigma2, refcnt");
97 if (m_erased || m_ob->ob_refcnt <= 0)
99 eDebug("invalid decref %s python object with refcounting value %d in file %s line %d!!!",
100 m_erased ? "deleted" : "undeleted", m_ob->ob_refcnt, file, line);
102 eDebug("last modified in file %s line %d from %d to %d",
103 m_file, m_line, m_from, m_to);
104 bsodFatal("enigma2, refcnt");
108 m_from = m_ob->ob_refcnt;
113 #endif // PYTHON_REFCOUNT_DEBUG
116 #include <lib/python/python.h>
121 // Py_VerboseFlag = 1;
123 // Py_OptimizeFlag = 1;
126 PyEval_InitThreads();
138 int ePython::execFile(const char *file)
140 FILE *fp = fopen(file, "r");
143 int ret = PyRun_SimpleFile(fp, file);
148 int ePython::execute(const std::string &pythonfile, const std::string &funcname)
150 ePyObject pName, pModule, pDict, pFunc, pArgs, pValue;
151 pName = PyString_FromString(pythonfile.c_str());
153 pModule = PyImport_Import(pName);
158 pDict = PyModule_GetDict(pModule);
160 pFunc = PyDict_GetItemString(pDict, funcname.c_str());
162 if (pFunc && PyCallable_Check(pFunc))
164 pArgs = PyTuple_New(0);
165 // implement arguments..
166 pValue = PyObject_CallObject(pFunc, pArgs);
170 printf("Result of call: %ld\n", PyInt_AsLong(pValue));
181 if (PyErr_Occurred())
188 int ePython::call(ePyObject pFunc, ePyObject pArgs)
192 if (pFunc && PyCallable_Check(pFunc))
194 pValue = PyObject_CallObject(pFunc, pArgs);
197 if (PyInt_Check(pValue))
198 res = PyInt_AsLong(pValue);
205 ePyObject FuncStr = PyObject_Str(pFunc);
206 ePyObject ArgStr = PyObject_Str(pArgs);
207 eDebug("(PyObject_CallObject(%s,%s) failed)", PyString_AS_STRING(FuncStr), PyString_AS_STRING(ArgStr));
216 ePyObject ePython::resolve(const std::string &pythonfile, const std::string &funcname)
218 ePyObject pName, pModule, pDict, pFunc;
220 pName = PyString_FromString(pythonfile.c_str());
222 pModule = PyImport_Import(pName);
227 pDict = PyModule_GetDict(pModule);
228 pFunc = PyDict_GetItemString(pDict, funcname.c_str());
231 } else if (PyErr_Occurred())