Skip to content

Commit db64fb9

Browse files
authoredOct 5, 2022
gh-97825: fix AttributeError when calling subprocess.check_output(input=None) with encoding or errors args (#97826)
* fix AttributeError, add unit test
1 parent 0ceafa7 commit db64fb9

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed
 

‎Lib/subprocess.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,8 @@ def check_output(*popenargs, timeout=None, **kwargs):
456456
if 'input' in kwargs and kwargs['input'] is None:
457457
# Explicitly passing input=None was previously equivalent to passing an
458458
# empty string. That is maintained here for backwards compatibility.
459-
if kwargs.get('universal_newlines') or kwargs.get('text'):
459+
if kwargs.get('universal_newlines') or kwargs.get('text') or kwargs.get('encoding') \
460+
or kwargs.get('errors'):
460461
empty = ''
461462
else:
462463
empty = b''

‎Lib/test/test_subprocess.py

+6
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ def test_check_output_input_none_universal_newlines(self):
238238
input=None, universal_newlines=True)
239239
self.assertNotIn('XX', output)
240240

241+
def test_check_output_input_none_encoding_errors(self):
242+
output = subprocess.check_output(
243+
[sys.executable, "-c", "print('foo')"],
244+
input=None, encoding='utf-8', errors='ignore')
245+
self.assertIn('foo', output)
246+
241247
def test_check_output_stdout_arg(self):
242248
# check_output() refuses to accept 'stdout' argument
243249
with self.assertRaises(ValueError) as c:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes :exc:`AttributeError` when :meth:`subprocess.check_output` is used with argument ``input=None`` and either of the arguments *encoding* or *errors* are used.

0 commit comments

Comments
 (0)
Please sign in to comment.