def test_different_instance_callbacks(self): class A(object): def b(self): pass def __eq__(self, other): return True b = A() c = A() self.assertFalse(reflection.is_same_callback(b.b, c.b)) self.assertTrue(reflection.is_same_callback(b.b, c.b, strict=False))
def is_registered(self, event_type, callback): """Check if a callback is registered.""" listeners = list(self._listeners.get(event_type, [])) for (cb, _args, _kwargs) in listeners: if reflection.is_same_callback(cb, callback): return True return False
def deregister(self, state, callback): if state not in self._listeners: return for i, (cb, args, kwargs) in enumerate(self._listeners[state]): if reflection.is_same_callback(cb, callback): self._listeners[state].pop(i) break
def deregister(self, event_type, callback): """Remove a single callback from listening to event ``event_type``.""" if event_type not in self._listeners: return for i, (cb, args, kwargs) in enumerate(self._listeners[event_type]): if reflection.is_same_callback(cb, callback): self._listeners[event_type].pop(i) break
def test_different_simple_callbacks(self): def a(): pass def b(): pass self.assertFalse(reflection.is_same_callback(a, b))
def test_static_instance_callbacks(self): class A(object): @staticmethod def b(a, b, c): pass a = A() b = A() self.assertTrue(reflection.is_same_callback(a.b, b.b))
def unbind(self, event, handler=None): """Remove a previously-attached event handler from the task. If handler function not passed, then unbind all event handlers for the provided event. If multiple of the same handlers are bound, then the first match is removed (and only the first match). :param event: event type :param handler: handler previously bound :rtype: boolean :return: whether anything was removed """ removed_any = False if not handler: removed_any = self._events_listeners.pop(event, removed_any) else: event_listeners = self._events_listeners.get(event, []) for i, (handler2, _event_data) in enumerate(event_listeners): if reflection.is_same_callback(handler, handler2): event_listeners.pop(i) removed_any = True break return bool(removed_any)
def is_registered(self, state, callback): listeners = list(self._listeners.get(state, [])) for (cb, _args, _kwargs) in listeners: if reflection.is_same_callback(cb, callback): return True return False