classification
Title: fcntl.ioctl always fails claiming an invalid fd
Type: behavior Stage:
Components: Extension Modules Versions: Python 3.1
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: bgamari, ned.deily
Priority: normal Keywords:

Created on 2010-11-07 01:16 by bgamari, last changed 2010-11-21 21:19 by ned.deily. This issue is now closed.

Messages (3)
msg120657 - (view) Author: Ben Gamari (bgamari) Date: 2010-11-07 01:16
Even the simple example included below fails in the following manner,

$ sudo python3.1 hi.py 
<class 'int'> 3
Traceback (most recent call last):
  File "hi.py", line 13, in <module>
    ioctl(a, EVIOCGID, buf, True)
TypeError: ioctl requires a file or file descriptor, an integer and optionally an integer or buffer argument

As the debugging output demonstrates, the fileno() is in fact a valid fd.


#!/usr/bin/python
from fcntl import ioctl
EVIOCGID = 1
f = open('/dev/input/mouse0', 'w')
buf = bytes([0]*128)
a = (f.fileno(),)
print(a.__class__, a)
ioctl(a, EVIOCGID, buf, True)
print(buf)
msg120659 - (view) Author: Ben Gamari (bgamari) Date: 2010-11-07 01:42
The problem seems to have been that I tried passing mutate_flag. Things seem to work fine when the last parameter is omitted.
msg121997 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2010-11-21 21:19
If you call ioctl with the mutate_flag True, you need to pass "an object supporting the read-write buffer protocol" as the arg parameter for the results of ioctl to be returned in.  In your example, you pass a bytes object which is immutable.  Try using a bytearray object instead, which is mutable, or, for instance, an array object as shown in the documentation.

http://docs.python.org/py3k/library/fcntl.html#fcntl.ioctl

http://docs.python.org/py3k/library/stdtypes.html#bytes-and-byte-array-methods
History
Date User Action Args
2010-11-21 21:19:54ned.deilysetmessages: - msg121996
2010-11-21 21:19:46ned.deilysetmessages: + msg121997
2010-11-21 21:18:46ned.deilysetstatus: open -> closed

nosy: + ned.deily
messages: + msg121996

resolution: not a bug
2010-11-07 01:42:17bgamarisetmessages: + msg120659
2010-11-07 01:17:06bgamarisettype: behavior
2010-11-07 01:16:41bgamaricreate