diff --git a/papers/events.py b/papers/events.py index 2c34137..2c805e0 100644 --- a/papers/events.py +++ b/papers/events.py @@ -1,4 +1,4 @@ -_listener = {} +_listener = [] class Event(object): @@ -9,16 +9,14 @@ class Event(object): """ This function sends the instance of the class, i.e. the event to be sent, to all function that listen to it. """ - if self.__class__ in _listener: - for f, args in _listener[self.__class__]: + for cls, f, args in _listener: + if isinstance(self, cls): f(self, *args) @classmethod def listen(cls, *args): def wrap(f): - if cls not in _listener: - _listener[cls] = [] - _listener[cls].append((f, args)) + _listener.append((cls, f, args)) # next step allow us to call the function itself without Event raised def wrapped_f(*args): diff --git a/tests/test_events.py b/tests/test_events.py index 705925f..0339d84 100644 --- a/tests/test_events.py +++ b/tests/test_events.py @@ -23,6 +23,17 @@ class AddEvent(Event): return a + b +class Info(Event): + def __init__(self, info): + self.info = info + + +class SpecificInfo(Info): + def __init__(self, info, specific): + Info.__init__(self, info) + self.specific = specific + + @TestEvent.listen(12, 15) def display(TestEventInstance, nb1, nb2): _output.append("%s %s %s" @@ -44,6 +55,13 @@ def do_it(AddEventInstance): _output.append(AddEventInstance.add(17, 25)) +@Info.listen() +def test_info_instance(infoevent): + _output.append(infoevent.info) + if isinstance(infoevent, SpecificInfo): + _output.append(infoevent.specific) + + class TestEvents(TestCase): def setUp(self): @@ -64,3 +82,9 @@ class TestEvents(TestCase): addevent.send() correct = [42] self.assertEquals(_output, correct) + + def test_listen_Info(self): + Info('info').send() + SpecificInfo('info', 'specific').send() + correct = ['info', 'info', 'specific'] + self.assertEquals(_output, correct)