aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2021-01-05 15:20:24 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2021-01-05 22:11:17 +0100
commit50a30e50ba5edd2cdeefe3118e0a0f7e79e3732f (patch)
treedfbda587878dc6c7c9423cc1d1789d1a1de8a8af
parent05db21e661c6ba9eb69516cb1386bcbd431b7d2d (diff)
shiboken6: Guard against repeated invocation of the module init function
It cannot entirely be avoided in the case of the scriptable application example. Generate code checking on the global variable. Pick-to: 6.0 Task-number: PYSIDE-487 Change-Id: I12bcd9df37c39f78f1d7edc63e16b3c6a9525011 Reviewed-by: Christian Tismer <tismer@stackless.com>
-rw-r--r--sources/shiboken6/generator/shiboken/cppgenerator.cpp7
1 files changed, 6 insertions, 1 deletions
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp
index 8858132b2..aaab6906a 100644
--- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp
+++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp
@@ -6128,8 +6128,13 @@ bool CppGenerator::finishGeneration()
// PYSIDE-510: Create a signatures string for the introspection feature.
writeSignatureStrings(s, signatureStream.toString(), moduleName(), "global functions");
+ // Write module init function
+ const QString globalModuleVar = pythonModuleObjectName();
s << "extern \"C\" LIBSHIBOKEN_EXPORT PyObject *PyInit_"
<< moduleName() << "()\n{\n" << indent;
+ // Guard against repeated invocation
+ s << "if (" << globalModuleVar << " != nullptr)\n"
+ << indent << "return " << globalModuleVar << ";\n" << outdent;
ErrorCode errorCode(QLatin1String("nullptr"));
// module inject-code target/beginning
@@ -6162,7 +6167,7 @@ bool CppGenerator::finishGeneration()
<< "PyObject *module = Shiboken::Module::create(\"" << moduleName()
<< "\", &moduledef);\n\n"
<< "// Make module available from global scope\n"
- << pythonModuleObjectName() << " = module;\n\n"
+ << globalModuleVar << " = module;\n\n"
<< "// Initialize classes in the type system\n"
<< s_classPythonDefines.toString();