You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
subprocess.Popen does not handle file-like objects without file descriptors #74175
bpo-19992: subprocess documentation not explicit about fileno()
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2017-04-10.04:33:38.646>created_at=<Date2017-04-05.02:38:53.726>labels= ['type-bug', 'invalid']
title='subprocess.Popen does not handle file-like objects without file descriptors'updated_at=<Date2017-04-10.07:20:19.185>user='https://github.com/rtpg'
However, when passing a file-like object without a file descriptor (that raises OSError when calling f.fileno()) to POpen (for stdout, for example), the raised exception is not handled properly.
(However, on inspection of subprocess code, returning -1 will cause the code to handle this properly)
I'm not sure whether this is an issue in the io module documentation or in the subprocess code.
the core issue seems to be in POpen.get_handles, that seems to expect that -1 is used to signal "no file descriptor available".
Hmm, what do you mean by 'the raised exception is not handled properly'? Currently the exception is just propagated to the user to signal the argument seems to be wrong, right?
rtpg commented on Apr 6, 2017
rtpgmannequin
MannequinAuthor
More actions
the subprocess module has code to handle file objects that don't use file descriptors. It's not that file descriptors are necessary for the parameter, but that the way POpen is trying to detect "no file descriptor support" is through "fileno returns -1" rather than "fileno raises OSError"
'crash' means OS message rather than Python exiting with exception traceback and message.
Can you post a minimal reproducer? What OS? subprocess.Popen._get_handles is different on POSIX and windows, though both seem to call f.fileno() without try-except.
All filenoes are initialized to -1, so it seems to me that either
a. all accesses should be wrapped with try-except: pass, or
b. subprocess doc should say that file-like objects must include a fileno method returning -1.
I am puzzled though. The 2.7 doc for (builtin)file.fileno() says
"
Note
File-like objects which do not have a real file descriptor should not provide this method! "
"
Rather than return -1
In must be that the subprocess test does not test with a 'file-like object without a file descriptor'
Raphael: Can you point to the implementation code that handles file objects without a file descriptor (or give a demonstration of it)? I suspect there is no such support and you are mistaken.
Perhaps we can instead clarify in the “subprocess” documentation that “fileno” is required. bpo-19992 already proposes this.
bpo-1260171 and bpo-10482 are also related, about streaming data between Python file objects in the parent and pipes connected to the child.
rtpg commented on Apr 10, 2017
rtpgmannequin
MannequinAuthor
More actions
I'm sorry for the confusion here, it turns out I was misinterpreting the results of my program and my workaround was not working. Like others have said, the subprocess code seems to rely on the existence of file descriptors
I would be pretty partial to the idea of having the subprocess's internal code catch OSError and raise a clearer error message like "subprocess can only operate on files with file descriptor support".
Having "complete" file object support would seem like a major win, but looking at the internals/OS support it seems like a hard thing to set up.
The current exception seems to give a reasonable hint:
>>> subprocess.Popen((executable,), stdout=BytesIO())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/subprocess.py", line 914, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File "/usr/lib/python3.5/subprocess.py", line 1400, in _get_handles
c2pwrite = stdout.fileno()
io.UnsupportedOperation: fileno
Perhaps we can close this in favour of fixing the documentation? (bpo-19992)
rtpg commented on Apr 10, 2017
rtpgmannequin
MannequinAuthor
More actions
that error is what is raised by StringIO (subclassing OSError), other file-like objects could just be raising OSError (according to the file object spec described in the documentation for the io module).
Fixing documentation would already be helpful though. I will close this issue
Activity
rtpg commentedon Apr 5, 2017
From the documentation of the io module:
However, when passing a file-like object without a file descriptor (that raises OSError when calling f.fileno()) to POpen (for stdout, for example), the raised exception is not handled properly.
(However, on inspection of subprocess code, returning -1 will cause the code to handle this properly)
I'm not sure whether this is an issue in the io module documentation or in the subprocess code.
the core issue seems to be in POpen.get_handles, that seems to expect that -1 is used to signal "no file descriptor available".
zhangyangyu commentedon Apr 6, 2017
Hmm, what do you mean by 'the raised exception is not handled properly'? Currently the exception is just propagated to the user to signal the argument seems to be wrong, right?
rtpg commentedon Apr 6, 2017
the subprocess module has code to handle file objects that don't use file descriptors. It's not that file descriptors are necessary for the parameter, but that the way POpen is trying to detect "no file descriptor support" is through "fileno returns -1" rather than "fileno raises OSError"
terryjreedy commentedon Apr 7, 2017
'crash' means OS message rather than Python exiting with exception traceback and message.
Can you post a minimal reproducer? What OS? subprocess.Popen._get_handles is different on POSIX and windows, though both seem to call f.fileno() without try-except.
All filenoes are initialized to -1, so it seems to me that either
a. all accesses should be wrapped with try-except: pass, or
b. subprocess doc should say that file-like objects must include a fileno method returning -1.
I am puzzled though. The 2.7 doc for (builtin)file.fileno() says
"
Note
File-like objects which do not have a real file descriptor should not provide this method! "
"
Rather than return -1
In must be that the subprocess test does not test with a 'file-like object without a file descriptor'
vadmium commentedon Apr 8, 2017
Raphael: Can you point to the implementation code that handles file objects without a file descriptor (or give a demonstration of it)? I suspect there is no such support and you are mistaken.
Perhaps we can instead clarify in the “subprocess” documentation that “fileno” is required. bpo-19992 already proposes this.
bpo-1260171 and bpo-10482 are also related, about streaming data between Python file objects in the parent and pipes connected to the child.
rtpg commentedon Apr 10, 2017
I'm sorry for the confusion here, it turns out I was misinterpreting the results of my program and my workaround was not working. Like others have said, the subprocess code seems to rely on the existence of file descriptors
I would be pretty partial to the idea of having the subprocess's internal code catch OSError and raise a clearer error message like "subprocess can only operate on files with file descriptor support".
Having "complete" file object support would seem like a major win, but looking at the internals/OS support it seems like a hard thing to set up.
vadmium commentedon Apr 10, 2017
The current exception seems to give a reasonable hint:
Perhaps we can close this in favour of fixing the documentation? (bpo-19992)
rtpg commentedon Apr 10, 2017
that error is what is raised by StringIO (subclassing OSError), other file-like objects could just be raising OSError (according to the file object spec described in the documentation for the io module).
Fixing documentation would already be helpful though. I will close this issue