Created
May 15, 2025 11:27
-
-
Save moreati/034fef45f73d809d9411a8a63eca34d6 to your computer and use it in GitHub Desktop.
Python subprocesses with closed stdio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import subprocess | |
import sys | |
PRINT_STDERR_REPR_PY = 'import sys; print(f" {sys.stderr=}\\n")' | |
print('Child stderr -> our stderr') | |
subprocess.run([sys.executable, '-c', PRINT_STDERR_REPR_PY]) | |
print('Child stderr -> /dev/null') | |
subprocess.run([sys.executable, '-c', PRINT_STDERR_REPR_PY], stderr=subprocess.DEVNULL) | |
print('Child stderr closed by shell') | |
subprocess.run(f"{sys.executable} -c '{PRINT_STDERR_REPR_PY}' 2>&-", shell=True) | |
print('Child stderr -> a closed file object/descriptor') | |
pid = os.posix_spawn( | |
sys.executable, | |
[sys.executable, '-c', PRINT_STDERR_REPR_PY], | |
os.environ, | |
file_actions=[(os.POSIX_SPAWN_CLOSE, 2)], | |
) | |
_, status = os.waitpid(pid, 0) | |
assert status == 0 | |
print("Child stderr -> parent's closed sys.stderr object") | |
subprocess.run([sys.executable, '-c', PRINT_STDERR_REPR_PY]) | |
print("Child stderr -> parent's closed stderr descriptor") | |
os.close(2) | |
subprocess.run([sys.executable, '-c', PRINT_STDERR_REPR_PY]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note the cases where
sys.stderr == None
in the child process