aboutsummaryrefslogtreecommitdiffstats
path: root/sources
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2021-01-05 15:20:24 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-01-05 22:21:56 +0000
commit71a62eb4fa193f9af35c52918ec2dac613f2504a (patch)
tree75a26353b6e6d0f6d5da272c61782c8234ca0189 /sources
parent39ed72638b893c7bb15472775a009252b2d3aee1 (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. Task-number: PYSIDE-487 Change-Id: I12bcd9df37c39f78f1d7edc63e16b3c6a9525011 Reviewed-by: Christian Tismer <tismer@stackless.com> (cherry picked from commit 50a30e50ba5edd2cdeefe3118e0a0f7e79e3732f) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'sources')
-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();