This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Clarify documentation for redirected stdout/stderr when using subprocess in Linux
Type: behavior Stage:
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, slateny, tanty
Priority: normal Keywords: easy

Created on 2021-05-17 19:46 by tanty, last changed 2022-04-11 14:59 by admin.

File name Uploaded Description Edit tanty, 2021-05-17 19:45 Example test
Messages (2)
msg393825 - (view) Author: Andres Gomez (tanty) Date: 2021-05-17 19:45
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
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 script shows this behavior. The code is very simple and self-explanatory. You can run with:



# -n

Somehow, related to:
msg416764 - (view) Author: Stanley (slateny) * Date: 2022-04-05 09:31
Hmm, I'm not quite following - when you say that the subproccess-ed command should also inherit the redirection, which subprocess command are you referring to, and nested or unnested? I gave it a ran and everything seems to run how I'd expected it, so maybe put another way, what's the expected output from the files and stdout/stderr?
Date User Action Args
2022-04-11 14:59:45adminsetgithub: 88324
2022-04-05 09:31:13slatenysetnosy: + slateny
messages: + msg416764
2022-01-15 16:38:08iritkatrielsetkeywords: + easy
2022-01-15 16:38:00iritkatrielsetassignee: docs@python

nosy: + docs@python
components: + Documentation, - Library (Lib)
versions: + Python 3.10, Python 3.11, - Python 3.7, Python 3.8
2021-05-17 19:46:00tantycreate