]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Escape non-ascii bytes in tracebacks line-by-line instead of all at once.
authorBen Darnell <ben@bendarnell.com>
Sat, 9 Feb 2013 17:55:06 +0000 (12:55 -0500)
committerBen Darnell <ben@bendarnell.com>
Sat, 9 Feb 2013 18:00:11 +0000 (13:00 -0500)
Previously if there were any bytes that needed escaping, the newlines
would get escaped as well, ruining the message formatting.

Closes #675 (again)

tornado/log.py
tornado/test/log_test.py

index 9ee40ca3b6cbca47007d4b7acc7978e427239a59..fa11f37953598f9dd442344546a208e3a5650760 100644 (file)
@@ -142,8 +142,12 @@ class LogFormatter(logging.Formatter):
             if not record.exc_text:
                 record.exc_text = self.formatException(record.exc_info)
         if record.exc_text:
-            formatted = (formatted.rstrip() + "\n" +
-                         safe_unicode(record.exc_text))
+            # exc_text contains multiple lines.  We need to safe_unicode
+            # each line separately so that non-utf8 bytes don't cause
+            # all the newlines to turn into '\n'.
+            lines = [formatted.rstrip()]
+            lines.extend(safe_unicode(ln) for ln in record.exc_text.split('\n'))
+            formatted = '\n'.join(lines)
         return formatted.replace("\n", "\n    ")
 
 
index da9e3fc48da7b8d9d967f7048adabd9b1178ae5d..f0e1f50ab300c02c2ff1d3c6cfaf0872aff8f8e2 100644 (file)
@@ -112,7 +112,10 @@ class LogFormatterTest(unittest.TestCase):
             self.logger.exception('caught exception')
         # This will be "Exception: \xe9" on python 2 or
         # "Exception: b'\xe9'" on python 3.
-        self.assertRegexpMatches(self.get_output(), br'Exception.*\\xe9')
+        output = self.get_output()
+        self.assertRegexpMatches(output, br'Exception.*\\xe9')
+        # The traceback contains newlines, which should not have been escaped.
+        self.assertNotIn(br'\n', output)
 
 
 class UnicodeLogFormatterTest(LogFormatterTest):