Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -2080,6 +2080,10 @@ def _communicate(self, input, endtime, orig_timeout):
self.stdin.flush()
except BrokenPipeError:
pass # communicate() must ignore BrokenPipeError.
except ValueError:
# ignore ValueError: I/O operation on closed file.
if not self.stdin.closed:
raise
if not input:
try:
self.stdin.close()
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,19 @@ def test_writes_before_communicate(self):
self.assertEqual(stdout, b"bananasplit")
self.assertEqual(stderr, b"")

def test_communicate_stdin_closed_before_call(self):
# gh-70560, gh-74389: stdin.close() before communicate()
# should not raise ValueError from stdin.flush()
with subprocess.Popen([sys.executable, "-c",
'import sys; sys.exit(0)'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE) as p:
p.stdin.close() # Close stdin before communicate
# This should not raise ValueError
(stdout, stderr) = p.communicate()
self.assertEqual(p.returncode, 0)

def test_universal_newlines_and_text(self):
args = [
sys.executable, "-c",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
When the stdin being used by a :class:`subprocess.Popen` instance is closed,
this is now ignored in :meth:`subprocess.Popen.communicate` instead of
leaving the class in an inconsistent state.
Loading