Skip to content

Commit 2a97015

Browse files
msmolensjcfr
authored andcommitted
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 76294d1 commit 2a97015

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/PythonQt.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ PythonQtClassWrapper* PythonQtPrivate::createNewPythonQtClassWrapper(PythonQtCla
695695
PyObject* className = PyString_FromString(pythonClassName.constData());
696696

697697
PyObject* baseClasses = PyTuple_New(1);
698+
Py_INCREF(&PythonQtInstanceWrapper_Type);
698699
PyTuple_SET_ITEM(baseClasses, 0, (PyObject*)&PythonQtInstanceWrapper_Type);
699700

700701
PyObject* typeDict = PyDict_New();
@@ -1650,6 +1651,7 @@ void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQ
16501651
#endif
16511652
_p->_pythonQtModuleName = name;
16521653

1654+
Py_INCREF(&PythonQtBoolResult_Type);
16531655
PyModule_AddObject(_p->pythonQtModule().object(), "BoolResult", (PyObject*)&PythonQtBoolResult_Type);
16541656
PythonQtObjectPtr sys;
16551657
sys.setNewRef(PyImport_ImportModule("sys"));
@@ -1673,7 +1675,9 @@ void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQ
16731675
Py_ssize_t old_size = PyTuple_Size(old_module_names);
16741676
PyObject *module_names = PyTuple_New(old_size + 1);
16751677
for (Py_ssize_t i = 0; i < old_size; i++) {
1676-
PyTuple_SetItem(module_names, i, PyTuple_GetItem(old_module_names, i));
1678+
PyObject *item = PyTuple_GetItem(old_module_names, i);
1679+
Py_INCREF(item);
1680+
PyTuple_SetItem(module_names, i, item);
16771681
}
16781682
PyTuple_SetItem(module_names, old_size, PyString_FromString(name.constData()));
16791683
PyModule_AddObject(sys.object(), "builtin_module_names", module_names);

0 commit comments

Comments
 (0)