[PATCH evemu 07/19] py: Fix file object detection
Daniel Martin
consume.noise at gmail.com
Mon Jan 6 09:38:07 PST 2014
Python 3 uses different classes for opened text and binary files.
Therefore a test on __name__ would need to cover more (i.e.
BufferedReader and TextIOWrapper). But, we just want to read from the
file objects and a test for the attribute "read" is sufficient and even
more generic.
Additionally, just check if the type of an object is str and not if its
__name__ is "str".
v2: Fix a typo in "Therefore".
Signed-off-by: Daniel Martin <consume.noise at gmail.com>
---
python/evemu/__init__.py | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/python/evemu/__init__.py b/python/evemu/__init__.py
index 3a1a8d4..21cd22f 100644
--- a/python/evemu/__init__.py
+++ b/python/evemu/__init__.py
@@ -46,9 +46,9 @@ class Device(object):
to create a pseudo-device node.
"""
- if type(f).__name__ == 'str':
+ if type(f) == str:
self._file = open(f)
- elif type(f).__name__ == 'file':
+ elif hasattr(f, "read"):
self._file = f
else:
raise TypeError("expected file or file name")
@@ -127,7 +127,7 @@ class Device(object):
Scripts that use this method need to be run as root.
"""
- if type(prop_file).__name__ != 'file':
+ if not hasattr(prop_file, "read"):
raise TypeError("expected file")
fs = self._evemu._call0(self._evemu.get_c_lib().fdopen,
@@ -146,7 +146,7 @@ class Device(object):
Scripts that use this method need to be run as root.
"""
- if type(events_file).__name__ != 'file':
+ if not hasattr(events_file, "read"):
raise TypeError("expected file")
fs = self._evemu._call0(self._evemu.get_c_lib().fdopen,
@@ -165,7 +165,7 @@ class Device(object):
Scripts that use this method need to be run as root.
"""
- if type(events_file).__name__ != 'file':
+ if not hasattr(events_file, "read"):
raise TypeError("expected file")
fs = self._evemu._call0(self._evemu.get_c_lib().fdopen,
--
1.8.5.2
More information about the Input-tools
mailing list