Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db19fb6

Browse files
author
Victor Stinner
committed
Merged revisions 81359-81361 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ........ r81359 | victor.stinner | 2010-05-19 19:00:07 +0200 (mer., 19 mai 2010) | 4 lines Issue #8663: distutils.log emulates backslashreplace error handler. Fix compilation in a non-ASCII directory if stdout encoding is ASCII (eg. if stdout is not a TTY). ........ r81360 | victor.stinner | 2010-05-19 19:11:19 +0200 (mer., 19 mai 2010) | 5 lines regrtest.py: call replace_stdout() before the first call to print() print("== ", os.getcwd()) fails if the current working directory is not ASCII whereas sys.stdout encoding is ASCII. ........ r81361 | victor.stinner | 2010-05-19 19:15:50 +0200 (mer., 19 mai 2010) | 2 lines Oops, add the new test_log.py for distutils test suite (missing part of r81359) ........
1 parent 20c043b commit db19fb6

4 files changed

Lines changed: 46 additions & 1 deletion

File tree

Lib/distutils/log.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ def _log(self, level, msg, args):
2727
stream = sys.stderr
2828
else:
2929
stream = sys.stdout
30+
if stream.errors == 'strict':
31+
# emulate backslashreplace error handler
32+
encoding = stream.encoding
33+
msg = msg.encode(encoding, "backslashreplace").decode(encoding)
3034
stream.write('%s\n' % msg)
3135
stream.flush()
3236

Lib/distutils/tests/test_log.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Tests for distutils.log"""
2+
3+
import sys
4+
import unittest
5+
from tempfile import NamedTemporaryFile
6+
7+
from distutils import log
8+
9+
class TestLog(unittest.TestCase):
10+
def test_non_ascii(self):
11+
# Issue #8663: test that non-ASCII text is escaped with
12+
# backslashreplace error handler (stream use ASCII encoding and strict
13+
# error handler)
14+
old_stdout = sys.stdout
15+
old_stderr = sys.stderr
16+
try:
17+
log.set_threshold(log.DEBUG)
18+
with NamedTemporaryFile(mode="w+", encoding='ascii') as stdout, \
19+
NamedTemporaryFile(mode="w+", encoding='ascii') as stderr:
20+
sys.stdout = stdout
21+
sys.stderr = stderr
22+
log.debug("debug:\xe9")
23+
log.fatal("fatal:\xe9")
24+
stdout.seek(0)
25+
self.assertEquals(stdout.read().rstrip(), "debug:\\xe9")
26+
stderr.seek(0)
27+
self.assertEquals(stderr.read().rstrip(), "fatal:\\xe9")
28+
finally:
29+
sys.stdout = old_stdout
30+
sys.stderr = old_stderr
31+
32+
def test_suite():
33+
return unittest.makeSuite(TestLog)
34+
35+
if __name__ == "__main__":
36+
unittest.main(defaultTest="test_suite")

Lib/test/regrtest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ def main(tests=None, testdir=None, verbose=0, quiet=False, generate=False,
215215
on the command line.
216216
"""
217217

218+
replace_stdout()
219+
218220
support.record_original_stdout(sys.stdout)
219221
try:
220222
opts, args = getopt.getopt(sys.argv[1:], 'hvgqxsSrf:lu:t:TD:NLR:wM:n',
@@ -411,7 +413,6 @@ def main(tests=None, testdir=None, verbose=0, quiet=False, generate=False,
411413
support.verbose = verbose # Tell tests to be moderately quiet
412414
support.use_resources = use_resources
413415
save_modules = sys.modules.keys()
414-
replace_stdout()
415416
for test in tests:
416417
if not quiet:
417418
print(test)

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ Core and Builtins
4343
Library
4444
-------
4545

46+
- Issue #8663: distutils.log emulates backslashreplace error handler. Fix
47+
compilation in a non-ASCII directory if stdout encoding is ASCII (eg. if
48+
stdout is not a TTY).
49+
4650
- Issue #8688: Distutils now recalculates MANIFEST everytime.
4751

4852
- Issue #5099: subprocess.Popen.__del__ no longer references global objects

0 commit comments

Comments
 (0)