[PATCH evemu 07/11] py: Fix file object detection

Peter Hutterer peter.hutterer at who-t.net
Thu Jan 2 16:43:41 PST 2014


On Thu, Jan 02, 2014 at 10:47:57PM +0100, Daniel Martin wrote:
> Python 3 uses different classes for opened text and binary files.
> Therefor a test on __name__ would need to cover more (i.e.

typo: therefore 

> BufferedReader and TextIOWrapper). But, we just want to read from the
> file objects and a test for the attribute "read" is sufficient and even
> more generic.
> 
> Additionally, just check if the type of an object is str and not if its
> __name__ is "str".
> 
> Signed-off-by: Daniel Martin <consume.noise at gmail.com>

Reviewed-by: Peter Hutterer <peter.hutterer at who-t.net>
(and tested that type("") == str works on python 2.6 too.

Cheers,
   Peter

> ---
>  python/evemu/__init__.py | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/python/evemu/__init__.py b/python/evemu/__init__.py
> index 3a1a8d4..21cd22f 100644
> --- a/python/evemu/__init__.py
> +++ b/python/evemu/__init__.py
> @@ -46,9 +46,9 @@ class Device(object):
>          to create a pseudo-device node.
>          """
>  
> -        if type(f).__name__ == 'str':
> +        if type(f) == str:
>              self._file = open(f)
> -        elif type(f).__name__ == 'file':
> +        elif hasattr(f, "read"):
>              self._file = f
>          else:
>              raise TypeError("expected file or file name")


> @@ -127,7 +127,7 @@ class Device(object):
>  
>          Scripts that use this method need to be run as root.
>          """
> -        if type(prop_file).__name__ != 'file':
> +        if not hasattr(prop_file, "read"):
>              raise TypeError("expected file")
>  
>          fs = self._evemu._call0(self._evemu.get_c_lib().fdopen,
> @@ -146,7 +146,7 @@ class Device(object):
>  
>          Scripts that use this method need to be run as root.
>          """
> -        if type(events_file).__name__ != 'file':
> +        if not hasattr(events_file, "read"):
>              raise TypeError("expected file")
>  
>          fs = self._evemu._call0(self._evemu.get_c_lib().fdopen,
> @@ -165,7 +165,7 @@ class Device(object):
>  
>          Scripts that use this method need to be run as root.
>          """
> -        if type(events_file).__name__ != 'file':
> +        if not hasattr(events_file, "read"):
>              raise TypeError("expected file")
>  
>          fs = self._evemu._call0(self._evemu.get_c_lib().fdopen,
> -- 
> 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