Skip to content

Commit 388ce6f

Browse files
committed
Add exception handling to functions returning futures
1 parent 0253875 commit 388ce6f

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎docs/changelog.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Unreleased
77

88
- Support path protocol in ``InputDevice``.
99

10+
- Exceptions raised during ``InputDevice.async_read()`` (and similar) are now
11+
handled properly (i.e. an exception is set on the returned future instead of
12+
leaking that exception into the event loop) (Fixes `#67`_).
13+
1014

1115
0.6.4 (Oct 07, 2016)
1216
====================
@@ -269,3 +273,4 @@ Unreleased
269273

270274
.. _issue21121: http://bugs.python.org/issue21121
271275
.. _`#63`: https://github.com/gvalkov/python-evdev/issues/63
276+
.. _`#63`: https://github.com/gvalkov/python-evdev/issues/67

‎evdev/eventio_async.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ def ready():
1414
callback()
1515
loop.add_reader(self.fileno(), ready)
1616

17+
def _set_result(self, future, cb):
18+
try:
19+
future.set_result(cb())
20+
except Exception as error:
21+
future.set_exception(error)
22+
1723
def async_read_one(self):
1824
'''
1925
Asyncio coroutine to read and return a single input event as
2026
an instance of :class:`InputEvent <evdev.events.InputEvent>`.
2127
'''
2228
future = asyncio.Future()
23-
self._do_when_readable(lambda: future.set_result(self.read_one()))
29+
self._do_when_readable(lambda: self._set_result(future, self.read_one))
2430
return future
2531

2632
def async_read(self):
@@ -30,7 +36,7 @@ def async_read(self):
3036
instances.
3137
'''
3238
future = asyncio.Future()
33-
self._do_when_readable(lambda: future.set_result(self.read()))
39+
self._do_when_readable(lambda: self._set_result(future, self.read))
3440
return future
3541

3642
def async_read_loop(self):

0 commit comments

Comments
 (0)