Skip to content

Commit 178f867

Browse files
msmolensjcfrpatmarion
committed
Add cleanup/finalization tests
Add tests that check for clean cleanup and finalization in different scenarios. (cherry picked from commit commontk/PythonQt@4ad7e74) Co-authored-by: Jean-Christophe Fillion-Robin <[email protected]> Co-authored-by: Pat Marion <[email protected]>
1 parent 7d7e038 commit 178f867

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

tests/PythonQtTestCleanup.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "PythonQtTestCleanup.h"
2+
#include "PythonQt.h"
3+
#include "PythonQt_QtBindings.h"
4+
5+
void PythonQtTestCleanup::initTestCase()
6+
{
7+
}
8+
9+
void PythonQtTestCleanup::cleanupTestCase()
10+
{
11+
}
12+
13+
void PythonQtTestCleanup::init()
14+
{
15+
// Initialize before each test
16+
17+
PythonQt::init(PythonQt::IgnoreSiteModule);
18+
PythonQt_init_QtBindings();
19+
20+
_helper = new PythonQtTestCleanupHelper();
21+
PythonQtObjectPtr main = PythonQt::self()->getMainModule();
22+
PythonQt::self()->addObject(main, "obj", _helper);
23+
}
24+
25+
void PythonQtTestCleanup::cleanup()
26+
{
27+
// Finalize and cleanup after each test
28+
29+
PythonQtObjectPtr main = PythonQt::self()->getMainModule();
30+
PythonQt::self()->removeVariable(main, "obj");
31+
delete _helper;
32+
_helper = NULL;
33+
34+
if (Py_IsInitialized()) {
35+
Py_Finalize();
36+
}
37+
38+
PythonQt::cleanup();
39+
}
40+
41+
void PythonQtTestCleanup::testQtEnum()
42+
{
43+
QVERIFY(_helper->runScript(
44+
"import PythonQt.QtCore\n" \
45+
"x = PythonQt.QtCore.QFile.ReadOnly\n" \
46+
"obj.setPassed()"
47+
));
48+
}
49+
50+
void PythonQtTestCleanup::testCallQtMethodInDel()
51+
{
52+
QVERIFY(_helper->runScript(
53+
"import PythonQt.QtCore\n" \
54+
"class TimerWrapper(object):\n" \
55+
" def __init__(self):\n" \
56+
" self.timer = PythonQt.QtCore.QTimer()\n" \
57+
" def __del__(self):\n" \
58+
" self.timer.setSingleShot(True)\n" \
59+
"x = TimerWrapper()\n" \
60+
"obj.setPassed()\n"
61+
));
62+
}
63+
64+
bool PythonQtTestCleanupHelper::runScript(const char* script)
65+
{
66+
_passed = false;
67+
PyRun_SimpleString(script);
68+
return _passed;
69+
}

tests/PythonQtTestCleanup.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef _PYTHONQTTESTCLEANUP_H
2+
#define _PYTHONQTTESTCLEANUP_H
3+
4+
#include "PythonQt.h"
5+
#include <QtTest/QtTest>
6+
7+
class PythonQtTestCleanupHelper;
8+
9+
//! Test PythonQt cleanup and Python interpreter finalization
10+
class PythonQtTestCleanup : public QObject
11+
{
12+
Q_OBJECT
13+
14+
private Q_SLOTS:
15+
void initTestCase();
16+
void cleanupTestCase();
17+
void init();
18+
void cleanup();
19+
20+
void testQtEnum();
21+
void testCallQtMethodInDel();
22+
23+
private:
24+
PythonQtTestCleanupHelper* _helper;
25+
};
26+
27+
//! Test helper class
28+
class PythonQtTestCleanupHelper : public QObject
29+
{
30+
Q_OBJECT
31+
public:
32+
PythonQtTestCleanupHelper() :
33+
_passed(false) {
34+
};
35+
36+
bool runScript(const char* script);
37+
38+
public Q_SLOTS:
39+
void setPassed() { _passed = true; }
40+
41+
private:
42+
bool _passed;
43+
};
44+
45+
#endif

tests/PythonQtTestMain.cpp

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

4242
#include "PythonQt.h"
4343
#include "PythonQtTests.h"
44+
#include "PythonQtTestCleanup.h"
4445

4546
#include <QApplication>
4647

@@ -65,6 +66,13 @@ int main( int argc, char **argv )
6566

6667
PythonQt::cleanup();
6768

69+
if (Py_IsInitialized()) {
70+
Py_Finalize();
71+
}
72+
73+
PythonQtTestCleanup cleanup;
74+
failCount += QTest::qExec(&cleanup, argc, argv);
75+
6876
if (failCount) {
6977
std::cerr << "Tests failed: " << failCount << std::endl;
7078
} else {

0 commit comments

Comments
 (0)