gvalkov / python-evdev

Python bindings for the Linux input subsystem
https://python-evdev.rtfd.org/
BSD 3-Clause "New" or "Revised" License
334 stars 112 forks source link

Clear bug in uinput.py? Missing argument on os.strerror() #135

Closed tlalexander closed 4 years ago

tlalexander commented 4 years ago

I am trying to create a custom force feedback device.

Something isn't working right, and so input tries to throw an error with this line: https://github.com/gvalkov/python-evdev/blob/862c4111e6adfe316d2d1c8222507f41f9d1e75e/evdev/uinput.py#L246

Except that line seems to have a bug. Instead of throwing UInputError it throws: TypeError: strerror() takes exactly one argument (0 given)

It looks like the return code from line 245 needs to be stored. So instead of:

if self.dll._uinput_begin_upload(self.fd, ctypes.byref(upload)):
            raise UInputError('Failed to begin uinput upload: ' + os.strerror())

You might want:

ret = self.dll._uinput_begin_upload(self.fd, ctypes.byref(upload))
if ret:
            raise UInputError('Failed to begin uinput upload: ' + os.strerror(ret))

Or am I missing something? If it is an error and I can help with a pull request please let me know!

LinusCDE commented 4 years ago

Hi, great find. I personally never worked with that particular error, but it seems that you understood it better than I did when I played a bit around with it.

I would suggest that you go ahead and push the fix. Would be nice if you could also fix the other lines with os.strerror() there.

Would be interesting to know what it is that way @gvalkov. Is that something historic that worked in python at some time? My guess would be that it might've been possible in python2 to get the last error value and use it without specifying explicitly. But that doesn't seem to be the case.

Python 2.7.18 (default, Sep  5 2020, 11:17:26) 
[GCC 10.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> open('/etc/passwd', 'w')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 13] Permission denied: '/etc/passwd'
>>> os.strerror()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: strerror() takes exactly 1 argument (0 given)
tlalexander commented 4 years ago

Okay I will work on a PR. ☺️

gvalkov commented 4 years ago

Hi @LinusCDE . It seems like os.strerror() always took a non-optional argument (just checked with Python 2.4). It's just something I overlooked.