Skip to content

Commit c463814

Browse files
authored
Also raise for get_dispatcher_event and emission_lock
1 parent 030a519 commit c463814

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

pydispatch/dispatch.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,21 @@ def get_dispatcher_event(self, name):
316316
Returns:
317317
The :class:`Event` instance for the event or property definition
318318
319+
Raises:
320+
DoesNotExistError: If no event or property with the given name exists
321+
322+
.. versionchanged:: 0.2.2
323+
:class:`DoesNotExistError` is now raised if the event or property
324+
does not exist
325+
319326
.. versionadded:: 0.1.0
320327
"""
321328
e = self.__property_events.get(name)
322329
if e is None:
323-
e = self.__events[name]
330+
try:
331+
e = self.__events[name]
332+
except KeyError:
333+
raise DoesNotExistError(name)
324334
return e
325335
def emission_lock(self, name):
326336
"""Holds emission of events and dispatches the last event on release
@@ -356,9 +366,14 @@ def emission_lock(self, name):
356366
The context manager is re-entrant, meaning that multiple calls to
357367
this method within nested context scopes are possible.
358368
369+
Raises:
370+
DoesNotExistError: If no event or property with the given name exists
371+
372+
.. versionchanged:: 0.2.2
373+
:class:`DoesNotExistError` is now raised if the event or property
374+
does not exist
375+
359376
.. _PEP 492: https://www.python.org/dev/peps/pep-0492/#asynchronous-context-managers-and-async-with
360377
"""
361-
e = self.__property_events.get(name)
362-
if e is None:
363-
e = self.__events[name]
378+
e = self.get_dispatcher_event(name)
364379
return e.emission_lock

tests/test_events.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ def callback(*args, **kwargs):
200200
sender.emit('foo')
201201
assert '"foo"' in str(excinfo.value)
202202

203+
with pytest.raises(DoesNotExistError) as excinfo:
204+
e = sender.get_dispatcher_event('foo')
205+
assert '"foo"' in str(excinfo.value)
206+
207+
with pytest.raises(DoesNotExistError) as excinfo:
208+
lock = sender.emission_lock('foo')
209+
assert '"foo"' in str(excinfo.value)
210+
203211
def test_register_existing_event():
204212
from pydispatch import Dispatcher, EventExistsError
205213

0 commit comments

Comments
 (0)