Skip to content

Commit 5c9fc59

Browse files
committed
Merge pull request #115 from The-Compiler/signal-args
Add an 'args' attribute to SignalBlocker.
2 parents f58fd30 + 30eb8a5 commit 5c9fc59

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

docs/signals.rst

+16
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ reached before the signal is triggered:
4343
assert_application_results(app)
4444
4545
46+
**Getting arguments of the emitted signal**
47+
48+
.. versionadded:: 1.10
49+
50+
The arguments emitted with the signal are available as the ``args`` attribute
51+
of the blocker:
52+
53+
54+
.. code-block:: python
55+
56+
def test_signal(qtbot):
57+
...
58+
with qtbot.waitSignal(app.got_cmd) as blocker:
59+
app.listen()
60+
assert blocker.args == ['test']
61+
4662
4763
**waitSignals**
4864

pytestqt/wait_signal.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,21 @@ class SignalBlocker(_AbstractSignalBlocker):
8686
:ivar bool raising:
8787
If :class:`SignalTimeoutError` should be raised if a timeout occurred.
8888
89+
:ivar list args:
90+
The arguments which were emitted by the signal, or None if the signal
91+
wasn't emitted at all.
92+
93+
.. versionadded:: 1.10
94+
The *args* attribute.
95+
8996
.. automethod:: wait
9097
.. automethod:: connect
9198
"""
9299

93100
def __init__(self, timeout=1000, raising=False):
94101
super(SignalBlocker, self).__init__(timeout, raising=raising)
95102
self._signals = []
103+
self.args = None
96104

97105
def connect(self, signal):
98106
"""
@@ -107,11 +115,12 @@ def connect(self, signal):
107115
signal.connect(self._quit_loop_by_signal)
108116
self._signals.append(signal)
109117

110-
def _quit_loop_by_signal(self):
118+
def _quit_loop_by_signal(self, *args):
111119
"""
112120
quits the event loop and marks that we finished because of a signal.
113121
"""
114122
self.signal_triggered = True
123+
self.args = list(args)
115124
self._loop.quit()
116125
self._cleanup()
117126

tests/test_wait_signal.py

+38
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ def signaller(timer):
168168
class Signaller(QtCore.QObject):
169169
signal = Signal()
170170
signal_2 = Signal()
171+
signal_args = Signal(str, int)
172+
signal_args_2 = Signal(str, int)
171173

172174
assert timer
173175

@@ -274,3 +276,39 @@ class Obj(QtCore.QObject):
274276

275277
assert sip.isdeleted(obj)
276278

279+
280+
class TestArgs:
281+
282+
"""Try to get the signal arguments from the signal blocker."""
283+
284+
def test_simple(self, qtbot, signaller):
285+
"""The blocker should store the signal args in an 'args' attribute."""
286+
with qtbot.waitSignal(signaller.signal_args) as blocker:
287+
signaller.signal_args.emit('test', 123)
288+
assert blocker.args == ['test', 123]
289+
290+
def test_timeout(self, qtbot):
291+
"""If there's a timeout, the args attribute is None."""
292+
with qtbot.waitSignal(timeout=100) as blocker:
293+
pass
294+
assert blocker.args is None
295+
296+
def test_without_args(self, qtbot, signaller):
297+
"""If a signal has no args, the args attribute is an empty list."""
298+
with qtbot.waitSignal(signaller.signal) as blocker:
299+
signaller.signal.emit()
300+
assert blocker.args == []
301+
302+
def test_multi(self, qtbot, signaller):
303+
"""A MultiSignalBlocker doesn't have an args attribute."""
304+
with qtbot.waitSignals([signaller.signal]) as blocker:
305+
signaller.signal.emit()
306+
with pytest.raises(AttributeError):
307+
blocker.args
308+
309+
def test_connected_signal(self, qtbot, signaller):
310+
"""A second signal connected via .connect also works."""
311+
with qtbot.waitSignal(signaller.signal_args) as blocker:
312+
blocker.connect(signaller.signal_args_2)
313+
signaller.signal_args_2.emit('foo', 2342)
314+
assert blocker.args == ['foo', 2342]

0 commit comments

Comments
 (0)