Skip to content

Commit 0b3a87e

Browse files
orenmnserhiy-storchaka
authored andcommitted
bpo-31471: Fix assertion failure in subprocess.Popen() on Windows, in case env has a bad keys() method. (#3580)
1 parent f350a26 commit 0b3a87e

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
@@ -2742,6 +2742,15 @@ def test_invalid_args(self):
27422742
stdout=subprocess.PIPE,
27432743
close_fds=True)
27442744

2745+
@support.cpython_only
2746+
def test_issue31471(self):
2747+
# There shouldn't be an assertion failure in Popen() in case the env
2748+
# argument has a bad keys() method.
2749+
class BadEnv(dict):
2750+
keys = None
2751+
with self.assertRaises(TypeError):
2752+
subprocess.Popen([sys.executable, "-c", "pass"], env=BadEnv())
2753+
27452754
def test_close_fds(self):
27462755
# close file descriptors
27472756
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)