[PATCH evemu 2/2] python: encode/decode C strings for Python 3

Peter Hutterer peter.hutterer at who-t.net
Wed Nov 9 05:03:18 UTC 2016


https://bugs.freedesktop.org/show_bug.cgi?id=97458

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 python/evemu/__init__.py | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/python/evemu/__init__.py b/python/evemu/__init__.py
index 230559f..323bcaf 100644
--- a/python/evemu/__init__.py
+++ b/python/evemu/__init__.py
@@ -50,20 +50,26 @@ def event_get_value(event_type, event_code = None):
 
     if isinstance(event_type, int):
         event_type = _libevdev.libevdev_event_type_get_name(event_type)
-        if event_type == 0: # NULL
+        if event_type is None:
             return None
 
-    t = _libevdev.libevdev_event_type_from_name(str(event_type))
+        event_type = event_type.decode("iso8859-1")
+
+    event_type = str(event_type).encode("iso8859-1")
+    t = _libevdev.libevdev_event_type_from_name(event_type)
 
     if event_code is None:
         return None if t < 0 else t
 
     if isinstance(event_code, int):
         event_code = _libevdev.libevdev_event_code_get_name(t, event_code)
-        if event_code == 0: # NULL
+        if event_code is None: # NULL
             return None
 
-    c = _libevdev.libevdev_event_code_from_name(t, str(event_code))
+        event_code = event_code.decode("iso8859-1")
+
+    event_code = str(event_code).encode("iso8859-1")
+    c = _libevdev.libevdev_event_code_from_name(t, event_code)
 
     return None if c < 0 else c
 
@@ -84,7 +90,11 @@ def event_get_name(event_type, event_code = None):
 
     if event_code is None:
         type_name = _libevdev.libevdev_event_type_get_name(event_type)
-        return None if type_name == 0 else type_name
+
+        if type_name is None:
+            return None
+
+        return type_name.decode("iso8859-1")
 
     if not isinstance(event_code, int):
         event_code = event_get_value(event_type, event_code)
@@ -93,8 +103,10 @@ def event_get_name(event_type, event_code = None):
         return None
 
     code_name = _libevdev.libevdev_event_code_get_name(event_type, event_code)
+    if code_name is None:
+        return None
 
-    return None if code_name == 0 else code_name
+    return code_name.decode("iso8859-1")
 
 def input_prop_get_name(prop):
     """
@@ -107,7 +119,7 @@ def input_prop_get_name(prop):
         return None
 
     prop = _libevdev.libevdev_property_get_name(prop)
-    return None if prop == 0 else prop
+    return None if prop is None else prop.decode("iso8859-1")
 
 def input_prop_get_value(prop):
     """
@@ -119,7 +131,8 @@ def input_prop_get_value(prop):
     if prop is None:
         return None
 
-    prop = _libevdev.libevdev_property_from_name(str(prop))
+    prop = str(prop).encode("iso8859-1")
+    prop = _libevdev.libevdev_property_from_name(prop)
     return None if prop < 0 else prop
 
 class InputEvent(object):
-- 
2.9.3



More information about the Input-tools mailing list