Skip to content

Commit 3b4050b

Browse files
committed
Fix refcount problems seen when re-initializing Python after finalizing
A sequence of calls like the following would crash in Python when reinitializing the interpreter the second time: PythonQt::init(0); ... Py_Finalize(); PythonQt::init(0); ... Py_Finalize();
1 parent c0d817d commit 3b4050b

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/PythonQt.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ PythonQtClassWrapper* PythonQtPrivate::createNewPythonQtClassWrapper(PythonQtCla
657657
PyObject* className = PyString_FromString(pythonClassName.constData());
658658

659659
PyObject* baseClasses = PyTuple_New(1);
660+
Py_INCREF(&PythonQtInstanceWrapper_Type);
660661
PyTuple_SET_ITEM(baseClasses, 0, (PyObject*)&PythonQtInstanceWrapper_Type);
661662

662663
PyObject* typeDict = PyDict_New();
@@ -1611,6 +1612,7 @@ void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQ
16111612
#endif
16121613
_p->_pythonQtModuleName = name;
16131614

1615+
Py_INCREF(&PythonQtBoolResult_Type);
16141616
PyModule_AddObject(_p->pythonQtModule().object(), "BoolResult", (PyObject*)&PythonQtBoolResult_Type);
16151617
PythonQtObjectPtr sys;
16161618
sys.setNewRef(PyImport_ImportModule("sys"));
@@ -1634,7 +1636,9 @@ void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQ
16341636
Py_ssize_t old_size = PyTuple_Size(old_module_names);
16351637
PyObject *module_names = PyTuple_New(old_size + 1);
16361638
for (Py_ssize_t i = 0; i < old_size; i++) {
1637-
PyTuple_SetItem(module_names, i, PyTuple_GetItem(old_module_names, i));
1639+
PyObject *item = PyTuple_GetItem(old_module_names, i);
1640+
Py_INCREF(item);
1641+
PyTuple_SetItem(module_names, i, item);
16381642
}
16391643
PyTuple_SetItem(module_names, old_size, PyString_FromString(name.constData()));
16401644
PyModule_AddObject(sys.object(), "builtin_module_names", module_names);

0 commit comments

Comments
 (0)