From c463814fcadf7f457fd0687fc8f5ecbfec7e53a9 Mon Sep 17 00:00:00 2001 From: nocarryr Date: Tue, 30 May 2023 17:03:54 +0000 Subject: [PATCH] Also raise for `get_dispatcher_event` and `emission_lock` --- pydispatch/dispatch.py | 23 +++++++++++++++++++---- tests/test_events.py | 8 ++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/pydispatch/dispatch.py b/pydispatch/dispatch.py index a3b6086..fce58ff 100644 --- a/pydispatch/dispatch.py +++ b/pydispatch/dispatch.py @@ -316,11 +316,21 @@ def get_dispatcher_event(self, name): Returns: The :class:`Event` instance for the event or property definition + Raises: + DoesNotExistError: If no event or property with the given name exists + + .. versionchanged:: 0.2.2 + :class:`DoesNotExistError` is now raised if the event or property + does not exist + .. versionadded:: 0.1.0 """ e = self.__property_events.get(name) if e is None: - e = self.__events[name] + try: + e = self.__events[name] + except KeyError: + raise DoesNotExistError(name) return e def emission_lock(self, name): """Holds emission of events and dispatches the last event on release @@ -356,9 +366,14 @@ def emission_lock(self, name): The context manager is re-entrant, meaning that multiple calls to this method within nested context scopes are possible. + Raises: + DoesNotExistError: If no event or property with the given name exists + + .. versionchanged:: 0.2.2 + :class:`DoesNotExistError` is now raised if the event or property + does not exist + .. _PEP 492: https://www.python.org/dev/peps/pep-0492/#asynchronous-context-managers-and-async-with """ - e = self.__property_events.get(name) - if e is None: - e = self.__events[name] + e = self.get_dispatcher_event(name) return e.emission_lock diff --git a/tests/test_events.py b/tests/test_events.py index ae2dbc8..8f78a13 100644 --- a/tests/test_events.py +++ b/tests/test_events.py @@ -200,6 +200,14 @@ def callback(*args, **kwargs): sender.emit('foo') assert '"foo"' in str(excinfo.value) + with pytest.raises(DoesNotExistError) as excinfo: + e = sender.get_dispatcher_event('foo') + assert '"foo"' in str(excinfo.value) + + with pytest.raises(DoesNotExistError) as excinfo: + lock = sender.emission_lock('foo') + assert '"foo"' in str(excinfo.value) + def test_register_existing_event(): from pydispatch import Dispatcher, EventExistsError