aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorandrei kulakov <andrei.avk@gmail.com>2022-10-04 20:47:49 -0400
committerGitHub <noreply@github.com>2022-10-04 17:47:49 -0700
commitdb64fb9bbe92b212db7dd173f787ea3607ae971a (patch)
tree792388cef007d2b5d95435f9d1afc3f2592009fc
parent0ceafa7fa408b64377ea31dd5386152da19ef38a (diff)
downloadcpython-db64fb9bbe92b212db7dd173f787ea3607ae971a.tar.gz
cpython-db64fb9bbe92b212db7dd173f787ea3607ae971a.zip
gh-97825: fix AttributeError when calling subprocess.check_output(input=None) with encoding or errors args (#97826)
* fix AttributeError, add unit test
-rw-r--r--Lib/subprocess.py3
-rw-r--r--Lib/test/test_subprocess.py6
-rw-r--r--Misc/NEWS.d/next/Library/2022-10-04-07-55-19.gh-issue-97825.mNdv1l.rst1
3 files changed, 9 insertions, 1 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index 7ae8df154b4..760b93b47eb 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -456,7 +456,8 @@ 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.
- if kwargs.get('universal_newlines') or kwargs.get('text'):
+ if kwargs.get('universal_newlines') or kwargs.get('text') or kwargs.get('encoding') \
+ or kwargs.get('errors'):
empty = ''
else:
empty = b''
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py
index f6854922a5b..424a4a93b6f 100644
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -238,6 +238,12 @@ class ProcessTestCase(BaseTestCase):
input=None, universal_newlines=True)
self.assertNotIn('XX', output)
+ def test_check_output_input_none_encoding_errors(self):
+ output = subprocess.check_output(
+ [sys.executable, "-c", "print('foo')"],
+ input=None, encoding='utf-8', errors='ignore')
+ self.assertIn('foo', output)
+
def test_check_output_stdout_arg(self):
# check_output() refuses to accept 'stdout' argument
with self.assertRaises(ValueError) as c:
diff --git a/Misc/NEWS.d/next/Library/2022-10-04-07-55-19.gh-issue-97825.mNdv1l.rst b/Misc/NEWS.d/next/Library/2022-10-04-07-55-19.gh-issue-97825.mNdv1l.rst
new file mode 100644
index 00000000000..4633dce7b66
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-10-04-07-55-19.gh-issue-97825.mNdv1l.rst
@@ -0,0 +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.