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.

Author tanty
Recipients tanty
Date 2021-05-17.19:45:59
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1621280760.31.0.799968756172.issue44158@roundup.psfhosted.org>
In-reply-to
Content
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:

https://docs.python.org/3/library/subprocess.html#frequently-used-arguments

"
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:

https://docs.python.org/3/library/contextlib.html?highlight=contextlib#contextlib.redirect_stdout

"
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

or

# test.py -n

Somehow, related to: https://bugs.python.org/issue1227748
History
Date User Action Args
2021-05-17 19:46:00tantysetrecipients: + tanty
2021-05-17 19:46:00tantysetmessageid: <1621280760.31.0.799968756172.issue44158@roundup.psfhosted.org>
2021-05-17 19:46:00tantylinkissue44158 messages
2021-05-17 19:46:00tantycreate