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
The behavior with Popen.communicate() if stdin/stdout/stderr are PIPEs and they are closed before calling communicate() is rather strange.
If both stdout and stderr are PIPEs and one (or both) is closed, communicate() succeeds. If only one of them is a PIPE and it is closed, the behavior changes and there's a ValueError instead:
If stdin is a closed PIPE, the behavior is pretty much the opposite of the above. If other streams are not PIPEs, communicate() succeeds, but if the are other PIPEs, we get a ValueError:
This inconsistency seems to be caused by an optimization that leads to two different code paths depending on the number of PIPEs. I needed to spend some time to understand why our tests behaved seemingly inconsistently after code was refactored.
It would be easy to fix problems by changing code like if self.stdout: to if self.stdout and not self.stdout.closed:. I guess it could be argued that raising a ValueError is the correct way to handle these situations, but in that case it should be raised always.
Tested with Python 3.14 alpha 5. Occurs also with earlier ones.
CPython versions tested on:
3.14
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered:
If this is agreed to be a bug, I can provide a PR fixing these inconsistencies so that closed PIPEs are always handled gracefully. In that case we needed to decide should None or an empty string/bytes (depending on self.text_mode) be returned if stdout or stderr is a closed PIPE. A naïve fix would return None, but the current un-optimized code path returns an empty string/bytes and changing it could cause backwards compatibility problems.
Bug report
Bug description:
The behavior with
Popen.communicate()
if stdin/stdout/stderr arePIPE
s and they are closed before callingcommunicate()
is rather strange.If both stdout and stderr are
PIPE
s and one (or both) is closed,communicate()
succeeds. If only one of them is aPIPE
and it is closed, the behavior changes and there's aValueError
instead:If stdin is a closed
PIPE
, the behavior is pretty much the opposite of the above. If other streams are notPIPE
s,communicate()
succeeds, but if the are otherPIPE
s, we get aValueError
:This inconsistency seems to be caused by an optimization that leads to two different code paths depending on the number of
PIPE
s. I needed to spend some time to understand why our tests behaved seemingly inconsistently after code was refactored.It would be easy to fix problems by changing code like
if self.stdout:
toif self.stdout and not self.stdout.closed:
. I guess it could be argued that raising aValueError
is the correct way to handle these situations, but in that case it should be raised always.Tested with Python 3.14 alpha 5. Occurs also with earlier ones.
CPython versions tested on:
3.14
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered: