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()
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",
--- /dev/null
+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.