]> granicus.if.org Git - python/commitdiff
Merged revisions 81359-81361 via svnmerge from
authorVictor Stinner <victor.stinner@haypocalc.com>
Wed, 19 May 2010 20:30:19 +0000 (20:30 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Wed, 19 May 2010 20:30:19 +0000 (20:30 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r81359 | victor.stinner | 2010-05-19 19:00:07 +0200 (mer., 19 mai 2010) | 4 lines

  Issue #8663: distutils.log emulates backslashreplace error handler. Fix
  compilation in a non-ASCII directory if stdout encoding is ASCII (eg. if stdout
  is not a TTY).
........
  r81360 | victor.stinner | 2010-05-19 19:11:19 +0200 (mer., 19 mai 2010) | 5 lines

  regrtest.py: call replace_stdout() before the first call to print()

  print("==  ", os.getcwd()) fails if the current working directory is not ASCII
  whereas sys.stdout encoding is ASCII.
........
  r81361 | victor.stinner | 2010-05-19 19:15:50 +0200 (mer., 19 mai 2010) | 2 lines

  Oops, add the new test_log.py for distutils test suite (missing part of r81359)
........

Lib/distutils/log.py
Lib/distutils/tests/test_log.py [new file with mode: 0644]
Lib/test/regrtest.py
Misc/NEWS

index 758857081c80e13ec772aea668323961478a752b..b301a8338c20dbbf7dee4b92b7b7ede449991ef0 100644 (file)
@@ -27,6 +27,10 @@ class Log:
                 stream = sys.stderr
             else:
                 stream = sys.stdout
+            if stream.errors == 'strict':
+                # emulate backslashreplace error handler
+                encoding = stream.encoding
+                msg = msg.encode(encoding, "backslashreplace").decode(encoding)
             stream.write('%s\n' % msg)
             stream.flush()
 
diff --git a/Lib/distutils/tests/test_log.py b/Lib/distutils/tests/test_log.py
new file mode 100644 (file)
index 0000000..d35de34
--- /dev/null
@@ -0,0 +1,36 @@
+"""Tests for distutils.log"""
+
+import sys
+import unittest
+from tempfile import NamedTemporaryFile
+
+from distutils import log
+
+class TestLog(unittest.TestCase):
+    def test_non_ascii(self):
+        # Issue #8663: test that non-ASCII text is escaped with
+        # backslashreplace error handler (stream use ASCII encoding and strict
+        # error handler)
+        old_stdout = sys.stdout
+        old_stderr = sys.stderr
+        try:
+            log.set_threshold(log.DEBUG)
+            with NamedTemporaryFile(mode="w+", encoding='ascii') as stdout, \
+                 NamedTemporaryFile(mode="w+", encoding='ascii') as stderr:
+                sys.stdout = stdout
+                sys.stderr = stderr
+                log.debug("debug:\xe9")
+                log.fatal("fatal:\xe9")
+                stdout.seek(0)
+                self.assertEquals(stdout.read().rstrip(), "debug:\\xe9")
+                stderr.seek(0)
+                self.assertEquals(stderr.read().rstrip(), "fatal:\\xe9")
+        finally:
+            sys.stdout = old_stdout
+            sys.stderr = old_stderr
+
+def test_suite():
+    return unittest.makeSuite(TestLog)
+
+if __name__ == "__main__":
+    unittest.main(defaultTest="test_suite")
index 30792c2f6641595ae5e635335ec1489ec3269a5c..c2c44e5c841570deaaff94b41ac58d91d2b67c4d 100755 (executable)
@@ -215,6 +215,8 @@ def main(tests=None, testdir=None, verbose=0, quiet=False, generate=False,
     on the command line.
     """
 
+    replace_stdout()
+
     support.record_original_stdout(sys.stdout)
     try:
         opts, args = getopt.getopt(sys.argv[1:], 'hvgqxsSrf:lu:t:TD:NLR:wM:n',
@@ -411,7 +413,6 @@ def main(tests=None, testdir=None, verbose=0, quiet=False, generate=False,
     support.verbose = verbose      # Tell tests to be moderately quiet
     support.use_resources = use_resources
     save_modules = sys.modules.keys()
-    replace_stdout()
     for test in tests:
         if not quiet:
             print(test)
index 56684d409ea18feeb5a7416e5517e9d3df26d694..d7db9a26c23e4d750912cb2d5ecd30d3958e4904 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -43,6 +43,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #8663: distutils.log emulates backslashreplace error handler. Fix
+  compilation in a non-ASCII directory if stdout encoding is ASCII (eg. if
+  stdout is not a TTY).
+
 - Issue #8688: Distutils now recalculates MANIFEST everytime.
 
 - Issue #5099: subprocess.Popen.__del__ no longer references global objects