]> granicus.if.org Git - python/commitdiff
Issue #6716/2: Backslash-replace error output in compilall.
authorMartin v. Löwis <martin@v.loewis.de>
Tue, 16 Mar 2010 13:19:21 +0000 (13:19 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Tue, 16 Mar 2010 13:19:21 +0000 (13:19 +0000)
Lib/compileall.py
Lib/test/test_compileall.py
Misc/NEWS

index c3bf25a5db05e4634c9c9471b1e5c43b3bb22cb0..eb5e24b31843745441a4456968e7d1c2ca28867f 100644 (file)
@@ -104,7 +104,10 @@ def compile_file(fullname, ddir=None, force=0, rx=None, quiet=0):
                     print('*** Error compiling', fullname, '...')
                 else:
                     print('*** ', end='')
-                print(err.msg)
+                # escape non-printable characters in msg
+                msg = err.msg.encode(sys.stdout.encoding, errors='backslashreplace')
+                msg = msg.decode(sys.stdout.encoding)
+                print(msg)
                 success = 0
             except (SyntaxError, UnicodeError, IOError) as e:
                 if quiet:
index 65622f712a5c6b1b310843c1ac148750d218ce46..4b6feba24eee5c25c583ae7f1e6c3602b4b1c978 100644 (file)
@@ -1,3 +1,4 @@
+import sys
 import compileall
 import imp
 import os
@@ -7,6 +8,7 @@ import struct
 import tempfile
 from test import support
 import unittest
+import io
 
 
 class CompileallTests(unittest.TestCase):
@@ -72,8 +74,30 @@ class CompileallTests(unittest.TestCase):
         os.unlink(self.bc_path)
         os.unlink(self.bc_path2)
 
+class EncodingTest(unittest.TestCase):
+    'Issue 6716: compileall should escape source code when printing errors to stdout.'
+
+    def setUp(self):
+        self.directory = tempfile.mkdtemp()
+        self.source_path = os.path.join(self.directory, '_test.py')
+        with open(self.source_path, 'w', encoding='utf-8') as file:
+            file.write('# -*- coding: utf-8 -*-\n')
+            file.write('print u"\u20ac"\n')
+
+    def tearDown(self):
+        shutil.rmtree(self.directory)
+
+    def test_error(self):
+        try:
+            orig_stdout = sys.stdout
+            sys.stdout = io.TextIOWrapper(io.BytesIO(),encoding='ascii')
+            compileall.compile_dir(self.directory)
+        finally:
+            sys.stdout = orig_stdout
+
 def test_main():
-    support.run_unittest(CompileallTests)
+    support.run_unittest(CompileallTests,
+                         EncodingTest)
 
 
 if __name__ == "__main__":
index 93a991be1198d2efbf1fd498d6130850071cd0e7..018adf07b0420a9fd947aca20225ca9b6c030fbf 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -283,6 +283,8 @@ C-API
 Library
 -------
 
+- Issue #6716/2: Backslash-replace error output in compilall.
+
 - Issue #4961: Inconsistent/wrong result of askyesno function in tkMessageBox
   with Tcl/Tk-8.5.