]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44666: Use default encoding as fallback for compile_file (GH-27236)
authorStefan Hoelzl <1478183+stefanhoelzl@users.noreply.github.com>
Fri, 30 Jul 2021 16:38:42 +0000 (18:38 +0200)
committerGitHub <noreply@github.com>
Fri, 30 Jul 2021 16:38:42 +0000 (18:38 +0200)
When sys.stdout.encoding is None compile_file will fall back to
sys.getdefaultencoding to encode/decode error messages.

Co-authored-by: Stefan Hoelzl <stefan.hoelzl@posteo.de>
Co-authored-by: Mickaël Schoentgen <contact@tiger-222.fr>
Lib/compileall.py
Lib/test/test_compileall.py
Misc/ACKS
Misc/NEWS.d/next/Library/2021-07-21-10-43-22.bpo-44666.CEThkv.rst [new file with mode: 0644]

index 61b4c5c0af1ad68e69e550586b4c73781ac1e757..454465bd8fc4ef8a00b384e5a36908997107936d 100644 (file)
@@ -254,9 +254,8 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
                 else:
                     print('*** ', end='')
                 # escape non-printable characters in msg
-                msg = err.msg.encode(sys.stdout.encoding,
-                                     errors='backslashreplace')
-                msg = msg.decode(sys.stdout.encoding)
+                encoding = sys.stdout.encoding or sys.getdefaultencoding()
+                msg = err.msg.encode(encoding, errors='backslashreplace').decode(encoding)
                 print(msg)
             except (SyntaxError, UnicodeError, OSError) as e:
                 success = False
index 96a0f8f729aa4eba84a9b1df89380971f668d079..4612953cf8df39e258edb19e45c30cee7139e7e1 100644 (file)
@@ -169,6 +169,14 @@ class CompileallTestsBase:
         compileall.compile_file(data_file)
         self.assertFalse(os.path.exists(os.path.join(data_dir, '__pycache__')))
 
+
+    def test_compile_file_encoding_fallback(self):
+        # Bug 44666 reported that compile_file failed when sys.stdout.encoding is None
+        self.add_bad_source_file()
+        with contextlib.redirect_stdout(io.StringIO()):
+            self.assertFalse(compileall.compile_file(self.bad_source_path))
+
+
     def test_optimize(self):
         # make sure compiling with different optimization settings than the
         # interpreter's creates the correct file names
index 87de95b938c20e9e791ed37627a74f34811585e8..ac10be97ae3d9c5d3bdc63dfd76ffac3791c3994 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -794,6 +794,7 @@ Fredrik Håård
 Florian Höch
 Oleg Höfling
 Robert Hölzl
+Stefan Hölzl
 Catalin Iacob
 Mihai Ibanescu
 Ali Ikinci
diff --git a/Misc/NEWS.d/next/Library/2021-07-21-10-43-22.bpo-44666.CEThkv.rst b/Misc/NEWS.d/next/Library/2021-07-21-10-43-22.bpo-44666.CEThkv.rst
new file mode 100644 (file)
index 0000000..ab2ef22
--- /dev/null
@@ -0,0 +1,2 @@
+Fixed issue in :func:`compileall.compile_file` when ``sys.stdout`` is redirected.
+Patch by Stefan Hölzl.