diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-07-30 17:12:05 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-30 17:12:05 (GMT) |
commit | 0db6c143ae5f04d223fdb2c94b820f16714b0a09 (patch) | |
tree | c25d7cd28da2d1a7eadbb0aba3c25f202d6aaf49 /Lib | |
parent | 6e6dc2517379289932c68fc986ee3994468374fc (diff) | |
download | cpython-0db6c143ae5f04d223fdb2c94b820f16714b0a09.zip cpython-0db6c143ae5f04d223fdb2c94b820f16714b0a09.tar.gz cpython-0db6c143ae5f04d223fdb2c94b820f16714b0a09.tar.bz2 |
bpo-44666: Use default encoding as fallback for compile_file (GH-27236) (GH-27488)
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)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/compileall.py | 5 | ||||
-rw-r--r-- | Lib/test/test_compileall.py | 8 |
2 files changed, 10 insertions, 3 deletions
diff --git a/Lib/compileall.py b/Lib/compileall.py index 61b4c5c..454465b 100644 --- a/Lib/compileall.py +++ b/Lib/compileall.py @@ -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 diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py index 96a0f8f..4612953 100644 --- a/Lib/test/test_compileall.py +++ b/Lib/test/test_compileall.py @@ -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 |