Skip to content

Commit 64abf37

Browse files
gpsheadAlexey Izbyshev
andauthored
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 <[email protected]>
1 parent 8badade commit 64abf37

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

Doc/library/subprocess.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,8 +1187,9 @@ calls these functions.
11871187
The arguments shown above are merely some common ones.
11881188
The full function signature is largely the same as that of :func:`run` -
11891189
most arguments are passed directly through to that interface.
1190-
However, explicitly passing ``input=None`` to inherit the parent's
1191-
standard input file handle is not supported.
1190+
One API deviation from :func:`run` behavior exists: passing ``input=None``
1191+
will behave the same as ``input=b''`` (or ``input=''``, depending on other
1192+
arguments) rather than using the parent's standard input file handle.
11921193

11931194
By default, this function will return the data as encoded bytes. The actual
11941195
encoding of the output data may depend on the command being invoked, so the

Lib/subprocess.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,11 @@ def check_output(*popenargs, timeout=None, **kwargs):
420420
if 'input' in kwargs and kwargs['input'] is None:
421421
# Explicitly passing input=None was previously equivalent to passing an
422422
# empty string. That is maintained here for backwards compatibility.
423-
kwargs['input'] = '' if kwargs.get('universal_newlines', False) else b''
423+
if kwargs.get('universal_newlines') or kwargs.get('text'):
424+
empty = ''
425+
else:
426+
empty = b''
427+
kwargs['input'] = empty
424428

425429
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
426430
**kwargs).stdout

Lib/test/test_subprocess.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,28 @@ def test_check_output_input_arg(self):
204204
input=b'pear')
205205
self.assertIn(b'PEAR', output)
206206

207+
def test_check_output_input_none(self):
208+
"""input=None has a legacy meaning of input='' on check_output."""
209+
output = subprocess.check_output(
210+
[sys.executable, "-c",
211+
"import sys; print('XX' if sys.stdin.read() else '')"],
212+
input=None)
213+
self.assertNotIn(b'XX', output)
214+
215+
def test_check_output_input_none_text(self):
216+
output = subprocess.check_output(
217+
[sys.executable, "-c",
218+
"import sys; print('XX' if sys.stdin.read() else '')"],
219+
input=None, text=True)
220+
self.assertNotIn('XX', output)
221+
222+
def test_check_output_input_none_universal_newlines(self):
223+
output = subprocess.check_output(
224+
[sys.executable, "-c",
225+
"import sys; print('XX' if sys.stdin.read() else '')"],
226+
input=None, universal_newlines=True)
227+
self.assertNotIn('XX', output)
228+
207229
def test_check_output_stdout_arg(self):
208230
# check_output() refuses to accept 'stdout' argument
209231
with self.assertRaises(ValueError) as c:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix subprocess.check_output(..., input=None) behavior when text=True to be
2+
consistent with that of the documentation and universal_newlines=True.

0 commit comments

Comments
 (0)