diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-12-24 21:18:13 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-24 21:18:13 -0800 |
commit | d5aadb28545fd15cd3517b604a8c7a520abd09c6 (patch) | |
tree | c9ac8e4e3804418b013427092136d55df3c32470 /Lib | |
parent | closes bpo-42726: gdb libpython: InstanceProxy support for py3 (GH-23912) (diff) | |
download | cpython-d5aadb28545fd15cd3517b604a8c7a520abd09c6.tar.gz cpython-d5aadb28545fd15cd3517b604a8c7a520abd09c6.tar.bz2 cpython-d5aadb28545fd15cd3517b604a8c7a520abd09c6.zip |
bpo-42388: Fix subprocess.check_output input=None when text=True (GH-23467)
When the modern text= spelling of the universal_newlines= parameter was added
for Python 3.7, check_output's special case around input=None was overlooked.
So it behaved differently with universal_newlines=True vs text=True. This
reconciles the behavior to be consistent and adds a test to guarantee it.
Also clarifies the existing check_output documentation.
Co-authored-by: Alexey Izbyshev <izbyshev@ispras.ru>
(cherry picked from commit 64abf373444944a240274a9b6d66d1cb01ecfcdd)
Co-authored-by: Gregory P. Smith <greg@krypto.org>
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/subprocess.py | 6 | ||||
-rw-r--r-- | Lib/test/test_subprocess.py | 22 |
2 files changed, 27 insertions, 1 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 5c2c2f05093..eecb1e7f253 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -406,7 +406,11 @@ def check_output(*popenargs, timeout=None, **kwargs): if 'input' in kwargs and kwargs['input'] is None: # Explicitly passing input=None was previously equivalent to passing an # empty string. That is maintained here for backwards compatibility. - kwargs['input'] = '' if kwargs.get('universal_newlines', False) else b'' + if kwargs.get('universal_newlines') or kwargs.get('text'): + empty = '' + else: + empty = b'' + kwargs['input'] = empty return run(*popenargs, stdout=PIPE, timeout=timeout, check=True, **kwargs).stdout diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 2d3ab93555b..4661d1e2528 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -195,6 +195,28 @@ class ProcessTestCase(BaseTestCase): input=b'pear') self.assertIn(b'PEAR', output) + def test_check_output_input_none(self): + """input=None has a legacy meaning of input='' on check_output.""" + output = subprocess.check_output( + [sys.executable, "-c", + "import sys; print('XX' if sys.stdin.read() else '')"], + input=None) + self.assertNotIn(b'XX', output) + + def test_check_output_input_none_text(self): + output = subprocess.check_output( + [sys.executable, "-c", + "import sys; print('XX' if sys.stdin.read() else '')"], + input=None, text=True) + self.assertNotIn('XX', output) + + def test_check_output_input_none_universal_newlines(self): + output = subprocess.check_output( + [sys.executable, "-c", + "import sys; print('XX' if sys.stdin.read() else '')"], + input=None, universal_newlines=True) + self.assertNotIn('XX', output) + def test_check_output_stdout_arg(self): # check_output() refuses to accept 'stdout' argument with self.assertRaises(ValueError) as c: |