Index: Lib/subprocess.py =================================================================== --- Lib/subprocess.py (revision 54211) +++ Lib/subprocess.py (working copy) @@ -547,9 +547,10 @@ if preexec_fn is not None: raise ValueError("preexec_fn is not supported on Windows " "platforms") - if close_fds: + if close_fds and (stdin is not None or stdout is not None or + stderr is not None): raise ValueError("close_fds is not supported on Windows " - "platforms") + "platforms if you redirect stdin/out/err") else: # POSIX if startupinfo is not None: @@ -772,6 +773,11 @@ args = list2cmdline(args) # Process startup details + if close_fds: + inheritHandles = 0 + else: + inheritHandles = 1 + if startupinfo is None: startupinfo = STARTUPINFO() if None not in (p2cread, c2pwrite, errwrite): @@ -779,6 +785,10 @@ startupinfo.hStdInput = p2cread startupinfo.hStdOutput = c2pwrite startupinfo.hStdError = errwrite + # Must inherit handles to pass std handles + # Checked close_fds earlier so this assert should hold + assert inheritHandles == 1 + inheritHandles = 1 # In case assert is disabled if shell: startupinfo.dwFlags |= STARTF_USESHOWWINDOW @@ -806,9 +816,7 @@ hp, ht, pid, tid = CreateProcess(executable, args, # no special security None, None, - # must inherit handles to pass std - # handles - 1, + inheritHandles, creationflags, env, cwd, Index: Lib/test/test_subprocess.py =================================================================== --- Lib/test/test_subprocess.py (revision 54211) +++ Lib/test/test_subprocess.py (working copy) @@ -617,8 +617,15 @@ self.assertRaises(ValueError, subprocess.call, [sys.executable, "-c", "import sys; sys.exit(47)"], + stdout=subprocess.PIPE, close_fds=True) + def test_close_fds(self): + rc = subprocess.call([sys.executable, "-c", + "import sys; sys.exit(47)"], + close_fds=True) + self.assertEqual(rc, 47) + def test_shell_sequence(self): # Run command through the shell (sequence) newenv = os.environ.copy() Index: Doc/lib/libsubprocess.tex =================================================================== --- Doc/lib/libsubprocess.tex (revision 54211) +++ Doc/lib/libsubprocess.tex (working copy) @@ -89,7 +89,10 @@ If \var{close_fds} is true, all file descriptors except \constant{0}, \constant{1} and \constant{2} will be closed before the child process is -executed. (\UNIX{} only) +executed. (\UNIX{} only). Or, on Windows, if \var{close_fds} is true +then no handles will be inherited by the child process. Note that on +Windows, you cannot set \var{close_fds} to true and also redirect the +standard handles by setting \var{stdin}, \var{stdout} or \var{stderr}. If \var{shell} is \constant{True}, the specified command will be executed through the shell.