From cab89774e5b773ed8bf64a1c523fb90a5990cd20 Mon Sep 17 00:00:00 2001 From: staticfox Date: Thu, 21 Apr 2016 05:05:49 -0400 Subject: Python: Properly initialize imported kvirc module --- src/modules/pythoncore/kvircmodule.cpp | 55 +++++++++++++++++++++-------- src/modules/pythoncore/libkvipythoncore.cpp | 3 -- 2 files changed, 41 insertions(+), 17 deletions(-) (limited to 'src/modules/pythoncore') diff --git a/src/modules/pythoncore/kvircmodule.cpp b/src/modules/pythoncore/kvircmodule.cpp index bc26c0759..79eb15d72 100644 --- a/src/modules/pythoncore/kvircmodule.cpp +++ b/src/modules/pythoncore/kvircmodule.cpp @@ -39,6 +39,24 @@ #include +// For compatibility with Python 3 +#if PY_MAJOR_VERSION >= 3 +#define KVI_PyMODINIT_RETVAL(module) module +#else +#define KVI_PyMODINIT_RETVAL(module) ; (void)(module) +struct PyModuleDef { + char m_base; + const char *m_name, *m_doc; + int m_size; + PyMethodDef *m_methods; +}; +static const char PyModuleDef_HEAD_INIT = 0; +static PyObject *PyModule_Create(const struct PyModuleDef *def) +{ + return Py_InitModule3(def->m_name, def->m_methods, def->m_doc); +} +#endif + extern KviKvsRunTimeContext * g_pCurrentKvsContext; extern bool g_bExecuteQuiet; extern KviCString g_szLastReturnValue; @@ -367,7 +385,7 @@ static PyMethodDef KVIrcMethods[] = { PyMODINIT_FUNC python_init() { - static const PyCFunction PyKVIrc_API[] = { + static PyCFunction PyKVIrc_API[] = { PyKVIrc_echo, PyKVIrc_say, PyKVIrc_warning, @@ -379,21 +397,30 @@ PyMODINIT_FUNC python_init() PyKVIrc_internalWarning, PyKVIrc_error, }; - - PyObject * pModule; - PyObject * pC_API_Object; - - if(!(pModule = Py_InitModule3("kvirc", KVIrcMethods, nullptr))) - { + static struct PyModuleDef def = { + PyModuleDef_HEAD_INIT, + "kvirc", + "KVIrc module", + -1, + KVIrcMethods + }; + PyObject * pModule = PyModule_Create(&def); + if(!pModule) { KVI_ASSERT("Python: Unable to initialize kvirc import module"); - return; + } else { + // Create a CObject containing the API pointer array's address + PyObject * pC_API_Object = PyCObject_FromVoidPtr(PyKVIrc_API, nullptr); + if(pC_API_Object) + PyModule_AddObject(pModule,"_C_API",pC_API_Object); } - - // Create a CObject containing the API pointer array's address - pC_API_Object = PyCObject_FromVoidPtr(const_cast(PyKVIrc_API), nullptr); - - if(pC_API_Object) - PyModule_AddObject(pModule,"_C_API",pC_API_Object); + return KVI_PyMODINIT_RETVAL(pModule); } +static struct KviPythonModuleInitializer { + KviPythonModuleInitializer() + { + PyImport_AppendInittab("kvirc", python_init); + } +} initializer; + #endif diff --git a/src/modules/pythoncore/libkvipythoncore.cpp b/src/modules/pythoncore/libkvipythoncore.cpp index b8e6c8e7b..80c10325c 100644 --- a/src/modules/pythoncore/libkvipythoncore.cpp +++ b/src/modules/pythoncore/libkvipythoncore.cpp @@ -32,7 +32,6 @@ #include "KviLocale.h" #ifdef COMPILE_PYTHON_SUPPORT -#include "kvircmodule.h" #include "pythoncoreinterface.h" #include @@ -89,8 +88,6 @@ KviPythonInterpreter::KviPythonInterpreter() KviPythonLock lock{mainThreadState}; m_uptrThreadState.reset(Py_NewInterpreter()); - python_init(); - // hook in the kvirc error handling routines QString szPreCode = QString( "import kvirc\n" -- cgit v1.3.1-10-gc9f91