Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added a way to check if a signal has listeners

  • Loading branch information...
commit 3fcca0e94721a734882389ce473f522d293907e9 1 parent 07ffe78
@akaariai akaariai authored
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):
"""
Please sign in to comment.
Something went wrong with that request. Please try again.