Skip to content

Commit

Permalink
Fix Bug when adding a function as an event handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
benmoran56 committed Jun 10, 2022
1 parent dc9a9bf commit 43bc99d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 5 deletions.
10 changes: 10 additions & 0 deletions RELEASE_NOTES
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
esper 2.1
=========
Maintenance release

Changes
-------
- Fix bug when adding a function as an event handler.
- Add some event handler unit tests.


esper 2.0
=========
Feature release
Expand Down
9 changes: 7 additions & 2 deletions esper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import time as _time

from types import MethodType as _MethodType
from typing import Any as _Any
from typing import Iterable as _Iterable
from typing import List as _List
from typing import Optional as _Optional
from typing import Tuple as _Tuple
from typing import Type as _Type
from typing import TypeVar as _TypeVar
from weakref import ref as _ref
from weakref import WeakMethod as _WeakMethod


version = '2.0'
version = '2.1'

_C = _TypeVar('_C')
_P = _TypeVar('_P')
Expand Down Expand Up @@ -76,7 +78,10 @@ def set_handler(name: str, func) -> None:
if name not in event_registry:
event_registry[name] = set()

event_registry[name].add(_WeakMethod(func, _make_callback(name)))
if isinstance(func, _MethodType):
event_registry[name].add(_WeakMethod(func, _make_callback(name)))
else:
event_registry[name].add(_ref(func, _make_callback(name)))


def remove_handler(name, func) -> None:
Expand Down
77 changes: 74 additions & 3 deletions tests/test_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,8 @@ def test_processor_kwargs(world):
world.process(eggs="eggs", spam="spam")


# def test_clear_cache(populated_world):
# # TODO: actually test something here
# populated_world.clear_cache()
def test_clear_cache(populated_world):
populated_world.clear_cache()


def test_cache_results(world):
Expand All @@ -270,6 +269,64 @@ def test_entity_exists(world):
assert not world.entity_exists(future_entity)


def test_event_dispatch_no_handlers():
esper.dispatch_event("foo")
esper.dispatch_event("foo", 1)
esper.dispatch_event("foo", 1, 2)
esper.event_registry.clear()


def test_event_dispatch_one_arg():
esper.set_handler("foo", myhandler_onearg)
esper.dispatch_event("foo", 1)
esper.event_registry.clear()


def test_event_dispatch_two_args():
esper.set_handler("foo", myhandler_twoargs)
esper.dispatch_event("foo", 1, 2)
esper.event_registry.clear()


def test_event_dispatch_incorrect_args():
esper.set_handler("foo", myhandler_noargs)
assert pytest.raises(TypeError), esper.dispatch_event("foo")
esper.event_registry.clear()


def test_set_methoad_as_handler_in_init():

class MyClass(esper.Processor):

def __init__(self):
esper.set_handler("foo", self._my_handler)

def _my_handler(self):
print("OK")

def process(self, dt):
pass

myclass = MyClass()
esper.dispatch_event("foo")
esper.event_registry.clear()


def test_set_instance_methoad_as_handler():
class MyClass(esper.Processor):

def my_handler(self):
print("OK")

def process(self, dt):
pass

myclass = MyClass()
esper.set_handler("foo", myclass.my_handler)
esper.dispatch_event("foo")
esper.event_registry.clear()


##################################################
# Some helper functions and Component templates:
##################################################
Expand Down Expand Up @@ -360,3 +417,17 @@ class IncorrectProcessor:

def process(self):
pass


# Event handler templates:

def myhandler_noargs():
print("OK")


def myhandler_onearg(arg):
print("Arg:", arg)


def myhandler_twoargs(arg1, arg2):
print("Args:", arg1, arg2)

0 comments on commit 43bc99d

Please sign in to comment.