[PATCH evemu] python: check for fileno(), not read() and document it

Peter Hutterer peter.hutterer at who-t.net
Thu Nov 6 16:27:21 PST 2014


We use fileno() on the various file arguments, not read() so check for that.
And document it in the API that we need real file objects here, not just
something that's file-like.

Reported-by: Dan Callaghan
Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 python/evemu/__init__.py | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/python/evemu/__init__.py b/python/evemu/__init__.py
index 531ef81..78cf42f 100644
--- a/python/evemu/__init__.py
+++ b/python/evemu/__init__.py
@@ -255,8 +255,10 @@ class Device(object):
 
         You need the required permissions to access the device file to
         succeed (usually root).
+
+        prop_file must be a real file with fileno(), not file-like.
         """
-        if not hasattr(prop_file, "read"):
+        if not hasattr(prop_file, "fileno"):
             raise TypeError("expected file")
 
         fs = self._libc.fdopen(prop_file.fileno(), b"w")
@@ -267,9 +269,12 @@ class Device(object):
         """
         Reads the events from the given file and returns them as a list of
         dicts.
+
+        If not None, events_file must be a real file with fileno(), not
+        file-like. If None, the file used for creating this device is used.
         """
         if events_file:
-            if not hasattr(events_file, "read"):
+            if not hasattr(events_file, "fileno"):
                 raise TypeError("expected file")
         else:
             events_file = self._file
@@ -287,8 +292,10 @@ class Device(object):
 
         You need the required permissions to access the device file to
         succeed (usually root).
+
+        events_file must be a real file with fileno(), not file-like.
         """
-        if not hasattr(events_file, "read"):
+        if not hasattr(events_file, "fileno"):
             raise TypeError("expected file")
 
         fs = self._libc.fdopen(events_file.fileno(), b"r")
@@ -303,8 +310,10 @@ class Device(object):
 
         You need the required permissions to access the device file to
         succeed (usually root).
+
+        events_file must be a real file with fileno(), not file-like.
         """
-        if not hasattr(events_file, "read"):
+        if not hasattr(events_file, "fileno"):
             raise TypeError("expected file")
 
         fs = self._libc.fdopen(events_file.fileno(), b"w")
-- 
2.1.0



More information about the Input-tools mailing list