From 23f1a8dad223ee79cd206c9032c871c874f7e7af Mon Sep 17 00:00:00 2001 From: Andriy Sokolovskiy Date: Fri, 2 Jan 2015 16:25:33 +0200 Subject: [PATCH] Added return value to Signal.disconnect(). --- django/dispatch/dispatcher.py | 3 +++ docs/topics/signals.txt | 11 ++++++++--- tests/dispatch/tests.py | 10 ++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/django/dispatch/dispatcher.py b/django/dispatch/dispatcher.py index 907f7fb211..fcf80f3fac 100644 --- a/django/dispatch/dispatcher.py +++ b/django/dispatch/dispatcher.py @@ -160,14 +160,17 @@ class Signal(object): else: lookup_key = (_make_id(receiver), _make_id(sender)) + disconnected = False with self.lock: self._clear_dead_receivers() for index in range(len(self.receivers)): (r_key, _) = self.receivers[index] if r_key == lookup_key: + disconnected = True del self.receivers[index] break self.sender_receivers_cache.clear() + return disconnected def has_listeners(self, sender=None): return bool(self._live_receivers(sender)) diff --git a/docs/topics/signals.txt b/docs/topics/signals.txt index dab29ba4ec..7722379769 100644 --- a/docs/topics/signals.txt +++ b/docs/topics/signals.txt @@ -278,7 +278,12 @@ Disconnecting signals .. method:: Signal.disconnect([receiver=None, sender=None, weak=True, dispatch_uid=None]) To disconnect a receiver from a signal, call :meth:`Signal.disconnect`. The -arguments are as described in :meth:`.Signal.connect`. +arguments are as described in :meth:`.Signal.connect`. The method returns +``True`` if a receiver was disconnected and ``False`` if not. -The *receiver* argument indicates the registered receiver to disconnect. It may -be ``None`` if ``dispatch_uid`` is used to identify the receiver. +The ``receiver`` argument indicates the registered receiver to disconnect. It +may be ``None`` if ``dispatch_uid`` is used to identify the receiver. + +.. versionchanged:: 1.8 + + The boolean return value was added. diff --git a/tests/dispatch/tests.py b/tests/dispatch/tests.py index aaa2dc0daa..14aa4a66bd 100644 --- a/tests/dispatch/tests.py +++ b/tests/dispatch/tests.py @@ -153,6 +153,16 @@ class DispatcherTests(unittest.TestCase): a_signal.disconnect(receiver_3) self.assertTestIsClean(a_signal) + def test_values_returned_by_disconnection(self): + receiver_1 = Callable() + receiver_2 = Callable() + a_signal.connect(receiver_1) + receiver_1_disconnected = a_signal.disconnect(receiver_1) + receiver_2_disconnected = a_signal.disconnect(receiver_2) + self.assertTrue(receiver_1_disconnected) + self.assertFalse(receiver_2_disconnected) + self.assertTestIsClean(a_signal) + def test_has_listeners(self): self.assertFalse(a_signal.has_listeners()) self.assertFalse(a_signal.has_listeners(sender=object()))