diff options
author | Florent Xicluna <florent.xicluna@gmail.com> | 2010-02-27 13:31:23 +0000 |
---|---|---|
committer | Florent Xicluna <florent.xicluna@gmail.com> | 2010-02-27 13:31:23 +0000 |
commit | b666b12a66bcb54ed2ef43b2b4e5c63f0da7f540 (patch) | |
tree | 49b9d1af3a6bc19739820744dce873c072066581 /Lib/doctest.py | |
parent | a5b70e3ac5f27053959690441867a1c9a6d8783c (diff) | |
download | cpython-b666b12a66bcb54ed2ef43b2b4e5c63f0da7f540.tar.gz |
Issue #1729305: Fix doctest to handle encode error with "backslashreplace". It fixes #7667 too.
Diffstat (limited to 'Lib/doctest.py')
-rw-r--r-- | Lib/doctest.py | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py index 41eccbe764..726517e6ca 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -218,11 +218,18 @@ def _load_testfile(filename, package, module_relative): 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) |