Commit 4b860fd7 authored by Serhiy Storchaka's avatar Serhiy Storchaka Committed by GitHub

bpo-34421: Improve distutils logging for non-ASCII strings. (GH-9126)

Use "backslashreplace" instead of "unicode-escape".  It is not
implementation depended and escapes only non-encodable characters.

Also simplify the code.
parent 8fabae3b
...@@ -27,14 +27,13 @@ class Log: ...@@ -27,14 +27,13 @@ class Log:
stream = sys.stderr stream = sys.stderr
else: else:
stream = sys.stdout stream = sys.stdout
if stream.errors == 'strict': try:
stream.write('%s\n' % msg)
except UnicodeEncodeError:
# emulate backslashreplace error handler # emulate backslashreplace error handler
encoding = stream.encoding encoding = stream.encoding
msg = msg.encode(encoding, "backslashreplace").decode(encoding) msg = msg.encode(encoding, "backslashreplace").decode(encoding)
try:
stream.write('%s\n' % msg) stream.write('%s\n' % msg)
except UnicodeEncodeError:
stream.write('%s\n' % msg.encode('unicode-escape').decode('ascii'))
stream.flush() stream.flush()
def log(self, level, msg, *args): def log(self, level, msg, *args):
......
...@@ -3,33 +3,39 @@ ...@@ -3,33 +3,39 @@
import sys import sys
import unittest import unittest
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
from test.support import run_unittest from test.support import swap_attr, run_unittest
from distutils import log from distutils import log
class TestLog(unittest.TestCase): class TestLog(unittest.TestCase):
def test_non_ascii(self): def test_non_ascii(self):
# Issue #8663: test that non-ASCII text is escaped with # Issues #8663, #34421: test that non-encodable text is escaped with
# backslashreplace error handler (stream use ASCII encoding and strict # backslashreplace error handler and encodable non-ASCII text is
# error handler) # output as is.
old_stdout = sys.stdout for errors in ('strict', 'backslashreplace', 'surrogateescape',
old_stderr = sys.stderr 'replace', 'ignore'):
with self.subTest(errors=errors), \
NamedTemporaryFile("w+", encoding='cp437', errors=errors) as stdout, \
NamedTemporaryFile("w+", encoding='cp437', errors=errors) as stderr:
old_threshold = log.set_threshold(log.DEBUG) old_threshold = log.set_threshold(log.DEBUG)
try: try:
with NamedTemporaryFile(mode="w+", encoding='ascii') as stdout, \ with swap_attr(sys, 'stdout', stdout), \
NamedTemporaryFile(mode="w+", encoding='ascii') as stderr: swap_attr(sys, 'stderr', stderr):
sys.stdout = stdout log.debug('Dεbug\tMėssãge')
sys.stderr = stderr log.fatal('Fαtal\tÈrrōr')
log.debug("debug:\xe9")
log.fatal("fatal:\xe9")
stdout.seek(0)
self.assertEqual(stdout.read().rstrip(), "debug:\\xe9")
stderr.seek(0)
self.assertEqual(stderr.read().rstrip(), "fatal:\\xe9")
finally: finally:
log.set_threshold(old_threshold) log.set_threshold(old_threshold)
sys.stdout = old_stdout
sys.stderr = old_stderr stdout.seek(0)
self.assertEqual(stdout.read().rstrip(),
'Dεbug\tM?ss?ge' if errors == 'replace' else
'Dεbug\tMssge' if errors == 'ignore' else
'Dεbug\tM\\u0117ss\\xe3ge')
stderr.seek(0)
self.assertEqual(stderr.read().rstrip(),
'Fαtal\t?rr?r' if errors == 'replace' else
'Fαtal\trrr' if errors == 'ignore' else
'Fαtal\t\\xc8rr\\u014dr')
def test_suite(): def test_suite():
return unittest.makeSuite(TestLog) return unittest.makeSuite(TestLog)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment