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

Skip to content

bpo-34421: Improve distutils logging for non-ASCII strings. #9126

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
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
46 changes: 26 additions & 20 deletions Lib/distutils/tests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,39 @@
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), \
NamedTemporaryFile("w+", encoding='cp437', errors=errors) as stdout, \
NamedTemporaryFile("w+", encoding='cp437', errors=errors) as stderr:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use 8 spaces instead of 5 here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines are already too long. I don't want neither make them longer, nor split.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it fit with 6 spaces? I just feel a little uneasy when there is only one space difference between two indent levels.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it doesn't fit even with 0 spaces. And I don't want to use negative indentation.

5 space indentation for with is acceptable by PEP 8.

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