mirror of https://github.com/python/cpython
gh-105436: The environment block should end with two null wchar_t values (GH-105495)
This commit is contained in:
parent
2b90796be6
commit
4f7d3b602d
|
@ -1692,6 +1692,13 @@ class RunFuncTestCase(BaseTestCase):
|
||||||
res = subprocess.run(args)
|
res = subprocess.run(args)
|
||||||
self.assertEqual(res.returncode, 57)
|
self.assertEqual(res.returncode, 57)
|
||||||
|
|
||||||
|
@unittest.skipUnless(mswindows, "Maybe test trigger a leak on Ubuntu")
|
||||||
|
def test_run_with_an_empty_env(self):
|
||||||
|
# gh-105436: fix subprocess.run(..., env={}) broken on Windows
|
||||||
|
args = [sys.executable, "-c", 'import sys; sys.exit(57)']
|
||||||
|
res = subprocess.run(args, env={})
|
||||||
|
self.assertEqual(res.returncode, 57)
|
||||||
|
|
||||||
def test_capture_output(self):
|
def test_capture_output(self):
|
||||||
cp = self.run_python(("import sys;"
|
cp = self.run_python(("import sys;"
|
||||||
"sys.stdout.write('BDFL'); "
|
"sys.stdout.write('BDFL'); "
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Ensure that an empty environment block is terminated by two null characters,
|
||||||
|
as is required by Windows.
|
|
@ -796,6 +796,17 @@ getenvironment(PyObject* environment)
|
||||||
}
|
}
|
||||||
|
|
||||||
envsize = PyList_GET_SIZE(keys);
|
envsize = PyList_GET_SIZE(keys);
|
||||||
|
|
||||||
|
if (envsize == 0) {
|
||||||
|
// A environment block must be terminated by two null characters --
|
||||||
|
// one for the last string and one for the block.
|
||||||
|
buffer = PyMem_Calloc(2, sizeof(wchar_t));
|
||||||
|
if (!buffer) {
|
||||||
|
PyErr_NoMemory();
|
||||||
|
}
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
if (PyList_GET_SIZE(values) != envsize) {
|
if (PyList_GET_SIZE(values) != envsize) {
|
||||||
PyErr_SetString(PyExc_RuntimeError,
|
PyErr_SetString(PyExc_RuntimeError,
|
||||||
"environment changed size during iteration");
|
"environment changed size during iteration");
|
||||||
|
@ -869,7 +880,8 @@ getenvironment(PyObject* environment)
|
||||||
*p++ = L'\0';
|
*p++ = L'\0';
|
||||||
assert(p == end);
|
assert(p == end);
|
||||||
|
|
||||||
error:
|
cleanup:
|
||||||
|
error:
|
||||||
Py_XDECREF(keys);
|
Py_XDECREF(keys);
|
||||||
Py_XDECREF(values);
|
Py_XDECREF(values);
|
||||||
return buffer;
|
return buffer;
|
||||||
|
|
Loading…
Reference in New Issue