Browse files

Added a way to check if a signal has listeners

  • Loading branch information...
1 parent 07ffe78 commit 3fcca0e94721a734882389ce473f522d293907e9 @akaariai akaariai committed Sep 20, 2012
Showing with 14 additions and 0 deletions.
  1. +3 −0 django/dispatch/dispatcher.py
  2. +11 −0 tests/regressiontests/dispatch/tests/test_dispatcher.py
View
3 django/dispatch/dispatcher.py
@@ -141,6 +141,9 @@ def disconnect(self, receiver=None, sender=None, weak=True, dispatch_uid=None):
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
11 tests/regressiontests/dispatch/tests/test_dispatcher.py
@@ -126,6 +126,17 @@ def testDisconnection(self):
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):
"""

0 comments on commit 3fcca0e

Please sign in to comment.