diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-01-05 13:44:24 +0100 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-01-06 08:51:28 +0100 |
commit | b62040783613568901e76a26799e130632004a7e (patch) | |
tree | edb38100c2e21f7b92cc49f6cd8874a381342d33 /examples/scriptableapplication/pythonutils.cpp | |
parent | 50a30e50ba5edd2cdeefe3118e0a0f7e79e3732f (diff) |
scriptable application: Actually make the generated module available
A call to PyImport_AppendInittab() before Py_Initialize() is required
to be able to import the module. Previously, the example would only
add the instance of the mainwindow under the "__main__" module.
Pick-to: 6.0
Task-number: PYSIDE-841
Change-Id: Ib87ddd9fa9e4dbdcf413abe1d9e6273811fc414c
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'examples/scriptableapplication/pythonutils.cpp')
-rw-r--r-- | examples/scriptableapplication/pythonutils.cpp | 17 |
1 files changed, 7 insertions, 10 deletions
diff --git a/examples/scriptableapplication/pythonutils.cpp b/examples/scriptableapplication/pythonutils.cpp index eef2fada7..18ac35112 100644 --- a/examples/scriptableapplication/pythonutils.cpp +++ b/examples/scriptableapplication/pythonutils.cpp @@ -64,11 +64,8 @@ /* from AppLib bindings */ -#if PY_MAJOR_VERSION >= 3 - extern "C" PyObject *PyInit_AppLib(); -#else - extern "C" void initAppLib(); -#endif +extern "C" PyObject *PyInit_AppLib(); +static const char moduleName[] = "AppLib"; // This variable stores all Python types exported by this module. extern PyTypeObject **SbkAppLibTypes; @@ -111,15 +108,15 @@ State init() if (qEnvironmentVariableIsSet(virtualEnvVar)) initVirtualEnvironment(); + if (PyImport_AppendInittab(moduleName, PyInit_AppLib) == -1) { + qWarning("Failed to add the module '%s' to the table of built-in modules.", moduleName); + return state; + } + Py_Initialize(); qAddPostRoutine(cleanup); state = PythonInitialized; -#if PY_MAJOR_VERSION >= 3 const bool pythonInitialized = PyInit_AppLib() != nullptr; -#else - const bool pythonInitialized = true; - initAppLib(); -#endif const bool pyErrorOccurred = PyErr_Occurred() != nullptr; if (pythonInitialized && !pyErrorOccurred) { state = AppModuleLoaded; |