Added a way to check if a signal has listeners

This commit is contained in:
Anssi Kääriäinen 2012-09-20 03:18:19 +03:00
parent 07ffe78143
commit 3fcca0e947
2 changed files with 14 additions and 0 deletions

View File

@ -141,6 +141,9 @@ class Signal(object):
del self.receivers[index]
break
def has_listeners(self, sender=None):
return bool(self._live_receivers(_make_id(sender)))
def send(self, sender, **named):
"""
Send signal from sender to all connected receivers.

View File

@ -126,6 +126,17 @@ class DispatcherTests(unittest.TestCase):
a_signal.disconnect(receiver_3)
self._testIsClean(a_signal)
def test_has_listeners(self):
self.assertIs(a_signal.has_listeners(), False)
self.assertIs(a_signal.has_listeners(sender=object()), False)
receiver_1 = Callable()
a_signal.connect(receiver_1)
self.assertIs(a_signal.has_listeners(), True)
self.assertIs(a_signal.has_listeners(sender=object()), True)
a_signal.disconnect(receiver_1)
self.assertIs(a_signal.has_listeners(), False)
self.assertIs(a_signal.has_listeners(sender=object()), False)
class ReceiverTestCase(unittest.TestCase):
"""