From e20d2f68a138f3a8ae61d521e9b1478e92a97424 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Sat, 9 Feb 2013 12:55:06 -0500 Subject: [PATCH] Escape non-ascii bytes in tracebacks line-by-line instead of all at once. 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 | 8 ++++++-- tornado/test/log_test.py | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/tornado/log.py b/tornado/log.py index 9ee40ca3b..fa11f3795 100644 --- a/tornado/log.py +++ b/tornado/log.py @@ -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 ") diff --git a/tornado/test/log_test.py b/tornado/test/log_test.py index da9e3fc48..f0e1f50ab 100644 --- a/tornado/test/log_test.py +++ b/tornado/test/log_test.py @@ -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): -- 2.47.3