Issue #1729305: Fix doctest to handle encode error with "backslashreplace". It fixes #7667 too.
diff --git a/Lib/doctest.py b/Lib/doctest.py
index 41eccbe..726517e 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -218,11 +218,18 @@
return file_contents.replace(os.linesep, '\n'), filename
return open(filename).read(), filename
+# Use sys.stdout encoding for ouput.
+_encoding = getattr(sys.__stdout__, 'encoding', None) or 'utf-8'
+
def _indent(s, indent=4):
"""
- Add the given number of space characters to the beginning every
- non-blank line in `s`, and return the result.
+ Add the given number of space characters to the beginning of
+ every non-blank line in `s`, and return the result.
+ If the string `s` is Unicode, it is encoded using the stdout
+ encoding and the `backslashreplace` error handler.
"""
+ if isinstance(s, unicode):
+ s = s.encode(_encoding, 'backslashreplace')
# This regexp matches the start of non-blank lines:
return re.sub('(?m)^(?!$)', indent*' ', s)