Skip to content

Commit

Permalink
less intrusive conversion from signal to boundsignal
Browse files Browse the repository at this point in the history
  • Loading branch information
keis committed Dec 4, 2013
1 parent 6fbbe97 commit f3b2153
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
16 changes: 4 additions & 12 deletions smoke.py
Expand Up @@ -65,9 +65,6 @@ def weak_method(*args, **kwargs):
def subscribers(obj, event):
'''Get a list of all subscribers to `event` on `obj`'''

if hasattr(event, '__get__'):
event = event.__get__(obj)

if not hasattr(obj, '_subscribers'):
obj._subscribers = defaultdict(list)

Expand All @@ -77,29 +74,24 @@ def subscribers(obj, event):
def subscribe(obj, event, subscriber):
'''Add a subscriber to `event` on `obj`'''

if hasattr(event, '__get__'):
event = event.__get__(obj)

subscribers(obj, event).append(subscriber)


def disconnect(obj, event, subscriber):
'''Disconnect a subscriber to `event` on `obj`'''

if hasattr(event, '__get__'):
event = event.__get__(obj)

subscribers(obj, event).remove(subscriber)


def variants(event):
def variants(obj, event):
'''Get a generator that yields variations of a event.'''

if hasattr(event, 'parameters'):
parent = event.parent
l = len(event.parameters)
for i in range(l+1):
yield parent.parameterise(event.parameters[:l-i])[0]
sig, _ = parent.parameterise(event.parameters[:l-i])
yield sig.__get__(obj)
else:
yield event
if hasattr(event, 'name'):
Expand All @@ -121,7 +113,7 @@ def _publish(obj, _event, **kwargs):
break the publish loop without notifing remaining subscribers
'''

for var in variants(_event):
for var in variants(obj, _event):
subs = subscribers(obj, var)
disconnected = []
try:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_mixed.py
@@ -1,5 +1,6 @@
import mock
from hamcrest import assert_that, equal_to
from nose.plugins.skip import SkipTest
from smoke import signal, Broker, Disconnect, StopPropagation
from tests.matchers import called_once_with

Expand Down Expand Up @@ -39,6 +40,18 @@ def test_subscribe_broker_publish_signal_with_name(self):

assert_that(self.listener.egg_cb, called_once_with(s=sentinel))

def test_subscribe_signal_publish_boundsignal(self):
# Supporting this in a general might be a bit to intrusive as
# boundmethod and function and other things implementing the descriptor
# protocol would be consider equal as well.
raise SkipTest("not supported, for now")

sentinel = object()
self.mixed.subscribe(Mixed.spam, self.listener.spam_cb)
self.mixed.publish(self.mixed.spam, s=sentinel)

assert_that(self.listener.spam_cb, called_once_with(s=sentinel))

def test_subscribe_by_name(self):
sentinel = object()
self.mixed.subscribe('egg', self.listener.egg_cb)
Expand Down

0 comments on commit f3b2153

Please sign in to comment.