Index: Misc/NEWS =================================================================== --- Misc/NEWS (révision 81326) +++ Misc/NEWS (copie de travail) @@ -366,6 +366,10 @@ Library ------- +- 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). + - Issue #8513: os.get_exec_path() supports b'PATH' key and bytes value. subprocess.Popen() and os._execvpe() support bytes program name. Add os.supports_bytes_environ flag: True if the native OS type of the environment Index: Lib/distutils/tests/test_log.py =================================================================== --- Lib/distutils/tests/test_log.py (révision 0) +++ Lib/distutils/tests/test_log.py (révision 0) @@ -0,0 +1,37 @@ +"""Tests for distutils.log""" + +import sys +import unittest +from tempfile import NamedTemporaryFile + +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 + try: + log.set_threshold(log.DEBUG) + 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") + stdout.seek(0) + self.assertEquals(stdout.read().rstrip(), "debug:\\xe9") + stderr.seek(0) + self.assertEquals(stderr.read().rstrip(), "fatal:\\xe9") + finally: + sys.stdout = old_stdout + sys.stderr = old_stderr + +def test_suite(): + return unittest.makeSuite(TestLog) + +if __name__ == "__main__": + unittest.main(defaultTest="test_suite") + Index: Lib/distutils/log.py =================================================================== --- Lib/distutils/log.py (révision 81326) +++ Lib/distutils/log.py (copie de travail) @@ -27,6 +27,10 @@ stream = sys.stderr else: stream = sys.stdout + if stream.errors == 'strict': + # emulate backslashreplace error handler + encoding = stream.encoding + msg = msg.encode(encoding, "backslashreplace").decode(encoding) stream.write('%s\n' % msg) stream.flush()