8000 [3.7] bpo-34421: Improve distutils logging for non-ASCII strings. (GH-9126) by serhiy-storchaka · Pull Request #9506 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.7] bpo-34421: Improve distutils logging for non-ASCII strings. (GH-9126) #9506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Lib/distutils/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ def _log(self, level, msg, args):
stream = sys.stderr
else:
stream = sys.stdout
if stream.errors == 'strict':
try:
stream.write('%s\n' % msg)
except UnicodeEncodeError:
# emulate backslashreplace error handler
encoding = stream.encoding
msg = msg.encode(encoding, "backslashreplace").decode(encoding)
try:
stream.write('%s\n' % msg)
except UnicodeEncodeError:
stream.write('%s\n' % msg.encode('unicode-escape').decode('ascii'))
stream.flush()

def log(self, level, msg, *args):
Expand Down
50 changes: 29 additions & 21 deletions Lib/distutils/tests/test_log.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
"""Tests for distutils.log"""

import io
import sys
import unittest
from tempfile import NamedTemporaryFile
from test.support import run_unittest
from test.support import swap_attr, run_unittest

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
old_threshold = log.set_threshold(log.DEBUG)
try:
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")
# Issues #8663, #34421: test that non-encodable text is escaped with
# backslashreplace error handler and encodable non-ASCII text is
# output as is.
for errors in ('strict', 'backslashreplace', 'surrogateescape',
'replace', 'ignore'):
with self.subTest(errors=errors):
stdout = io.TextIOWrapper(io.BytesIO(),
encoding='cp437', errors=errors)
stderr = io.TextIOWrapper(io.BytesIO(),
encoding='cp437', errors=errors)
old_threshold = log.set_threshold(log.DEBUG)
try:
with swap_attr(sys, 'stdout', stdout), \
swap_attr(sys, 'stderr', stderr):
log.debug('Dεbug\tMėssãge')
log.fatal('Fαtal\tÈrrōr')
finally:
log.set_threshold(old_threshold)

stdout.seek(0)
self.assertEqual(stdout.read().rstrip(), "debug:\\xe9")
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(), "fatal:\\xe9")
finally:
log.set_threshold(old_threshold)
sys.stdout = old_stdout
sys.stderr = old_stderr
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():
return unittest.makeSuite(TestLog)
Expand Down
0