Repository navigation
Expand file tree
/
Copy pathPythonQtTestCleanup.cpp
More file actions
225 lines (187 loc) · 7.88 KB
/
Copy pathPythonQtTestCleanup.cpp
File metadata and controls
225 lines (187 loc) · 7.88 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "PythonQtTestCleanup.h"
#include "PythonQt.h"
#include "PythonQt_QtAll.h"
#include <QTimer>
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()
{
QVERIFY(_helper->runScript("import PythonQt.QtCore\n"
"timer = PythonQt.QtCore.QTimer(obj)\n"
"timer.connect('destroyed()', obj.onDestroyed)\n"
"obj.setPassed()\n"));
if (Py_IsInitialized()) {
Py_Finalize();
}
PythonQt::cleanup();
}
void PythonQtTestCleanup::testShellInstanceSignalReceiverAfterPyFinalize()
{
// Python subclass of a wrapped class (C++ shell instance) owned by C++ (it has a parent).
// The signal receiver becomes a child of the shell instance (the "self" of the slot).
QVERIFY(_helper->runScript("import PythonQt.QtCore\n"
"class MyTimer(PythonQt.QtCore.QTimer):\n"
" def onTimeout(self):\n"
" pass\n"
"timer = MyTimer(obj)\n"
"timer.connect('timeout()', timer.onTimeout)\n"
"obj.setPassed()\n"));
if (Py_IsInitialized()) {
Py_Finalize();
}
// Deleting the signal receiver sends a QChildEvent to the shell instance,
// which must not call into the finalized interpreter.
PythonQt::cleanup();
}
void PythonQtTestCleanup::testDeleteChildOfShellInstanceAfterPyFinalize()
{
QVERIFY(_helper->runScript("import PythonQt.QtCore\n"
"class MyTimer(PythonQt.QtCore.QTimer):\n"
" pass\n"
"timer = MyTimer(obj)\n"
"obj.setPassed()\n"));
QTimer* shellInstance = _helper->findChild<QTimer*>();
QVERIFY(shellInstance);
QObject* cppChild = new QObject(shellInstance);
if (Py_IsInitialized()) {
Py_Finalize();
}
// Deleting the child sends a QChildEvent to the shell instance,
// which must not call into the finalized interpreter.
delete cppChild;
PythonQt::cleanup();
}
void PythonQtTestCleanup::testCppConnectionToPythonSlotAfterPyFinalize()
{
QVERIFY(_helper->runScript("import PythonQt.QtCore\n"
"class MyTimer(PythonQt.QtCore.QTimer):\n"
" @PythonQt.QtCore.Slot()\n"
" def onTriggered(self):\n"
" pass\n"
"timer = MyTimer(obj)\n"
"obj.setPassed()\n"));
QTimer* shellInstance = _helper->findChild<QTimer*>();
QVERIFY(shellInstance);
QVERIFY(QObject::connect(_helper, SIGNAL(triggered()), shellInstance, SLOT(onTriggered())));
if (Py_IsInitialized()) {
Py_Finalize();
}
// The shell instance's qt_metacall() must not call the Python slot in the finalized interpreter
QVERIFY(_helper->emitTriggered());
PythonQt::cleanup();
}
void PythonQtTestCleanup::testShellInstanceAfterPythonReinitialization()
{
// Event type that the C++ base implementation does not handle (QObject::event() returns false)
// and that has no QEvent subclass (PythonQt would cast the event to that subclass when passing it to Python)
QEvent testEvent(QEvent::Leave);
QTimer plainTimer;
QVERIFY(!QCoreApplication::sendEvent(&plainTimer, &testEvent));
// Python subclass of a wrapped class (C++ shell instance) owned by C++ (it has a parent),
// overriding a virtual method
QVERIFY(_helper->runScript("import PythonQt.QtCore\n"
"class MyTimer(PythonQt.QtCore.QTimer):\n"
" def event(self, event):\n"
" return True\n"
"timer = MyTimer(obj)\n"
"obj.setPassed()\n"));
QTimer* shellInstance = _helper->findChild<QTimer*>();
QVERIFY(shellInstance);
// The Python override is called
QVERIFY(QCoreApplication::sendEvent(shellInstance, &testEvent));
if (Py_IsInitialized()) {
Py_Finalize();
}
PythonQt::cleanup();
// Start a new interpreter while the shell instance created in the previous one is still alive
PythonQt::init(PythonQt::IgnoreSiteModule);
PythonQt_QtAll::init();
// The shell instance must not call the Python override from the previous interpreter,
// but fall back to the C++ base implementation
QVERIFY(!QCoreApplication::sendEvent(shellInstance, &testEvent));
}
void PythonQtTestCleanup::testShellInstanceDeletedBeforePyFinalize()
{
// Python subclass of a wrapped class (C++ shell instance) owned by C++ (it has a parent)
QVERIFY(_helper->runScript("import PythonQt.QtCore\n"
"class MyTimer(PythonQt.QtCore.QTimer):\n"
" pass\n"
"timer = MyTimer(obj)\n"
"obj.setPassed()\n"));
QTimer* shellInstance = _helper->findChild<QTimer*>();
QVERIFY(shellInstance);
// Delete the C++ object while its Python wrapper ("timer") is still alive
delete shellInstance;
// Detaching shell instances from their wrappers at exit must not access the deleted object
// (the error is detected reliably only with a debug heap or address sanitizer)
if (Py_IsInitialized()) {
Py_Finalize();
}
PythonQt::cleanup();
}
bool PythonQtTestCleanupHelper::runScript(const char* script)
{
_passed = false;
PyRun_SimpleString(script);
return _passed;
}