[Python-checkins] r78992 - in python/branches/py3k: Lib/compileall.py Lib/test/test_compileall.py Misc/NEWS

martin.v.loewis python-checkins at python.org
Tue Mar 16 14:19:21 CET 2010


Author: martin.v.loewis
Date: Tue Mar 16 14:19:21 2010
New Revision: 78992

Log:
Issue #6716/2: Backslash-replace error output in compilall.


Modified:
   python/branches/py3k/Lib/compileall.py
   python/branches/py3k/Lib/test/test_compileall.py
   python/branches/py3k/Misc/NEWS

Modified: python/branches/py3k/Lib/compileall.py
==============================================================================
--- python/branches/py3k/Lib/compileall.py	(original)
+++ python/branches/py3k/Lib/compileall.py	Tue Mar 16 14:19:21 2010
@@ -104,7 +104,10 @@
                     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:

Modified: python/branches/py3k/Lib/test/test_compileall.py
==============================================================================
--- python/branches/py3k/Lib/test/test_compileall.py	(original)
+++ python/branches/py3k/Lib/test/test_compileall.py	Tue Mar 16 14:19:21 2010
@@ -1,3 +1,4 @@
+import sys
 import compileall
 import imp
 import os
@@ -7,6 +8,7 @@
 import tempfile
 from test import support
 import unittest
+import io
 
 
 class CompileallTests(unittest.TestCase):
@@ -72,8 +74,30 @@
         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__":

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Tue Mar 16 14:19:21 2010
@@ -283,6 +283,8 @@
 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.
 


More information about the Python-checkins mailing list