]> granicus.if.org Git - python/commitdiff
bpo-31471: Fix assertion failure in subprocess.Popen() on Windows, in case env has...
authorOren Milman <orenmn@gmail.com>
Thu, 14 Sep 2017 19:30:28 +0000 (22:30 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Thu, 14 Sep 2017 19:30:27 +0000 (22:30 +0300)
Lib/test/test_subprocess.py
Misc/NEWS.d/next/Core and Builtins/2017-09-14-19-47-57.bpo-31471.0yiA5Q.rst [new file with mode: 0644]
Modules/_winapi.c

index 10ef87b849586bfeef6c59fca84e8f1aabbe4aff..00dc37bc2c729a32c5d19c2c26fc7ff9527300da 100644 (file)
@@ -2742,6 +2742,15 @@ class Win32ProcessTestCase(BaseTestCase):
                           stdout=subprocess.PIPE,
                           close_fds=True)
 
+    @support.cpython_only
+    def test_issue31471(self):
+        # There shouldn't be an assertion failure in Popen() in case the env
+        # argument has a bad keys() method.
+        class BadEnv(dict):
+            keys = None
+        with self.assertRaises(TypeError):
+            subprocess.Popen([sys.executable, "-c", "pass"], env=BadEnv())
+
     def test_close_fds(self):
         # close file descriptors
         rc = subprocess.call([sys.executable, "-c",
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-09-14-19-47-57.bpo-31471.0yiA5Q.rst b/Misc/NEWS.d/next/Core and Builtins/2017-09-14-19-47-57.bpo-31471.0yiA5Q.rst
new file mode 100644 (file)
index 0000000..73c4444
--- /dev/null
@@ -0,0 +1,2 @@
+Fix an assertion failure in `subprocess.Popen()` on Windows, in case the env
+argument has a bad keys() method. Patch by Oren Milman.
index a81ccafdda9709d2d42a2c2a5e0745af6e78b7e9..00a26d515e0cd3228b4d4ede1ced804d3495f878 100644 (file)
@@ -723,9 +723,13 @@ getenvironment(PyObject* environment)
     }
 
     keys = PyMapping_Keys(environment);
+    if (!keys) {
+        return NULL;
+    }
     values = PyMapping_Values(environment);
-    if (!keys || !values)
+    if (!values) {
         goto error;
+    }
 
     envsize = PySequence_Fast_GET_SIZE(keys);
     if (PySequence_Fast_GET_SIZE(values) != envsize) {