Skip to content

Commit ece5f7e

Browse files
gh-97825: fix AttributeError when calling subprocess.check_output(input=None) with encoding or errors args (GH-97826)
* fix AttributeError, add unit test (cherry picked from commit db64fb9) Co-authored-by: andrei kulakov <andrei.avk@gmail.com>
1 parent 24908f1 commit ece5f7e

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
@@ -411,7 +411,8 @@ def check_output(*popenargs, timeout=None, **kwargs):
411411
if 'input' in kwargs and kwargs['input'] is None:
412412
# Explicitly passing input=None was previously equivalent to passing an
413413
# empty string. That is maintained here for backwards compatibility.
414-
if kwargs.get('universal_newlines') or kwargs.get('text'):
414+
if kwargs.get('universal_newlines') or kwargs.get('text') or kwargs.get('encoding') \
415+
or kwargs.get('errors'):
415416
empty = ''
416417
else:
417418
empty = b''

Lib/test/test_subprocess.py

+6
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ def test_check_output_input_none_universal_newlines(self):
227227
input=None, universal_newlines=True)
228228
self.assertNotIn('XX', output)
229229

230+
def test_check_output_input_none_encoding_errors(self):
231+
output = subprocess.check_output(
232+
[sys.executable, "-c", "print('foo')"],
233+
input=None, encoding='utf-8', errors='ignore')
234+
self.assertIn('foo', output)
235+
230236
def test_check_output_stdout_arg(self):
231237
# check_output() refuses to accept 'stdout' argument
232238
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)