Skip to content

Commit 81b317c

Browse files
committed
Refactor signal disconnecting code
1 parent 86e41e3 commit 81b317c

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

pytestqt/wait_signal.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ def _quit_loop_by_timeout(self):
5555

5656
def _cleanup(self):
5757
if self._timer is not None:
58-
try:
59-
self._timer.timeout.disconnect(self._quit_loop_by_timeout)
60-
except (TypeError, RuntimeError):
61-
# already disconnected by Qt?
62-
pass
58+
_silent_disconnect(self._timer.timeout, self._quit_loop_by_timeout)
6359
self._timer.stop()
6460
self._timer = None
6561

@@ -131,11 +127,7 @@ def _quit_loop_by_signal(self, *args):
131127
def _cleanup(self):
132128
super(SignalBlocker, self)._cleanup()
133129
for signal in self._signals:
134-
try:
135-
signal.disconnect(self._quit_loop_by_signal)
136-
except (TypeError, RuntimeError): # pragma: no cover
137-
# already disconnected by Qt?
138-
pass
130+
_silent_disconnect(signal, self._quit_loop_by_signal)
139131
self._signals = []
140132

141133

@@ -188,11 +180,7 @@ def _signal_emitted(self, signal):
188180
def _cleanup(self):
189181
super(MultiSignalBlocker, self)._cleanup()
190182
for signal, slot in self._slots.items():
191-
try:
192-
signal.disconnect(slot)
193-
except (TypeError, RuntimeError): # pragma: no cover
194-
# already disconnected by Qt?
195-
pass
183+
_silent_disconnect(signal, slot)
196184
self._signals.clear()
197185
self._slots.clear()
198186

@@ -206,3 +194,12 @@ class SignalTimeoutError(Exception):
206194
"""
207195
pass
208196

197+
198+
def _silent_disconnect(signal, slot):
199+
"""Disconnects a signal from a slot, ignoring errors. Sometimes
200+
Qt might disconnect a signal automatically for unknown reasons.
201+
"""
202+
try:
203+
signal.disconnect(slot)
204+
except (TypeError, RuntimeError): # pragma: no cover
205+
pass

0 commit comments

Comments
 (0)