diff options
author | Martin Panter <vadmium+py@gmail.com> | 2016-05-13 07:45:21 (GMT) |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2016-05-13 07:45:21 (GMT) |
commit | f94ec1bd832831186e309afbec60ac1ed0200564 (patch) | |
tree | ab76afcf8f48ce5115abc852a00d73d40e468913 /Lib | |
parent | 99740925ed5472e0c0c22ee01d0f2250d9287ff9 (diff) | |
parent | c76358924f5d750460f8693c05987dd866cb9e8a (diff) | |
download | cpython-f94ec1bd832831186e309afbec60ac1ed0200564.zip cpython-f94ec1bd832831186e309afbec60ac1ed0200564.tar.gz cpython-f94ec1bd832831186e309afbec60ac1ed0200564.tar.bz2 |
Issue #22274: Merge stderr=STDOUT fix from 3.5
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/subprocess.py | 5 | ||||
-rw-r--r-- | Lib/test/test_subprocess.py | 21 |
2 files changed, 25 insertions, 1 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py index e980349..642c7f2 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -1412,7 +1412,10 @@ class Popen(object): 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 index e7519a2..012ff79 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -500,6 +500,27 @@ class ProcessTestCase(BaseTestCase): 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", |