]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-97825: fix AttributeError when calling subprocess.check_output(input=None) with...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 5 Oct 2022 03:35:53 +0000 (20:35 -0700)
committerGitHub <noreply@github.com>
Wed, 5 Oct 2022 03:35:53 +0000 (20:35 -0700)
* fix AttributeError, add unit test
(cherry picked from commit db64fb9bbe92b212db7dd173f787ea3607ae971a)

Co-authored-by: andrei kulakov <andrei.avk@gmail.com>
Lib/subprocess.py
Lib/test/test_subprocess.py
Misc/NEWS.d/next/Library/2022-10-04-07-55-19.gh-issue-97825.mNdv1l.rst [new file with mode: 0644]

index a414321b9d197e2fb4bc2e0f1aae2998ed1890a1..e5d7f0981861413710aea048e8bfe5230cca22d7 100644 (file)
@@ -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''
index b91791a02a2e52f9870c7cbcd026c1d027f35b38..ea02a9ecb67af5ee8a7967421285340ce8e00e18 100644 (file)
@@ -227,6 +227,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 (file)
index 0000000..4633dce
--- /dev/null
@@ -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.