summaryrefslogtreecommitdiffstats
path: root/Lib/doctest.py
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2008-06-11 18:37:52 (GMT)
committerGeorg Brandl <georg@python.org>2008-06-11 18:37:52 (GMT)
commit559e5d7f4d1155e95fb6f925c927a263f9196935 (patch)
tree4688423e81e9ffed7a5b2c87c50b55419e8e885a /Lib/doctest.py
parentea6d58d9d3033436b52e84960b9571525a4f5412 (diff)
downloadcpython-559e5d7f4d1155e95fb6f925c927a263f9196935.zip
cpython-559e5d7f4d1155e95fb6f925c927a263f9196935.tar.gz
cpython-559e5d7f4d1155e95fb6f925c927a263f9196935.tar.bz2
#2630: Implement PEP 3138.
The repr() of a string now contains printable Unicode characters unescaped. The new ascii() builtin can be used to get a repr() with only ASCII characters in it. PEP and patch were written by Atsuo Ishimoto.
Diffstat (limited to 'Lib/doctest.py')
-rw-r--r--Lib/doctest.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py
index 74be21e..476c718 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -1440,6 +1440,12 @@ class OutputChecker:
and returns true if they match; and `output_difference`, which
returns a string describing the differences between two outputs.
"""
+ def _toAscii(self, s):
+ """
+ Convert string to hex-escaped ASCII string.
+ """
+ return str(s.encode('ASCII', 'backslashreplace'), "ASCII")
+
def check_output(self, want, got, optionflags):
"""
Return True iff the actual output from an example (`got`)
@@ -1450,6 +1456,15 @@ class OutputChecker:
documentation for `TestRunner` for more information about
option flags.
"""
+
+ # If `want` contains hex-escaped character such as "\u1234",
+ # then `want` is a string of six characters(e.g. [\,u,1,2,3,4]).
+ # On the other hand, `got` could be an another sequence of
+ # characters such as [\u1234], so `want` and `got` should
+ # be folded to hex-escaped ASCII string to compare.
+ got = self._toAscii(got)
+ want = self._toAscii(want)
+
# Handle the common case first, for efficiency:
# if they're string-identical, always return true.
if got == want: