Skip to content

Commit 6b799e1

Browse files
[3.14] gh-74389: gh-70560: subprocess.Popen.communicate() now ignores stdin.flush error when closed (GH-142061) (#142064)
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 c7f7411 commit 6b799e1

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
@@ -2097,6 +2097,10 @@ def _communicate(self, input, endtime, orig_timeout):
20972097
self.stdin.flush()
20982098
except BrokenPipeError:
20992099
pass # communicate() must ignore BrokenPipeError.
2100+
except ValueError:
2101+
# ignore ValueError: I/O operation on closed file.
2102+
if not self.stdin.closed:
2103+
raise
21002104
if not input:
21012105
try:
21022106
self.stdin.close()

Lib/test/test_subprocess.py

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

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