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

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>
This commit is contained in:
Stefan Hoelzl 2021-07-30 18:38:42 +02:00 committed by GitHub
parent 7cad0bee80
commit 80f0707629
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 3 deletions

View 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