forked from MeVisLab/pythonqt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonQtTestCleanup.cpp
More file actions
106 lines (90 loc) · 2.43 KB
/
PythonQtTestCleanup.cpp
File metadata and controls
106 lines (90 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "PythonQtTestCleanup.h"
#include "PythonQt.h"
#include "PythonQt_QtAll.h"
void PythonQtTestCleanup::initTestCase()
{
}
void PythonQtTestCleanup::cleanupTestCase()
{
}
void PythonQtTestCleanup::init()
{
// Initialize before each test
PythonQt::init(PythonQt::IgnoreSiteModule);
PythonQt_QtAll::init();
_helper = new PythonQtTestCleanupHelper();
PythonQtObjectPtr main = PythonQt::self()->getMainModule();
PythonQt::self()->addObject(main, "obj", _helper);
}
void PythonQtTestCleanup::cleanup()
{
// Cleanup PythonQt resources before finalizing Python
PythonQt::cleanup();
if (Py_IsInitialized()) {
Py_Finalize();
}
delete _helper;
_helper = nullptr;
}
void PythonQtTestCleanup::testQtEnum()
{
QVERIFY(_helper->runScript(
"import PythonQt.QtCore\n" \
"x = PythonQt.QtCore.QFile.ReadOnly\n" \
"obj.setPassed()"
));
}
void PythonQtTestCleanup::testCallQtMethodInDestructorOwnedQTimer()
{
QVERIFY(_helper->runScript(
"import PythonQt.QtCore\n" \
"class TimerWrapper(object):\n" \
" def __init__(self):\n" \
" self.timer = PythonQt.QtCore.QTimer()\n" \
" def __del__(self):\n" \
" self.timer.setSingleShot(True)\n" \
"x = TimerWrapper()\n" \
"del x\n" \
"obj.setPassed()\n"
));
}
void PythonQtTestCleanup::testCallQtMethodInDestructorWeakRefGuarded()
{
QVERIFY(_helper->runScript(
"import weakref\n" \
"import PythonQt.QtCore\n" \
"class TimerWrapper(object):\n" \
" def __init__(self):\n" \
" self.timerWeakRef = weakref.ref(PythonQt.QtCore.QTimer())\n" \
" def __del__(self):\n" \
" if self.timerWeakRef():\n" \
" self.timerWeakRef().setSingleShot(True)\n" \
"x = TimerWrapper()\n" \
"obj.setPassed()\n"
));
}
void PythonQtTestCleanup::testSignalReceiverCleanup()
{
PythonQtObjectPtr main = PythonQt::self()->getMainModule();
// Test that PythonQtSignalReceiver is cleaned up properly,
// i.e. PythonQt::cleanup() doesn't segfault
QVERIFY(_helper->runScript(
"import PythonQt.QtCore\n" \
"timer = PythonQt.QtCore.QTimer(obj)\n" \
"timer.connect('destroyed()', obj.onDestroyed)\n" \
"obj.setPassed()\n"
));
}
void PythonQtTestCleanup::testPyFinalizeThenPythonQtCleanup()
{
if (Py_IsInitialized()) {
Py_Finalize();
}
PythonQt::cleanup();
}
bool PythonQtTestCleanupHelper::runScript(const char* script)
{
_passed = false;
PyRun_SimpleString(script);
return _passed;
}