Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added a way to check if a signal has listeners

  • Loading branch information...
commit 3fcca0e94721a734882389ce473f522d293907e9 1 parent 07ffe78
Anssi Kääriäinen authored September 20, 2012
3  django/dispatch/dispatcher.py
@@ -141,6 +141,9 @@ def disconnect(self, receiver=None, sender=None, weak=True, dispatch_uid=None):
141 141
                     del self.receivers[index]
142 142
                     break
143 143
 
  144
+    def has_listeners(self, sender=None):
  145
+        return bool(self._live_receivers(_make_id(sender)))
  146
+
144 147
     def send(self, sender, **named):
145 148
         """
146 149
         Send signal from sender to all connected receivers.
11  tests/regressiontests/dispatch/tests/test_dispatcher.py
@@ -126,6 +126,17 @@ def testDisconnection(self):
126 126
         a_signal.disconnect(receiver_3)
127 127
         self._testIsClean(a_signal)
128 128
 
  129
+    def test_has_listeners(self):
  130
+        self.assertIs(a_signal.has_listeners(), False)
  131
+        self.assertIs(a_signal.has_listeners(sender=object()), False)
  132
+        receiver_1 = Callable()
  133
+        a_signal.connect(receiver_1)
  134
+        self.assertIs(a_signal.has_listeners(), True)
  135
+        self.assertIs(a_signal.has_listeners(sender=object()), True)
  136
+        a_signal.disconnect(receiver_1)
  137
+        self.assertIs(a_signal.has_listeners(), False)
  138
+        self.assertIs(a_signal.has_listeners(sender=object()), False)
  139
+
129 140
 
130 141
 class ReceiverTestCase(unittest.TestCase):
131 142
     """

0 notes on commit 3fcca0e

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