* 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.
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
.. 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, \
``False`` on Windows.
(Contributed by Serhiy Storchaka in :gh:`59616`)
+* :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
-------
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
--- /dev/null
+:func:`os.posix_spawn` now accepts ``env=None``, which makes the newly spawned
+process use the current process environment. Patch by Jakub Kulik.
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;
}
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) {
if (attrp) {
(void)posix_spawnattr_destroy(attrp);
}
- if (envlist) {
+ if (envlist && envlist != environ) {
free_string_array(envlist, envc);
}
if (argvlist) {