From 4d3851727fb82195e4995c6064b0b2d6cbc031c4 Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Fri, 18 Aug 2017 16:18:13 +0300 Subject: [PATCH] bpo-30121: Fix debug assert in subprocess on Windows (#1224) * bpo-30121: Fix debug assert in subprocess on Windows This is caused by closing HANDLEs using os.close which is for CRT file descriptors and not for HANDLEs. * bpo-30121: Suppress debug assertion in test_subprocess when ran directly --- Lib/subprocess.py | 8 +++++++- Lib/test/test_subprocess.py | 9 +++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 99bca477ce..2805ec3fa3 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -727,7 +727,10 @@ class Popen(object): 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 @@ -1007,6 +1010,9 @@ class Popen(object): 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 1d2d15c7a2..8cda1e81c8 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -1116,10 +1116,11 @@ class ProcessTestCase(BaseTestCase): 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) -- 2.40.0