Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4b00307

Browse files
committed
Issue #6716/2: Backslash-replace error output in compilall.
1 parent 09c86af commit 4b00307

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

Lib/compileall.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ def compile_file(fullname, ddir=None, force=0, rx=None, quiet=0):
104104
print('*** Error compiling', fullname, '...')
105105
else:
106106
print('*** ', end='')
107-
print(err.msg)
107+
# escape non-printable characters in msg
108+
msg = err.msg.encode(sys.stdout.encoding, errors='backslashreplace')
109+
msg = msg.decode(sys.stdout.encoding)
110+
print(msg)
108111
success = 0
109112
except (SyntaxError, UnicodeError, IOError) as e:
110113
if quiet:

Lib/test/test_compileall.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import compileall
23
import imp
34
import os
@@ -7,6 +8,7 @@
78
import tempfile
89
from test import support
910
import unittest
11+
import io
1012

1113

1214
class CompileallTests(unittest.TestCase):
@@ -72,8 +74,30 @@ def test_compile_files(self):
7274
os.unlink(self.bc_path)
7375
os.unlink(self.bc_path2)
7476

77+
class EncodingTest(unittest.TestCase):
78+
'Issue 6716: compileall should escape source code when printing errors to stdout.'
79+
80+
def setUp(self):
81+
self.directory = tempfile.mkdtemp()
82+
self.source_path = os.path.join(self.directory, '_test.py')
83+
with open(self.source_path, 'w', encoding='utf-8') as file:
84+
file.write('# -*- coding: utf-8 -*-\n')
85+
file.write('print u"\u20ac"\n')
86+
87+
def tearDown(self):
88+
shutil.rmtree(self.directory)
89+
90+
def test_error(self):
91+
try:
92+
orig_stdout = sys.stdout
93+
sys.stdout = io.TextIOWrapper(io.BytesIO(),encoding='ascii')
94+
compileall.compile_dir(self.directory)
95+
finally:
96+
sys.stdout = orig_stdout
97+
7598
def test_main():
76-
support.run_unittest(CompileallTests)
99+
support.run_unittest(CompileallTests,
100+
EncodingTest)
77101

78102

79103
if __name__ == "__main__":

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ C-API
283283
Library
284284
-------
285285

286+
- Issue #6716/2: Backslash-replace error output in compilall.
287+
286288
- Issue #4961: Inconsistent/wrong result of askyesno function in tkMessageBox
287289
with Tcl/Tk-8.5.
288290

0 commit comments

Comments
 (0)