Skip to content

Commit c36cc0a

Browse files
MinyazevRusiems
authored andcommitted
Fix memory leak and add memory tests
1 parent 5331f00 commit c36cc0a

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

src/PythonQt.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1898,7 +1898,11 @@ void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQ
18981898
Py_XDECREF(old_module_names);
18991899

19001900
#ifdef PY3K
1901-
PyDict_SetItem(PyObject_GetAttrString(sys.object(), "modules"), PyUnicode_FromString(name.constData()), _p->_pythonQtModule.object());
1901+
PyObject* modulesAttr = PyObject_GetAttrString(sys.object(), "modules");
1902+
PyObject* pyUnicodeObject = PyUnicode_FromString(name.constData());
1903+
PyDict_SetItem(modulesAttr, pyUnicodeObject, _p->_pythonQtModule.object());
1904+
Py_XDECREF(modulesAttr);
1905+
Py_XDECREF(pyUnicodeObject);
19021906
#endif
19031907
}
19041908

tests/PythonQtTestMain.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,13 @@ int main( int argc, char **argv )
4848
{
4949
QApplication qapp(argc, argv);
5050

51-
PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
51+
if (QProcessEnvironment::systemEnvironment().contains("PYTHONQT_RUN_ONLY_MEMORY_TESTS")) {
52+
PythonQtMemoryTests test;
53+
QTest::qExec(&test, argc, argv);
54+
return 0;
55+
}
5256

57+
PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
5358
int failCount = 0;
5459
PythonQtTestApi api;
5560
failCount += QTest::qExec(&api, argc, argv);

tests/PythonQtTests.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,43 @@
4141

4242
#include "PythonQtTests.h"
4343

44+
void PythonQtMemoryTests::testBaseCleanup()
45+
{
46+
PythonQt::init();
47+
PythonQt::cleanup();
48+
}
49+
50+
void PythonQtMemoryTests::testCleanupWithFlags()
51+
{
52+
PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
53+
PythonQt::cleanup();
54+
}
55+
56+
void PythonQtMemoryTests::testInitAlreadyInitialized()
57+
{
58+
Py_InitializeEx(true);
59+
PythonQt::init(PythonQt::PythonAlreadyInitialized);
60+
PythonQt::cleanup();
61+
}
62+
63+
void PythonQtMemoryTests::testSeveralCleanup() {
64+
PythonQt::init();
65+
PythonQt::cleanup();
66+
67+
PythonQt::init();
68+
PythonQt::cleanup();
69+
}
70+
71+
void PythonQtMemoryTests::testInitWithPreconfig() {
72+
#if PY_VERSION_HEX >= 0x030800
73+
PyConfig config;
74+
PyConfig_InitPythonConfig(&config);
75+
Py_InitializeFromConfig(&config);
76+
PythonQt::init(PythonQt::RedirectStdOut | PythonQt::PythonAlreadyInitialized);
77+
PythonQt::cleanup();
78+
#endif
79+
}
80+
4481
void PythonQtTestSlotCalling::initTestCase()
4582
{
4683
_helper = new PythonQtTestSlotCallingHelper(this);

tests/PythonQtTests.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ class PythonQtTestSlotCallingHelper;
5858
class PythonQtTestApiHelper;
5959
class QWidget;
6060

61+
class PythonQtMemoryTests : public QObject
62+
{
63+
Q_OBJECT
64+
65+
private Q_SLOTS:
66+
void testBaseCleanup();
67+
void testCleanupWithFlags();
68+
void testSeveralCleanup();
69+
void testInitWithPreconfig();
70+
void testInitAlreadyInitialized();
71+
};
72+
6173
//! test the PythonQt api
6274
class PythonQtTestApi : public QObject
6375
{

0 commit comments

Comments
 (0)