[PATCH evemu 2/2] python: encode/decode C strings for Python 3
Benjamin Tissoires
benjamin.tissoires at gmail.com
Wed Nov 9 15:23:31 UTC 2016
On Wed, Nov 9, 2016 at 6:03 AM, Peter Hutterer <peter.hutterer at who-t.net> wrote:
> 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:
These changes seemed to be different than the ones mentioned in the commit.
0 and None is rather different and I'd like to be sure we are not
screwing anything before applying this patch (which is why I applied
only 1/2).
> 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")
I had to run the python CLI to understand what was going on. But I am
still not sure about the end result (the commit message would need to
be extended):
When run under python2, _libevdev.libevdev_event_type_get_name()
returns a plain "str" which can be reused immediately by
_libevdev.libevdev_event_type_from_name().
But when run under python3, _libevdev.libevdev_event_type_get_name()
returns a bytes (b"SOMETHING"). But then
_libevdev.libevdev_event_type_from_name() also requires a bytes string
as argument.
So yes, I understand why there is a need to have the decode/encode
dance (to actually have the type libevdev_event_type_from_name()
expects).
But why do we need to keep around the cast to a str? It looks like the
original str() call was to convert the bytes string into a pure
string. But the call was not working (returns something like
"b'EV_KEY'"), so unless I am missing something, I'd rather drop this.
Also, how about we stick to string only (not bytes) for local
variables (by calling t =
_libevdev.libevdev_event_type_from_name(event_type.encode("iso8859-1")))?
Cheers,
Benjamin
> + 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
>
> _______________________________________________
> Input-tools mailing list
> Input-tools at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/input-tools
More information about the Input-tools
mailing list