[New-bugs-announce] [issue44158] Clarify documentation for redirected stdout/stderr when using subprocess in Linux

Andres Gomez report at bugs.python.org
Mon May 17 15:46:00 EDT 2021

New submission from Andres Gomez <agomez at igalia.com>:

1. Redirect the stderr and stdout with contextlib's redirect_stderr, redirect_stdout or just by replacing the fd in sys.stderr and sys.stdout
2. We run a subprocess with subprocess.run()
3. The documentation for subprocess states:


With the default settings of None, no redirection will occur; the child’s file handles will be inherited from the parent.

4. The documentation for contextlib states:


Note that the global side effect on sys.stdout means that this context manager is not suitable for use in library code and most threaded applications. It also has no effect on the output of subprocesses. 
5. The stdout and stderr for the subprocess-ed command is not redirected, as stated in the contextlib documentation.

It took me a while to realize that redirecting stdout and stderr didn't have an effect in the child subprocess.

It would be great if this could be improved so the subproccess-ed command also inherits the redirection. At the very least, this issue with the redirection should be added as a notice in the subprocess documentation.

The attached test.py script shows this behavior. The code is very simple and self-explanatory. You can run with:

# test.py


# test.py -n

Somehow, related to: https://bugs.python.org/issue1227748

components: Library (Lib)
files: test.py
messages: 393825
nosy: tanty
priority: normal
severity: normal
status: open
title: Clarify documentation for redirected stdout/stderr when using subprocess in Linux
type: behavior
versions: Python 3.7, Python 3.8, Python 3.9
Added file: https://bugs.python.org/file50048/test.py

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list