Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a way to check if a signal has listeners

  • Loading branch information...
commit 93bf3309ad4b9ee90ea953658ae444a0deea24ec 1 parent 1360bd4
@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)

You should use assertFalse and assertTrue instead of assertIs

@akaariai Owner

Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
class ReceiverTestCase(unittest.TestCase):
"""

0 comments on commit 93bf330

Please sign in to comment.
Something went wrong with that request. Please try again.