Skip to content

Commit bffb71c

Browse files
committed
Making the references to the widgets weak references so that tests which add awidget but still delete them to check for cycles work.
1 parent af09d83 commit bffb71c

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

pytestqt/plugin.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import functools
33
import sys
44
import traceback
5+
import weakref
56

67
import pytest
78

@@ -173,7 +174,9 @@ def _close(self):
173174
Clear up method. Called at the end of each test that uses a ``qtbot`` fixture.
174175
"""
175176
for w in self._widgets:
176-
w.close()
177+
w = w()
178+
if w is not None:
179+
w.close()
177180
self._widgets[:] = []
178181

179182
def addWidget(self, widget):
@@ -184,7 +187,9 @@ def addWidget(self, widget):
184187
:param QWidget widget:
185188
Widget to keep track of.
186189
"""
187-
self._widgets.append(widget)
190+
self._widgets.append(weakref.ref(widget))
191+
192+
add_widget = addWidget # pep-8 alias
188193

189194
def waitForWindowShown(self, widget):
190195
"""
@@ -197,6 +202,8 @@ def waitForWindowShown(self, widget):
197202
"""
198203
QtTest.QTest.qWaitForWindowShown(widget)
199204

205+
wait_for_window_shown = waitForWindowShown # pep-8 alias
206+
200207
def stopForInteraction(self):
201208
"""
202209
Stops the current test flow, letting the user interact with any visible widget.
@@ -209,12 +216,15 @@ def stopForInteraction(self):
209216
210217
.. note:: As a convenience, it is also aliased as `stop`.
211218
"""
212-
widget_visibility = [widget.isVisible() for widget in self._widgets]
219+
widget_and_visibility = []
220+
for weak_widget in self._widgets:
221+
widget = weak_widget()
222+
if widget is not None:
223+
widget_and_visibility.append((widget, widget.isVisible()))
213224

214225
self._app.exec_()
215226

216-
for index, visible in enumerate(widget_visibility):
217-
widget = self._widgets[index]
227+
for widget, visible in widget_and_visibility:
218228
widget.setVisible(visible)
219229

220230
stop = stopForInteraction
@@ -256,6 +266,8 @@ def waitSignal(self, signal=None, timeout=1000):
256266
blocker.connect(signal)
257267
return blocker
258268

269+
wait_signal = waitSignal # pep-8 alias
270+
259271

260272
class SignalBlocker(object):
261273

0 commit comments

Comments
 (0)