From 7f45ba247a7cbb93f44dfb8650d5d9a86ac12301 Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Thu, 24 Sep 2009 22:02:53 +0000 Subject: more fixes to pytho module. now it is able to correctly run python code :) git-svn-id: https://svn.kvirc.de/svn/trunk/kvirc@3525 17fca916-40b9-46aa-a4ea-0a15b648b75c --- src/modules/pythoncore/libkvipythoncore.cpp | 45 ++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 7 deletions(-) (limited to 'src/modules/pythoncore') diff --git a/src/modules/pythoncore/libkvipythoncore.cpp b/src/modules/pythoncore/libkvipythoncore.cpp index 37fa88dc0..fd9602b9a 100644 --- a/src/modules/pythoncore/libkvipythoncore.cpp +++ b/src/modules/pythoncore/libkvipythoncore.cpp @@ -112,19 +112,51 @@ bool KviPythonInterpreter::execute( return false; } + int retVal; + // grab the global interpreter lock PyEval_AcquireLock(); // swap in my thread state PyThreadState_Swap(m_pThreadState); // execute some python code - PyRun_SimpleString("import sys\n"); - PyRun_SimpleString("sys.stdout.write('Hello from a C thread!\\n')\n"); + retVal = PyRun_SimpleString(szCode.toUtf8().data()); + + szRetVal.setNum(retVal); + + if (PyErr_Occurred()) + { + // PyErr_Print(); + PyObject *type = NULL, *value = NULL, *traceback = NULL, *pyString = NULL; + PyErr_Fetch(&type, &value, &traceback); + PyErr_Clear(); + szError = "Python error: "; + if (type != NULL && (pyString=PyObject_Str(type))!=NULL && + (PyString_Check(pyString))) + szError += PyString_AsString(pyString); + else szError += " "; + Py_XDECREF(pyString); + + if (value != NULL && (pyString=PyObject_Str(value))!=NULL && + (PyString_Check(pyString))) + { + szError += ": "; + szError += PyString_AsString(value); + } else szError += " "; + Py_XDECREF(pyString); + + Py_XDECREF(type); + Py_XDECREF(value); + Py_XDECREF(traceback); + } + // clear the thread state PyThreadState_Swap(NULL); // release our hold on the global interpreter PyEval_ReleaseLock(); - return true; + if(retVal) + return false; + else return true; } static KviPointerHashTable * g_pInterpreters = 0; @@ -223,7 +255,7 @@ static bool pythoncore_module_init(KviModule *) PyEval_ReleaseLock(); // Initialize the Python module for KVIrc -// python_init(); + python_init(); g_pInterpreters = new KviPointerHashTable(17,false); g_pInterpreters->setAutoDelete(false); @@ -243,10 +275,9 @@ static bool pythoncore_module_cleanup(KviModule *) // shut down the interpreter PyEval_AcquireLock(); + PyThreadState_Swap(mainThreadState); + PyEval_ReleaseLock(); Py_Finalize(); - - // Close the intepreter - Py_Exit(0); #endif // COMPILE_PYTHON_SUPPORT return true; -- cgit v1.3.1-10-gc9f91