diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 551aad342b8304..010730fdb992c1 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -727,7 +727,10 @@ def __init__(self, args, bufsize=-1, executable=None, to_close.append(self._devnull) for fd in to_close: try: - os.close(fd) + if _mswindows and isinstance(fd, Handle): + fd.Close() + else: + os.close(fd) except OSError: pass @@ -1005,6 +1008,9 @@ def _execute_child(self, args, executable, preexec_fn, close_fds, errwrite.Close() if hasattr(self, '_devnull'): os.close(self._devnull) + # Prevent a double close of these handles/fds from __init__ + # on error. + self._closed_child_pipe_fds = True # Retain the process handle, but close the thread handle self._child_created = True diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 7fabe6ad765332..9fae5360dd8ed0 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -1068,10 +1068,11 @@ def _test_bufsize_equal_one(self, line, expected, universal_newlines): p.stdin.write(line) # expect that it flushes the line in text mode os.close(p.stdin.fileno()) # close it without flushing the buffer read_line = p.stdout.readline() - try: - p.stdin.close() - except OSError: - pass + with support.SuppressCrashReport(): + try: + p.stdin.close() + except OSError: + pass p.stdin = None self.assertEqual(p.returncode, 0) self.assertEqual(read_line, expected)