Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e94b221

Browse files
committed
Merged revisions 78493 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ................ r78493 | florent.xicluna | 2010-02-27 15:21:57 +0100 (sam, 27 fév 2010) | 11 lines For 3.x, the "backslashreplace" error handling is plugged on the "write" method. Recorded merge of revisions 78488 via svnmerge from svn+ssh://[email protected]/python/trunk ........ r78488 | florent.xicluna | 2010-02-27 14:31:23 +0100 (sam, 27 fév 2010) | 2 lines Issue #1729305: Fix doctest to handle encode error with "backslashreplace". It fixes #7667 too. ........ ................
1 parent 8f1c275 commit e94b221

3 files changed

Lines changed: 42 additions & 4 deletions

File tree

Lib/doctest.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ def _load_testfile(filename, package, module_relative, encoding):
218218

219219
def _indent(s, indent=4):
220220
"""
221-
Add the given number of space characters to the beginning every
222-
non-blank line in `s`, and return the result.
221+
Add the given number of space characters to the beginning of
222+
every non-blank line in `s`, and return the result.
223223
"""
224224
# This regexp matches the start of non-blank lines:
225225
return re.sub('(?m)^(?!$)', indent*' ', s)
@@ -1354,7 +1354,14 @@ def run(self, test, compileflags=None, out=None, clear_globs=True):
13541354

13551355
save_stdout = sys.stdout
13561356
if out is None:
1357-
out = save_stdout.write
1357+
encoding = save_stdout.encoding
1358+
if encoding is None or encoding.lower() == 'utf-8':
1359+
out = save_stdout.write
1360+
else:
1361+
# Use backslashreplace error handling on write
1362+
def out(s):
1363+
s = str(s.encode(encoding, 'backslashreplace'), encoding)
1364+
save_stdout.write(s)
13581365
sys.stdout = self._fakeout
13591366

13601367
# Patch pdb.set_trace to restore sys.stdout during interactive

Lib/test/test_doctest.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2147,6 +2147,13 @@ def test_testfile(): r"""
21472147
called with the name of a file, which is taken to be relative to the
21482148
calling module. The return value is (#failures, #tests).
21492149
2150+
We don't want `-v` in sys.argv for these tests.
2151+
2152+
>>> save_argv = sys.argv
2153+
>>> if '-v' in sys.argv:
2154+
... sys.argv = [arg for arg in save_argv if arg != '-v']
2155+
2156+
21502157
>>> doctest.testfile('test_doctest.txt') # doctest: +ELLIPSIS
21512158
**********************************************************************
21522159
File "...", line 6, in test_doctest.txt
@@ -2286,6 +2293,28 @@ def test_testfile(): r"""
22862293
>>> doctest.testfile('test_doctest4.txt', encoding='utf-8')
22872294
TestResults(failed=0, attempted=2)
22882295
>>> doctest.master = None # Reset master.
2296+
2297+
Test the verbose output:
2298+
2299+
>>> doctest.testfile('test_doctest4.txt', encoding='utf-8', verbose=True)
2300+
Trying:
2301+
'föö'
2302+
Expecting:
2303+
'f\xf6\xf6'
2304+
ok
2305+
Trying:
2306+
'bąr'
2307+
Expecting:
2308+
'b\u0105r'
2309+
ok
2310+
1 items passed all tests:
2311+
2 tests in test_doctest4.txt
2312+
2 tests in 1 items.
2313+
2 passed and 0 failed.
2314+
Test passed.
2315+
TestResults(failed=0, attempted=2)
2316+
>>> doctest.master = None # Reset master.
2317+
>>> sys.argv = save_argv
22892318
"""
22902319

22912320
def test_testmod(): r"""
@@ -2295,7 +2324,7 @@ def test_testmod(): r"""
22952324
out of the binary module.
22962325
22972326
>>> import unicodedata
2298-
>>> doctest.testmod(unicodedata)
2327+
>>> doctest.testmod(unicodedata, verbose=False)
22992328
TestResults(failed=0, attempted=0)
23002329
"""
23012330

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ Core and Builtins
9292
Library
9393
-------
9494

95+
- Issue #1729305: Fix doctest to handle encode error with "backslashreplace".
96+
9597
- Issue #691291: codecs.open() should not convert end of lines on reading and
9698
writing.
9799

0 commit comments

Comments
 (0)