[Python-checkins] cpython (3.5): Issue #22274: Redirect stderr=STDOUT when stdout not redirected, by Akira Li

martin.panter python-checkins at python.org
Fri May 13 03:55:48 EDT 2016


https://hg.python.org/cpython/rev/642933771fa5
changeset:   101312:642933771fa5
branch:      3.5
parent:      101309:2c6b6667a26e
user:        Martin Panter <vadmium+py at gmail.com>
date:        Fri May 13 01:54:44 2016 +0000
summary:
  Issue #22274: Redirect stderr=STDOUT when stdout not redirected, by Akira Li

files:
  Lib/subprocess.py           |   5 ++++-
  Lib/test/test_subprocess.py |  21 +++++++++++++++++++++
  Misc/NEWS                   |   3 +++
  3 files changed, 28 insertions(+), 1 deletions(-)


diff --git a/Lib/subprocess.py b/Lib/subprocess.py
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -1401,7 +1401,10 @@
             elif stderr == PIPE:
                 errread, errwrite = os.pipe()
             elif stderr == STDOUT:
-                errwrite = c2pwrite
+                if c2pwrite != -1:
+                    errwrite = c2pwrite
+                else: # child's stdout is not set, use parent's stdout
+                    errwrite = sys.__stdout__.fileno()
             elif stderr == DEVNULL:
                 errwrite = self._get_devnull()
             elif isinstance(stderr, int):
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -504,6 +504,27 @@
         tf.seek(0)
         self.assertStderrEqual(tf.read(), b"strawberry")
 
+    def test_stderr_redirect_with_no_stdout_redirect(self):
+        # test stderr=STDOUT while stdout=None (not set)
+
+        # - grandchild prints to stderr
+        # - child redirects grandchild's stderr to its stdout
+        # - the parent should get grandchild's stderr in child's stdout
+        p = subprocess.Popen([sys.executable, "-c",
+                              'import sys, subprocess;'
+                              'rc = subprocess.call([sys.executable, "-c",'
+                              '    "import sys;"'
+                              '    "sys.stderr.write(\'42\')"],'
+                              '    stderr=subprocess.STDOUT);'
+                              'sys.exit(rc)'],
+                             stdout=subprocess.PIPE,
+                             stderr=subprocess.PIPE)
+        stdout, stderr = p.communicate()
+        #NOTE: stdout should get stderr from grandchild
+        self.assertStderrEqual(stdout, b'42')
+        self.assertStderrEqual(stderr, b'') # should be empty
+        self.assertEqual(p.returncode, 0)
+
     def test_stdout_stderr_pipe(self):
         # capture stdout and stderr to the same pipe
         p = subprocess.Popen([sys.executable, "-c",
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -118,6 +118,9 @@
 Library
 -------
 
+- Issue #22274: In the subprocess module, allow stderr to be redirected to
+  stdout even when stdout is not redirected.  Patch by Akira Li.
+
 - Issue #25745: Fixed leaking a userptr in curses panel destructor.
 
 - Issue #26977: Removed unnecessary, and ignored, call to sum of squares helper

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list