]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-97825: fix AttributeError when calling subprocess.check_output(input=None) with...
authorandrei kulakov <andrei.avk@gmail.com>
Wed, 5 Oct 2022 00:47:49 +0000 (20:47 -0400)
committerGitHub <noreply@github.com>
Wed, 5 Oct 2022 00:47:49 +0000 (17:47 -0700)
* fix AttributeError, add unit test

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 7ae8df154b481f995444f1f88132181115592d18..760b93b47ebba673e3e50d958a998aa78ba0495d 100644 (file)
@@ -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''
index f6854922a5b87825469a8dc00eb6f0855c7baf04..424a4a93b6f9722c51f1ae2cb82aac639a489d97 100644 (file)
@@ -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 (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.