summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/distutils/log.py4
-rw-r--r--Lib/distutils/tests/test_log.py36
-rwxr-xr-xLib/test/regrtest.py3
3 files changed, 42 insertions, 1 deletions
diff --git a/Lib/distutils/log.py b/Lib/distutils/log.py
index 7588570..b301a83 100644
--- a/Lib/distutils/log.py
+++ b/Lib/distutils/log.py
@@ -27,6 +27,10 @@ class Log:
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()
diff --git a/Lib/distutils/tests/test_log.py b/Lib/distutils/tests/test_log.py
new file mode 100644
index 0000000..d35de34
--- /dev/null
+++ b/Lib/distutils/tests/test_log.py
@@ -0,0 +1,36 @@
+"""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")
diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py
index 30792c2..c2c44e5 100755
--- a/Lib/test/regrtest.py
+++ b/Lib/test/regrtest.py
@@ -215,6 +215,8 @@ def main(tests=None, testdir=None, verbose=0, quiet=False, generate=False,
on the command line.
"""
+ replace_stdout()
+
support.record_original_stdout(sys.stdout)
try:
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,
support.verbose = verbose # Tell tests to be moderately quiet
support.use_resources = use_resources
save_modules = sys.modules.keys()
- replace_stdout()
for test in tests:
if not quiet:
print(test)