Skip to content

Commit 99b70fe

Browse files
[3.13] gh-74389: gh-70560: subprocess.Popen.communicate() now ignores stdin.flush error when closed (GH-142061) (#142065)
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 385688d commit 99b70fe

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

Lib/subprocess.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,6 +2099,10 @@ def _communicate(self, input, endtime, orig_timeout):
20992099
self.stdin.flush()
21002100
except BrokenPipeError:
21012101
pass # communicate() must ignore BrokenPipeError.
2102+
except ValueError:
2103+
# ignore ValueError: I/O operation on closed file.
2104+
if not self.stdin.closed:
2105+
raise
21022106
if not input:
21032107
try:
21042108
self.stdin.close()

Lib/test/test_subprocess.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,19 @@ def test_writes_before_communicate(self):
11591159
self.assertEqual(stdout, b"bananasplit")
11601160
self.assertEqual(stderr, b"")
11611161

1162+
def test_communicate_stdin_closed_before_call(self):
1163+
# gh-70560, gh-74389: stdin.close() before communicate()
1164+
# should not raise ValueError from stdin.flush()
1165+
with subprocess.Popen([sys.executable, "-c",
1166+
'import sys; sys.exit(0)'],
1167+
stdin=subprocess.PIPE,
1168+
stdout=subprocess.PIPE,
1169+
stderr=subprocess.PIPE) as p:
1170+
p.stdin.close() # Close stdin before communicate
1171+
# This should not raise ValueError
1172+
(stdout, stderr) = p.communicate()
1173+
self.assertEqual(p.returncode, 0)
1174+
11621175
def test_universal_newlines_and_text(self):
11631176
args = [
11641177
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)