[Python-checkins] r83759 - in python/branches/py3k: Lib/test/test_subprocess.py PC/_subprocess.c

tim.golden python-checkins at python.org
Fri Aug 6 15:03:56 CEST 2010


Author: tim.golden
Date: Fri Aug  6 15:03:56 2010
New Revision: 83759

Log:
Issue #3210: Ensure stdio handles are closed if CreateProcess fails

Modified:
   python/branches/py3k/Lib/test/test_subprocess.py
   python/branches/py3k/PC/_subprocess.c

Modified: python/branches/py3k/Lib/test/test_subprocess.py
==============================================================================
--- python/branches/py3k/Lib/test/test_subprocess.py	(original)
+++ python/branches/py3k/Lib/test/test_subprocess.py	Fri Aug  6 15:03:56 2010
@@ -544,6 +544,26 @@
         output = subprocess.check_output([sys.executable, '-c', code])
         self.assert_(output.startswith(b'Hello World!'), ascii(output))
 
+    def test_handles_closed_on_exception(self):
+        # If CreateProcess exits with an error, ensure the
+        # duplicate output handles are released
+        ifhandle, ifname = mkstemp()
+        ofhandle, ofname = mkstemp()
+        efhandle, efname = mkstemp()
+        try:
+            subprocess.Popen (["*"], stdin=ifhandle, stdout=ofhandle,
+              stderr=efhandle)
+        except OSError:
+            os.close(ifhandle)
+            os.remove(ifname)
+            os.close(ofhandle)
+            os.remove(ofname)
+            os.close(efhandle)
+            os.remove(efname)
+        self.assertFalse(os.path.exists(ifname))
+        self.assertFalse(os.path.exists(ofname))
+        self.assertFalse(os.path.exists(efname))
+
 
 # context manager
 class _SuppressCoreFiles(object):

Modified: python/branches/py3k/PC/_subprocess.c
==============================================================================
--- python/branches/py3k/PC/_subprocess.c	(original)
+++ python/branches/py3k/PC/_subprocess.c	Fri Aug  6 15:03:56 2010
@@ -429,6 +429,7 @@
     PyObject* env_mapping;
     Py_UNICODE* current_directory;
     PyObject* startup_info;
+    DWORD error;
 
     if (! PyArg_ParseTuple(args, "ZZOOiiOZO:CreateProcess",
                            &application_name,
@@ -478,8 +479,22 @@
 
     Py_XDECREF(environment);
 
-    if (! result)
-        return PyErr_SetFromWindowsErr(GetLastError());
+    if (! result) {
+        error = GetLastError();
+        if(si.hStdInput != INVALID_HANDLE_VALUE) {
+            CloseHandle(si.hStdInput);
+            si.hStdInput = INVALID_HANDLE_VALUE;
+        }
+        if(si.hStdOutput != INVALID_HANDLE_VALUE) {
+            CloseHandle(si.hStdOutput);
+            si.hStdOutput = INVALID_HANDLE_VALUE;
+        }
+        if(si.hStdError != INVALID_HANDLE_VALUE) {
+            CloseHandle(si.hStdError);
+            si.hStdError = INVALID_HANDLE_VALUE;
+        }
+        return PyErr_SetFromWindowsErr(error);
+    }
 
     return Py_BuildValue("NNii",
                          sp_handle_new(pi.hProcess),


More information about the Python-checkins mailing list