Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Lib/compileall.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_compileall.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed issue in :func:`compileall.compile_file` when ``sys.stdout`` is redirected.
Patch by Stefan Hölzl.