Skip to content

Commit

Permalink
Allow Registering and unregistering instance methods as listeners (#102)
Browse files Browse the repository at this point in the history
* Fixes #12 - registering instance methods as listeners doesn’t allow to easily unregister them as the registry tries to find the handler with 'is' but it should use '==' to allow the python BoundMethod wrapper (which is a new instance every time instance.$methodname is accessed).
  • Loading branch information
dwt authored and Michael Howitz committed Nov 28, 2017
1 parent d9c5d5e commit d50e3d4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Expand Up @@ -4,7 +4,9 @@ Changes
4.4.4 (unreleased)
------------------

- Nothing changed yet.
- Allow registering and unregistering instance methods as listeners.
See `issue 12 <https://github.com/zopefoundation/zope.interface/issues/12>`_
and `PR 102 <https://github.com/zopefoundation/zope.interface/pull/102>`_.


4.4.3 (2017-09-22)
Expand Down
2 changes: 1 addition & 1 deletion src/zope/interface/adapter.py
Expand Up @@ -257,7 +257,7 @@ def unsubscribe(self, required, provided, value=None):
if value is None:
new = ()
else:
new = tuple([v for v in old if v is not value])
new = tuple([v for v in old if v != value])

if new == old:
return
Expand Down
11 changes: 11 additions & 0 deletions src/zope/interface/tests/test_adapter.py
Expand Up @@ -234,6 +234,17 @@ def test_unsubscribe_with_value_not_None_miss(self):
registry.subscribe([IB1], None, orig)
registry.unsubscribe([IB1], None, nomatch) #doesn't raise
self.assertEqual(len(registry._subscribers), 2)

def _instance_method_notify_target(self):
self.fail("Example method, not intended to be called.")

def test_unsubscribe_instance_method(self):
IB0, IB1, IB2, IB3, IB4, IF0, IF1, IR0, IR1 = _makeInterfaces()
registry = self._makeOne()
self.assertEqual(len(registry._subscribers), 0)
registry.subscribe([IB1], None, self._instance_method_notify_target)
registry.unsubscribe([IB1], None, self._instance_method_notify_target)
self.assertEqual(len(registry._subscribers), 0)


class LookupBaseFallbackTests(unittest.TestCase):
Expand Down

0 comments on commit d50e3d4

Please sign in to comment.