gh-113119 fix environment handling in subprocess.Popen when posix_spawn is used (#113120)

* Allow posix_spawn to inherit environment form parent environ variable.

With this change, posix_spawn call can behave similarly to execv with regards to environments when used in subprocess functions.
This commit is contained in:
Jakub Kulík 2023-12-17 06:19:05 +01:00 committed by GitHub
parent cde1335485
commit 48c907a15c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 10 deletions

View file

@ -7129,9 +7129,9 @@ py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *a
return NULL;
}
if (!PyMapping_Check(env)) {
if (!PyMapping_Check(env) && env != Py_None) {
PyErr_Format(PyExc_TypeError,
"%s: environment must be a mapping object", func_name);
"%s: environment must be a mapping object or None", func_name);
goto exit;
}
@ -7145,9 +7145,13 @@ py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *a
goto exit;
}
envlist = parse_envlist(env, &envc);
if (envlist == NULL) {
goto exit;
if (env == Py_None) {
envlist = environ;
} else {
envlist = parse_envlist(env, &envc);
if (envlist == NULL) {
goto exit;
}
}
if (file_actions != NULL && file_actions != Py_None) {
@ -7210,7 +7214,7 @@ exit:
if (attrp) {
(void)posix_spawnattr_destroy(attrp);
}
if (envlist) {
if (envlist && envlist != environ) {
free_string_array(envlist, envc);
}
if (argvlist) {