Mailing List Archive

bpo-44666: Use default encoding as fallback for compile_file (GH-27236) (GH-27489)
https://github.com/python/cpython/commit/73240d425b770c26d9424665259cd9a2f339b626
commit: 73240d425b770c26d9424665259cd9a2f339b626
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
committer: ambv <lukasz@langa.pl>
date: 2021-07-30T19:12:29+02:00
summary:

bpo-44666: Use default encoding as fallback for compile_file (GH-27236) (GH-27489)

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>
(cherry picked from commit 80f07076294bc09a55ed76d9bbf307404eef25e6)

files:
A Misc/NEWS.d/next/Library/2021-07-21-10-43-22.bpo-44666.CEThkv.rst
M Lib/compileall.py
M Lib/test/test_compileall.py
M Misc/ACKS

diff --git a/Lib/compileall.py b/Lib/compileall.py
index fe7f450c55e1c..33fb76c541544 100644
--- a/Lib/compileall.py
+++ b/Lib/compileall.py
@@ -252,9 +252,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
diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py
index 3bbc6817f8d56..8df705666c092 100644
--- a/Lib/test/test_compileall.py
+++ b/Lib/test/test_compileall.py
@@ -164,6 +164,14 @@ def test_no_pycache_in_non_package(self):
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
diff --git a/Misc/ACKS b/Misc/ACKS
index f01cc5d970bb7..fc5602d39b65a 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -781,6 +781,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
index 0000000000000..ab2ef22d0c455
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-07-21-10-43-22.bpo-44666.CEThkv.rst
@@ -0,0 +1,2 @@
+Fixed issue in :func:`compileall.compile_file` when ``sys.stdout`` is redirected.
+Patch by Stefan Hölzl.

_______________________________________________
Python-checkins mailing list
Python-checkins@python.org
https://mail.python.org/mailman/listinfo/python-checkins