[PATCH evemu 08/19] py: Add _callstr() - returns str object

Daniel Martin consume.noise at gmail.com
Mon Jan 6 09:38:08 PST 2014


Python 3 handles bytes objects differently. Therefore the result check
in _call() raises a "TypeError: unorderable types: bytes() < int()" when
the called library function returns a bytes object with len() > 1.

Add an additional _callstr() function, which converts such bytes objects
to str on its way, to fix this error.

v2: Changed the commit message.

Signed-off-by: Daniel Martin <consume.noise at gmail.com>
---
 python/evemu/__init__.py | 2 +-
 python/evemu/base.py     | 7 +++++++
 python/evemu/const.py    | 1 +
 3 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/python/evemu/__init__.py b/python/evemu/__init__.py
index 21cd22f..20d82b5 100644
--- a/python/evemu/__init__.py
+++ b/python/evemu/__init__.py
@@ -199,7 +199,7 @@ class Device(object):
         """
         func = self._evemu.get_lib().evemu_get_name
         func.restype = ctypes.c_char_p
-        return self._evemu._call(func, self._evemu_device)
+        return self._evemu._callstr(func, self._evemu_device)
 
     @property
     def id_bustype(self):
diff --git a/python/evemu/base.py b/python/evemu/base.py
index 0f2fa97..66ea36a 100644
--- a/python/evemu/base.py
+++ b/python/evemu/base.py
@@ -28,6 +28,13 @@ class EvEmuBase(object):
                 api_call.__name__, self.get_c_error()))
         return result
 
+    def _callstr(self, api_call, *parameters):
+        result = api_call(*parameters)
+        if result == None and self.get_c_errno() != 0:
+            raise exception.ExecutionError("%s: %s" % (
+                api_call.__name__, self.get_c_error()))
+        return result.decode(const.ENCODING)
+
     def get_c_errno(self):
         return ctypes.get_errno()
 
diff --git a/python/evemu/const.py b/python/evemu/const.py
index bd1bcde..257963e 100644
--- a/python/evemu/const.py
+++ b/python/evemu/const.py
@@ -1,3 +1,4 @@
+ENCODING = "iso8859-1"
 LIB = "libevemu.so"
 UINPUT_NODE = "/dev/uinput"
 MAX_EVENT_NODE = 32
-- 
1.8.5.2



More information about the Input-tools mailing list