Skip to content

Commit f135f62

Browse files
miss-islingtonserhiy-storchaka
authored andcommitted
[3.6] bpo-31471: Fix assertion failure in subprocess.Popen() on Windows, in case env has a bad keys() method. (GH-3580) (#3584)
(cherry picked from commit 0b3a87e)
1 parent 20fa05d commit f135f62

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

Lib/test/test_subprocess.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2735,6 +2735,15 @@ def test_invalid_args(self):
27352735
stdout=subprocess.PIPE,
27362736
close_fds=True)
27372737

2738+
@support.cpython_only
2739+
def test_issue31471(self):
2740+
# There shouldn't be an assertion failure in Popen() in case the env
2741+
# argument has a bad keys() method.
2742+
class BadEnv(dict):
2743+
keys = None
2744+
with self.assertRaises(TypeError):
2745+
subprocess.Popen([sys.executable, "-c", "pass"], env=BadEnv())
2746+
27382747
def test_close_fds(self):
27392748
# close file descriptors
27402749
rc = subprocess.call([sys.executable, "-c",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an assertion failure in `subprocess.Popen()` on Windows, in case the env
2+
argument has a bad keys() method. Patch by Oren Milman.

Modules/_winapi.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,9 +723,13 @@ getenvironment(PyObject* environment)
723723
}
724724

725725
keys = PyMapping_Keys(environment);
726+
if (!keys) {
727+
return NULL;
728+
}
726729
values = PyMapping_Values(environment);
727-
if (!keys || !values)
730+
if (!values) {
728731
goto error;
732+
}
729733

730734
envsize = PySequence_Fast_GET_SIZE(keys);
731735
if (PySequence_Fast_GET_SIZE(values) != envsize) {

0 commit comments

Comments
 (0)