Skip to content

Commit 93f7643

Browse files
gpsheadmiss-islington
authored andcommitted
gh-74389: gh-70560: subprocess.Popen.communicate() now ignores stdin.flush error when closed (GH-142061)
gh-70560: gh-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 b3a0101 commit 93f7643

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
@@ -2080,6 +2080,10 @@ def _communicate(self, input, endtime, orig_timeout):
20802080
self.stdin.flush()
20812081
except BrokenPipeError:
20822082
pass # communicate() must ignore BrokenPipeError.
2083+
except ValueError:
2084+
# ignore ValueError: I/O operation on closed file.
2085+
if not self.stdin.closed:
2086+
raise
20832087
if not input:
20842088
try:
20852089
self.stdin.close()

Lib/test/test_subprocess.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,19 @@ def test_writes_before_communicate(self):
10611061
self.assertEqual(stdout, b"bananasplit")
10621062
self.assertEqual(stderr, b"")
10631063

1064+
def test_communicate_stdin_closed_before_call(self):
1065+
# gh-70560, gh-74389: stdin.close() before communicate()
1066+
# should not raise ValueError from stdin.flush()
1067+
with subprocess.Popen([sys.executable, "-c",
1068+
'import sys; sys.exit(0)'],
1069+
stdin=subprocess.PIPE,
1070+
stdout=subprocess.PIPE,
1071+
stderr=subprocess.PIPE) as p:
1072+
p.stdin.close() # Close stdin before communicate
1073+
# This should not raise ValueError
1074+
(stdout, stderr) = p.communicate()
1075+
self.assertEqual(p.returncode, 0)
1076+
10641077
def test_universal_newlines_and_text(self):
10651078
args = [
10661079
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)