Skip to content

Commit 6be662d

Browse files
committed
prevent segfault if there is nothing to be read from device
Instead of segfaulting, _input.read() will return an empty tuple if the device has nothing to offer.
1 parent 2d55a37 commit 6be662d

3 files changed

Lines changed: 6 additions & 6 deletions

File tree

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ Changelog
3232
Development
3333
^^^^^^^^^^^
3434

35+
Fixes:
36+
- ``device.read()`` will return an empty tuple if the device has
37+
nothing to offer (instead of segfaulting).
3538

3639
0.3.0 (Nov 06, 2012)
3740
^^^^^^^^^^^^^^^^^^^^

evdev/device.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,6 @@ def read(self):
232232
Read multiple input events from device. This function returns a
233233
generator object that yields :class:`InputEvent
234234
<evdev.events.InputEvent>` instances.
235-
236-
.. warning:: Attempting to read from an input device that has no
237-
available data will result in a segmentation fault. Consider
238-
wrapping calls to ``read()`` in a ``select()``.
239235
'''
240236

241237
# events -> [(sec, usec, type, code, val), ...]

evdev/input.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ device_read_many(PyObject *self, PyObject *args)
8888
struct input_event event[64];
8989

9090
size_t event_size = sizeof(struct input_event);
91-
size_t nread;
91+
size_t nread = read(fd, event, event_size*64);
9292

93-
nread = read(fd, event, event_size*64);
93+
if (nread == -1)
94+
return PyTuple_New(0);
9495

9596
// Construct a list of event tuples, which we'll make sense of in Python
9697
for (i = 0 ; i < nread/event_size ; i++) {

0 commit comments

Comments
 (0)