Skip to content

Commit 2ae7484

Browse files
gpsheadmiss-islington
authored andcommitted
pythongh-74389: pythongh-70560: subprocess.Popen.communicate() now ignores stdin.flush error when closed (pythonGH-142061)
pythongh-70560: pythongh-74389: subprocess.Popen.communicate() now ignores stdin.flush error when closed with a unittest and news entry. (cherry picked from commit 923056b) Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
1 parent c1d3e25 commit 2ae7484

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

Lib/subprocess.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2078,6 +2078,10 @@ def _communicate(self, input, endtime, orig_timeout):
20782078
self.stdin.flush()
20792079
except BrokenPipeError:
20802080
pass # communicate() must ignore BrokenPipeError.
2081+
except ValueError:
2082+
# ignore ValueError: I/O operation on closed file.
2083+
if not self.stdin.closed:
2084+
raise
20812085
if not input:
20822086
try:
20832087
self.stdin.close()

Lib/test/test_subprocess.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,19 @@ def test_writes_before_communicate(self):
11041104
self.assertEqual(stdout, b"bananasplit")
11051105
self.assertEqual(stderr, b"")
11061106

1107+
def test_communicate_stdin_closed_before_call(self):
1108+
# gh-70560, gh-74389: stdin.close() before communicate()
1109+
# should not raise ValueError from stdin.flush()
1110+
with subprocess.Popen([sys.executable, "-c",
1111+
'import sys; sys.exit(0)'],
1112+
stdin=subprocess.PIPE,
1113+
stdout=subprocess.PIPE,
1114+
stderr=subprocess.PIPE) as p:
1115+
p.stdin.close() # Close stdin before communicate
1116+
# This should not raise ValueError
1117+
(stdout, stderr) = p.communicate()
1118+
self.assertEqual(p.returncode, 0)
1119+
11071120
def test_universal_newlines_and_text(self):
11081121
args = [
11091122
sys.executable, "-c",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
When the stdin being used by a :class:`subprocess.Popen` instance is closed,
2+
this is now ignored in :meth:`subprocess.Popen.communicate` instead of
3+
leaving the class in an inconsistent state.

0 commit comments

Comments
 (0)