Skip to content

Commit 08b6873

Browse files
Thibault-Pelletierusiems
authored andcommitted
fix(cleanup): Skip GIL scope when Python is finalized
Avoid crash during cleanup with threading enabled if Python has been finalized before cleanup.
1 parent 4091abd commit 08b6873

3 files changed

Lines changed: 10 additions & 5 deletions

File tree

‎src/PythonQtThreadSupport.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ bool PythonQtGILScope::_enableGILScope = false;
4747
PythonQtGILScope::PythonQtGILScope()
4848
: _ensured(false)
4949
{
50-
if (_enableGILScope) {
50+
if (_enableGILScope && Py_IsInitialized()) {
5151
_state = PyGILState_Ensure();
5252
_ensured = true;
5353
}

‎tests/PythonQtTestCleanup.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ void PythonQtTestCleanup::testSignalReceiverCleanup()
7979

8080
void PythonQtTestCleanup::testPyFinalizeThenPythonQtCleanup()
8181
{
82+
QVERIFY(_helper->runScript("import PythonQt.QtCore\n"
83+
"timer = PythonQt.QtCore.QTimer(obj)\n"
84+
"timer.connect('destroyed()', obj.onDestroyed)\n"
85+
"obj.setPassed()\n"));
86+
8287
if (Py_IsInitialized()) {
8388
Py_Finalize();
8489
}

‎tests/PythonQtTestMain.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ int main(int argc, char* argv[])
4949
{
5050
QApplication qapp(argc, argv);
5151

52+
PythonQt::setEnableThreadSupport(QProcessEnvironment::systemEnvironment().contains("PYTHONQT_ENABLE_THREAD_SUPPORT"));
53+
5254
if (QProcessEnvironment::systemEnvironment().contains("PYTHONQT_RUN_ONLY_MEMORY_TESTS")) {
5355
PythonQtMemoryTests test;
54-
QTest::qExec(&test, argc, argv);
55-
return 0;
56+
return QTest::qExec(&test, argc, argv);
5657
}
5758

5859
if (QProcessEnvironment::systemEnvironment().contains("PYTHONQT_RUN_ONLY_CLEANUP_TESTS")) {
5960
PythonQtTestCleanup cleanup;
60-
QTest::qExec(&cleanup, argc, argv);
61-
return 0;
61+
return QTest::qExec(&cleanup, argc, argv);
6262
}
6363

6464
PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);

0 commit comments

Comments
 (0)