[Python-checkins] gh-97825: fix AttributeError when calling subprocess.check_output(input=None) with encoding or errors args (GH-97826)

miss-islington webhook-mailer at python.org
Tue Oct 4 23:35:58 EDT 2022


https://github.com/python/cpython/commit/ece5f7e04600150712d88b928b76d0f2de5f5011
commit: ece5f7e04600150712d88b928b76d0f2de5f5011
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-10-04T20:35:53-07:00
summary:

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 db64fb9bbe92b212db7dd173f787ea3607ae971a)

Co-authored-by: andrei kulakov <andrei.avk at gmail.com>

files:
A Misc/NEWS.d/next/Library/2022-10-04-07-55-19.gh-issue-97825.mNdv1l.rst
M Lib/subprocess.py
M Lib/test/test_subprocess.py

diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index a414321b9d19..e5d7f0981861 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -411,7 +411,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 b91791a02a2e..ea02a9ecb67a 100644
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -227,6 +227,12 @@ def test_check_output_input_none_universal_newlines(self):
                 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 000000000000..4633dce7b663
--- /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.



More information about the Python-checkins mailing list