From 10029dd955a5c7ca14f6a78ee601cffdaf23e1cc Mon Sep 17 00:00:00 2001 From: Vincent Fazio Date: Sun, 6 Apr 2025 19:10:59 -0500 Subject: [PATCH 1/2] Add _PYTHON_SUBPROCESS_USE_POSIX_SPAWN environment knob Add support for disabling the use of `posix_spawn` via a variable in the process environment. While it was previously possible to toggle this by modifying the value of `subprocess._USE_POSIX_SPAWN`, this required either patching CPython or modifying it within the interpreter instance which is not always possible, such as when running applications or scripts not under a user's control. Signed-off-by: Vincent Fazio --- Lib/subprocess.py | 3 +++ .../Library/2025-04-06-19-25-12.gh-issue-129204.sAVFO6.rst | 2 ++ 2 files changed, 5 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-04-06-19-25-12.gh-issue-129204.sAVFO6.rst diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 749c728db729ae..da5f5729e097e9 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -715,6 +715,9 @@ def _use_posix_spawn(): # os.posix_spawn() is not available return False + if ((_env := os.environ.get('_PYTHON_SUBPROCESS_USE_POSIX_SPAWN')) in ('0', '1')): + return bool(int(_env)) + if sys.platform in ('darwin', 'sunos5'): # posix_spawn() is a syscall on both macOS and Solaris, # and properly reports errors diff --git a/Misc/NEWS.d/next/Library/2025-04-06-19-25-12.gh-issue-129204.sAVFO6.rst b/Misc/NEWS.d/next/Library/2025-04-06-19-25-12.gh-issue-129204.sAVFO6.rst new file mode 100644 index 00000000000000..54deeefe126008 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-04-06-19-25-12.gh-issue-129204.sAVFO6.rst @@ -0,0 +1,2 @@ +Introduce new `_PYTHON_SUBPROCESS_USE_POSIX_SPAWN` environment variable knob in +:mod:`subprocess` to control the use of :func:`os.posix_spawn`. From e08d8ddb36dce5ddf8ac94f8c22c34fb86fd9b04 Mon Sep 17 00:00:00 2001 From: Vincent Fazio Date: Sun, 6 Apr 2025 20:19:48 -0500 Subject: [PATCH 2/2] fixup NEWS entry --- .../next/Library/2025-04-06-19-25-12.gh-issue-129204.sAVFO6.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-04-06-19-25-12.gh-issue-129204.sAVFO6.rst b/Misc/NEWS.d/next/Library/2025-04-06-19-25-12.gh-issue-129204.sAVFO6.rst index 54deeefe126008..d8994bf760b7bc 100644 --- a/Misc/NEWS.d/next/Library/2025-04-06-19-25-12.gh-issue-129204.sAVFO6.rst +++ b/Misc/NEWS.d/next/Library/2025-04-06-19-25-12.gh-issue-129204.sAVFO6.rst @@ -1,2 +1,2 @@ -Introduce new `_PYTHON_SUBPROCESS_USE_POSIX_SPAWN` environment variable knob in +Introduce new ``_PYTHON_SUBPROCESS_USE_POSIX_SPAWN`` environment variable knob in :mod:`subprocess` to control the use of :func:`os.posix_spawn`.