[issue21191] os.fdopen() may eat file descriptor and still raise exception

Dima Tisnek report at bugs.python.org
Fri Apr 11 11:40:20 CEST 2014


Dima Tisnek added the comment:

I think consistency between Python versions is just as important as consistency between fd types.

Here's my hack quickfix outline:

fd = os.open(...)
try:
    if not stat.S_ISREG(os.fstat(fd).st_mode):
        raise OSError(None, "Not a regular file", ...)
    f = os.fdopen(fd, ...)
except EnvironmentError:
    os.close(fd)

Can something like this be implemented in os.py
There's already a check `if not isinstance(fd, int): raise ...`

Granted we'd have to get fd type check exactly right.
fdopen should probably succeed for regular files, pipes, char devices, block device, ptry's ...
fdopen should fail where underlying implementation fails, i.e. directories, sockets(?), epoll(?), timerfd(?)

There's a list at http://en.wikipedia.org/wiki/File_descriptor
I'm not sure about some types.

P.S. I wish there was a way to rescue fd from FILE*, but nothing like that seems to exist...

P.P.S. another option is to always use dup(), but that may break existing programs is they expect fd == fdopen(fd).fileno()

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21191>
_______________________________________


More information about the Python-bugs-list mailing list