
This issue trackerhas been migrated toGitHub, and is currentlyread-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.
Created on2014-08-25 21:46 byakira, last changed2022-04-11 14:58 byadmin. This issue is nowclosed.
| Files | ||||
|---|---|---|---|---|
| File name | Uploaded | Description | Edit | |
| subprocess-stderr_redirect_with_no_stdout_redirect.diff | akira,2014-08-25 21:46 | review | ||
| subprocess-stderr_redirect_with_no_stdout_redirect-2.diff | akira,2016-05-08 12:46 | print -> .write | review | |
| Messages (7) | |||
|---|---|---|---|
| msg225898 -(view) | Author: Akira Li (akira)* | Date: 2014-08-25 21:46 | |
The following command should not produce any output but it does: $ ./python >/dev/null -c 'import subprocess as S, sys; S.call([sys.executable, "-c", "import sys; print(42, file=sys.stderr)"], stderr=S.STDOUT)'Its stdout is redirected to /dev/null. It starts a subprocess with itsstderr redirected to stdout. See "Redirect subprocess stderr tostdout" [1] on Stackoverflow.[1]http://stackoverflow.com/questions/11495783/redirect-subprocess-stderr-to-stdoutI've uploaded a patch that fixes the issue on POSIX.Please, run the provided test (in the patch), to see whether the codeshould be fixed on Windows too (it might work as is there).No documentation changes are required.Please, review. | |||
| msg225907 -(view) | Author: Josh Rosenberg (josh.r)*![]() | Date: 2014-08-25 23:48 | |
Okay, dumb question: Is there a reason the Windows code explicitly initializes c2pwrite in the "stdout not passed" case, while the Linux code leaves it as -1? Windows doesn't look like it would have the problem (because c2pwrite is always set to a non-default value), and it seems like the fix for Linux could just mimic the Windows approach; the code that sets errwrite wouldn't change, but instead of a "pass", when stdout is None, we'd explicitly set it to os.STDOUT_FILENO, and the stderr=subprocess.STDOUT (stdout unset) case would work automatically, and the code would be more similar.Haven't explored the negative consequences of that change, if any. | |||
| msg226020 -(view) | Author: Akira Li (akira)* | Date: 2014-08-28 12:10 | |
Josh, on Windows, if at least one standard stream is replaced; all threehStdInput, hStdOutput, hStdError handles are provided(all-or-nothing). On POSIX, standard streams stdin (0), stdout (1),stderr (2) are always inherited from the parent. Each stream can bemanipulated independently. c2pwrite=-1 is different from providingc2pwrite=1 (STDOUT_FILENO) explicitly e.g., set_inheritable() call ismade after the fork() in the latter case.My patch leads to dup2(fileno(stdout), STDERR_FILENO) when stdout isNone and stderr=STDOUT on POSIX i.e., it redirects stderr to theinherited stdout (like 2>&1 in the shell). It has no effect otherwise.sys.__stdout__ is used so that the call fails sooner without fork() iffileno(stdout) is not a valid file descriptor when python initializes(daemon, GUI). __stdout__-based solution doesn't support a case whenfileno(stdout) is changed later in the program e.g., using freopen() onsome systems. | |||
| msg265136 -(view) | Author: Akira Li (akira)* | Date: 2016-05-08 12:46 | |
Updated the patch to address vadmium's review comments. | |||
| msg265139 -(view) | Author: Martin Panter (martin.panter)*![]() | Date: 2016-05-08 13:27 | |
I think this patch is pretty good. I will try to commit it in the next few days. | |||
| msg265451 -(view) | Author: Roundup Robot (python-dev)![]() | Date: 2016-05-13 07:55 | |
New changeset642933771fa5 by Martin Panter in branch '3.5':Issue#22274: Redirect stderr=STDOUT when stdout not redirected, by Akira Lihttps://hg.python.org/cpython/rev/642933771fa5New changeset5979e7aadd59 by Martin Panter in branch 'default':Issue#22274: Merge stderr=STDOUT fix from 3.5https://hg.python.org/cpython/rev/5979e7aadd59New changeset5f46ecaf8c6e by Martin Panter in branch '2.7':Issue#22274: Redirect stderr=STDOUT when stdout not redirected, by Akira Lihttps://hg.python.org/cpython/rev/5f46ecaf8c6e | |||
| msg265460 -(view) | Author: Martin Panter (martin.panter)*![]() | Date: 2016-05-13 10:00 | |
Thanks for the patch Akira. | |||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:58:07 | admin | set | github: 66470 |
| 2017-05-26 18:33:14 | eryksun | link | issue30488 superseder |
| 2016-05-13 10:00:07 | martin.panter | set | status: open -> closed resolution: fixed messages: +msg265460 stage: patch review -> resolved |
| 2016-05-13 07:55:51 | python-dev | set | nosy: +python-dev messages: +msg265451 |
| 2016-05-08 13:27:17 | martin.panter | set | nosy: +martin.panter messages: +msg265139 versions: + Python 2.7, Python 3.5, Python 3.6 |
| 2016-05-08 12:46:34 | akira | set | files: +subprocess-stderr_redirect_with_no_stdout_redirect-2.diff messages: +msg265136 versions: - Python 3.4 |
| 2014-08-28 12:10:11 | akira | set | messages: +msg226020 |
| 2014-08-26 00:34:42 | vstinner | set | nosy: +vstinner |
| 2014-08-25 23:48:35 | josh.r | set | nosy: +josh.r messages: +msg225907 |
| 2014-08-25 22:13:44 | r.david.murray | set | nosy: +r.david.murray |
| 2014-08-25 22:00:27 | ned.deily | set | nosy: +gregory.p.smith stage: patch review |
| 2014-08-25 21:46:01 | akira | create | |