bpo-31471: Fix assertion failure in subprocess.Popen() on Windows, in… · python/cpython@0b3a87e (original) (raw)

File tree

3 files changed

lines changed

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -2742,6 +2742,15 @@ def test_invalid_args(self):
2742 2742 stdout=subprocess.PIPE,
2743 2743 close_fds=True)
2744 2744
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 +
2745 2754 def test_close_fds(self):
2746 2755 # close file descriptors
2747 2756 rc = subprocess.call([sys.executable, "-c",
Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff line change
@@ -723,9 +723,13 @@ getenvironment(PyObject* environment)
723 723 }
724 724
725 725 keys = PyMapping_Keys(environment);
726 +if (!keys) {
727 +return NULL;
728 + }
726 729 values = PyMapping_Values(environment);
727 -if (!keys |
730 +if (!values) {
728 731 goto error;
732 + }
729 733
730 734 envsize = PySequence_Fast_GET_SIZE(keys);
731 735 if (PySequence_Fast_GET_SIZE(values) != envsize) {