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

Peter Hutterer peter.hutterer at who-t.net
Thu Jan 2 17:11:30 PST 2014


On Thu, Jan 02, 2014 at 10:47:58PM +0100, Daniel Martin wrote:
> Python 3 handles bytes objects differently. Therefor the result check in
> _call() raises a "TypeError: unorderable types: bytes() < int()".
> 
> Add an additional _callstr() function, which converts the bytes object
> to str on its way, to fix this error.
> 
> 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)

if you have _callstr it should also handle the func.restype automatically.
having said that, the _call* interface is rather meh. python allows for a
more expressive approach (and it's internal here anyway) so maybe something
like this would be better:

def _call(self, func, return_type=int, allow_zero=True, *parameters):
        if return_type == str:
                func.restype = ctypes.c_char_p
        result = api_call(*parameters)
        if not allow_zero and result == 0 and self.get_c_errno() != 0:
                error...
        return result

which would allow us to get rid of _call0 which is not intuitive at all and
have the callers do somethin like:

      self._evemu._call(func, return_type=str, self._evemu_device)
      self._evemu._call(func, allow_zero=False, self._evemu_device)
etc.

Cheers,
   Peter


>  
>      @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
> 
> _______________________________________________
> Input-tools mailing list
> Input-tools at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/input-tools


More information about the Input-tools mailing list