From 0abca3465b85b9e295ee23fbb54833514a285408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kul=C3=ADk?= Date: Thu, 14 Dec 2023 10:01:47 +0100 Subject: [PATCH 1/4] 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. --- Lib/subprocess.py | 3 --- Modules/posixmodule.c | 16 ++++++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index d6edd1a9807d1b..1919ea4bddeeda 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -1756,9 +1756,6 @@ def _posix_spawn(self, args, executable, env, restore_signals, c2pread, c2pwrite, errread, errwrite): """Execute program using os.posix_spawn().""" - if env is None: - env = os.environ - kwargs = {} if restore_signals: # See _Py_RestoreSignals() in Python/pylifecycle.c diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index ddbb4cd43babfc..99389254a16306 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -7082,9 +7082,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; } @@ -7098,9 +7098,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) { @@ -7163,7 +7167,7 @@ py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *a if (attrp) { (void)posix_spawnattr_destroy(attrp); } - if (envlist) { + if (envlist && envlist != environ) { free_string_array(envlist, envc); } if (argvlist) { From 8c0974c2148ffda7abdbd22503d166656a8f340d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kul=C3=ADk?= Date: Thu, 14 Dec 2023 11:10:33 +0100 Subject: [PATCH 2/4] Add documentation --- Doc/library/os.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Doc/library/os.rst b/Doc/library/os.rst index 9d2a3d65069253..2443155a561650 100644 --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -4553,7 +4553,8 @@ written in Python, such as a mail server's external command delivery program. Most users should use :func:`subprocess.run` instead of :func:`posix_spawn`. The positional-only arguments *path*, *args*, and *env* are similar to - :func:`execve`. + :func:`execve`. *env* is allowed to be ``None``, in which case current + process' environment is used. The *path* parameter is the path to the executable file. The *path* should contain a directory. Use :func:`posix_spawnp` to pass an executable file @@ -4628,6 +4629,9 @@ written in Python, such as a mail server's external command delivery program. .. versionadded:: 3.8 + .. versionchanged:: 3.13 + *env* parameter accepts ``None``. + .. availability:: Unix, not Emscripten, not WASI. .. function:: posix_spawnp(path, argv, env, *, file_actions=None, \ From c321f35e1d839ff2276fd4fafd170662dbc2836e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kul=C3=ADk?= Date: Fri, 15 Dec 2023 18:21:46 +0100 Subject: [PATCH 3/4] Add NEWS fragment and whatsnew/3.13 entry --- Doc/whatsnew/3.13.rst | 4 ++++ .../Library/2023-12-15-18-13-59.gh-issue-113119.al-569.rst | 2 ++ 2 files changed, 6 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-12-15-18-13-59.gh-issue-113119.al-569.rst diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index d599ba9ae6fac8..cdcb88349c1c64 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -261,6 +261,10 @@ os CPU resources of a container system without having to modify the container (application code). (Contributed by Donghee Na in :gh:`109595`) +* :func:`os.posix_spawn` now accepts ``env=None``, which makes the newly spawned + process use the current process environment. + (Contributed by Jakub Kulik in :gh:`113119`.) + pathlib ------- diff --git a/Misc/NEWS.d/next/Library/2023-12-15-18-13-59.gh-issue-113119.al-569.rst b/Misc/NEWS.d/next/Library/2023-12-15-18-13-59.gh-issue-113119.al-569.rst new file mode 100644 index 00000000000000..94087b00515e97 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-12-15-18-13-59.gh-issue-113119.al-569.rst @@ -0,0 +1,2 @@ +:func:`os.posix_spawn` now accepts ``env=None``, which makes the newly spawned +process use the current process environment. Patch by Jakub Kulik. From ade82f1fa03af99ed46b165db609aabb09aea59a Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sat, 16 Dec 2023 20:55:53 -0800 Subject: [PATCH 4/4] remove trailing whitespace --- Doc/whatsnew/3.13.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 715245cc3e65b3..4af023566ff0bc 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -292,7 +292,7 @@ os * :func:`os.posix_spawn` now accepts ``env=None``, which makes the newly spawned process use the current process environment. (Contributed by Jakub Kulik in :gh:`113119`.) - + pathlib -------