-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Closed
Closed
Copy link
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
Subprocess created with posix_spawn can have different environment when compared to a process created with stardard fork/exec.
The reason is that posix_spawn uses os.environ (when no env was given), which is not affected by os.putenv and os.unsetenv. This is different from execv, which uses process environment (available through environ).
This can be easily reproduced with test_putenv_unsetenv modified to call subprocess.run with close_fds=False (which makes it use the posix_spawn call):
import subprocess
import sys
import os
name = "PYTHONTESTVAR"
value = "testvalue"
code = f'import os; print(repr(os.environ.get({name!r})))'
os.putenv(name, value)
proc = subprocess.run([sys.executable, '-c', code], check=True,
stdout=subprocess.PIPE, text=True, close_fds=False)
assert proc.stdout.rstrip() == repr(value)
os.unsetenv(name)
proc = subprocess.run([sys.executable, '-c', code], check=True,
stdout=subprocess.PIPE, text=True, close_fds=False)
assert proc.stdout.rstrip() == repr(None)I found it when I patched Python with #113118 (on Solaris, but this is pretty platform independent, I think).
CPython versions tested on:
3.9, 3.11
Operating systems tested on:
Other
Linked PRs
gpshead, qqwqqw689 and mxmlnknkulikjak and qqwqqw689
Metadata
Metadata
Assignees
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error