@@ -319,25 +319,29 @@ def waitSignals(self, signals=None, timeout=1000):
319
319
wait_signals = waitSignals # pep-8 alias
320
320
321
321
322
- class SignalBlocker (object ):
322
+ class AbstractSignalBlocker (object ):
323
323
324
324
"""
325
- Returned by :meth:`QtBot.waitSignal` method .
325
+ Base class for :class:`SignalBlocker` and :class:`MultiSignalBlocker` .
326
326
327
- .. automethod:: wait
328
- .. automethod:: connect
327
+ Provides :meth:`wait` and a context manager protocol, but no means to add
328
+ new signals and to detect when the signals should be considered "done".
329
+ This needs to be implemented by subclasses.
330
+
331
+ Subclasses also need to provide ``self._signals`` which should evaluate to
332
+ ``False`` if no signals were configured.
329
333
330
334
:ivar int timeout: maximum time to wait for a signal to be triggered. Can
331
335
be changed before :meth:`wait` is called.
332
336
333
- :ivar bool signal_triggered: set to ``True`` if a signal was triggered, or
334
- ``False`` if timeout was reached instead. Until :meth:`wait` is called,
335
- this is set to ``None``.
337
+ :ivar bool signal_triggered: set to ``True`` if a signal (or all signals in
338
+ case of :class:MultipleSignalBlocker:) was triggered, or ``False`` if
339
+ timeout was reached instead. Until :meth:`wait` is called, this is set
340
+ to ``None``.
336
341
"""
337
342
338
343
def __init__ (self , timeout = 1000 ):
339
344
self ._loop = QtCore .QEventLoop ()
340
- self ._signals = []
341
345
self .timeout = timeout
342
346
self .signal_triggered = False
343
347
@@ -350,12 +354,33 @@ def wait(self):
350
354
"""
351
355
if self .signal_triggered :
352
356
return
353
- if self .timeout is None and len ( self ._signals ) == 0 :
357
+ if self .timeout is None and not self ._signals :
354
358
raise ValueError ("No signals or timeout specified." )
355
359
if self .timeout is not None :
356
360
QtCore .QTimer .singleShot (self .timeout , self ._loop .quit )
357
361
self ._loop .exec_ ()
358
362
363
+ def __enter__ (self ):
364
+ return self
365
+
366
+ def __exit__ (self , type , value , traceback ):
367
+ self .wait ()
368
+
369
+
370
+ class SignalBlocker (AbstractSignalBlocker ):
371
+
372
+ """
373
+ Returned by :meth:`QtBot.waitSignal` method.
374
+
375
+ .. automethod:: wait
376
+ .. automethod:: connect
377
+
378
+ """
379
+
380
+ def __init__ (self , timeout = 1000 ):
381
+ super (SignalBlocker , self ).__init__ (timeout )
382
+ self ._signals = []
383
+
359
384
def connect (self , signal ):
360
385
"""
361
386
Connects to the given signal, making :meth:`wait()` return once this signal
@@ -373,49 +398,19 @@ def _quit_loop_by_signal(self):
373
398
self .signal_triggered = True
374
399
self ._loop .quit ()
375
400
376
- def __enter__ (self ):
377
- return self
378
401
379
- def __exit__ (self , type , value , traceback ):
380
- self .wait ()
381
-
382
-
383
- class MultiSignalBlocker (object ):
402
+ class MultiSignalBlocker (AbstractSignalBlocker ):
384
403
385
404
"""
386
405
Returned by :meth:`QtBot.waitSignals` method.
387
406
388
407
.. automethod:: wait
389
408
.. automethod:: add_signal
390
-
391
- :ivar int timeout: maximum time to wait for a signal to be triggered. Can
392
- be changed before :meth:`wait` is called.
393
-
394
- :ivar bool signals_triggered: set to ``True`` if all signals were
395
- triggered, or ``False`` if timeout was reached instead. Until
396
- :meth:`wait` is called, this is set to ``None``.
397
409
"""
398
410
399
411
def __init__ (self , timeout = 1000 ):
400
- self . _loop = QtCore . QEventLoop ( )
412
+ super ( MultiSignalBlocker , self ). __init__ ( timeout )
401
413
self ._signals = {}
402
- self .timeout = timeout
403
- self .signals_triggered = False
404
-
405
- def wait (self ):
406
- """
407
- Waits until either a connected signal is triggered or timeout is reached.
408
-
409
- :raise ValueError: if no signals are connected and timeout is None; in
410
- this case it would wait forever.
411
- """
412
- if self .signals_triggered :
413
- return
414
- if self .timeout is None and not self ._signals :
415
- raise ValueError ("No signals or timeout specified." )
416
- if self .timeout is not None :
417
- QtCore .QTimer .singleShot (self .timeout , self ._loop .quit )
418
- self ._loop .exec_ ()
419
414
420
415
def add_signal (self , signal ):
421
416
"""
@@ -436,15 +431,9 @@ def _signal_emitted(self, signal):
436
431
"""
437
432
self ._signals [signal ] = True
438
433
if all (self ._signals .values ()):
439
- self .signals_triggered = True
434
+ self .signal_triggered = True
440
435
self ._loop .quit ()
441
436
442
- def __enter__ (self ):
443
- return self
444
-
445
- def __exit__ (self , type , value , traceback ):
446
- self .wait ()
447
-
448
437
449
438
@contextmanager
450
439
def capture_exceptions ():
0 commit comments