[PATCH evemu 08/11] py: Add _callstr() - returns str object
Daniel Martin
consume.noise at gmail.com
Mon Jan 6 09:56:54 PST 2014
On Fri, Jan 03, 2014 at 11:11:30AM +1000, Peter Hutterer wrote:
> 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)
I've left this patch in the series. So, that the initial patches don't
change to much.
But, with the added patches the _call stuff has been replaced, which
makes it possible to just:
self._libevemu.evemu_get_name(self._evemu_device)
A callback function is registered at such calls to check for an expected
return value, i.e. for evemu_get_name the callback is expect_not_none().
The wrappers could've even been improved by adding names for the
arguments. With that it'd be possible to pass keyword arguments, i.e.:
self._libevemu.evemu_get_name(dev=self._evemu_device)
Not usefull for functions with one argument only, but nifty when having
more and by using kw args the order of arguments becomes irrelevant.
I've tested it, but didn't added it at the end as it didn't looked that
necessary to have.
> >
> > @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