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

Skip to content

Commit 5925085

Browse files
committed
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 e33721e commit 5925085

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
@@ -2149,6 +2149,13 @@ def test_testfile(): r"""
21492149
called with the name of a file, which is taken to be relative to the
21502150
calling module. The return value is (#failures, #tests).
21512151
2152+
We don't want `-v` in sys.argv for these tests.
2153+
2154+
>>> save_argv = sys.argv
2155+
>>> if '-v' in sys.argv:
2156+
... sys.argv = [arg for arg in save_argv if arg != '-v']
2157+
2158+
21522159
>>> doctest.testfile('test_doctest.txt') # doctest: +ELLIPSIS
21532160
**********************************************************************
21542161
File "...", line 6, in test_doctest.txt
@@ -2288,6 +2295,28 @@ def test_testfile(): r"""
22882295
>>> doctest.testfile('test_doctest4.txt', encoding='utf-8')
22892296
TestResults(failed=0, attempted=2)
22902297
>>> doctest.master = None # Reset master.
2298+
2299+
Test the verbose output:
2300+
2301+
>>> doctest.testfile('test_doctest4.txt', encoding='utf-8', verbose=True)
2302+
Trying:
2303+
'föö'
2304+
Expecting:
2305+
'f\xf6\xf6'
2306+
ok
2307+
Trying:
2308+
'bąr'
2309+
Expecting:
2310+
'b\u0105r'
2311+
ok
2312+
1 items passed all tests:
2313+
2 tests in test_doctest4.txt
2314+
2 tests in 1 items.
2315+
2 passed and 0 failed.
2316+
Test passed.
2317+
TestResults(failed=0, attempted=2)
2318+
>>> doctest.master = None # Reset master.
2319+
>>> sys.argv = save_argv
22912320
"""
22922321

22932322
def test_testmod(): r"""
@@ -2297,7 +2326,7 @@ def test_testmod(): r"""
22972326
out of the binary module.
22982327
22992328
>>> import unicodedata
2300-
>>> doctest.testmod(unicodedata)
2329+
>>> doctest.testmod(unicodedata, verbose=False)
23012330
TestResults(failed=0, attempted=0)
23022331
"""
23032332

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ C-API
260260
Library
261261
-------
262262

263+
- Issue #1729305: Fix doctest to handle encode error with "backslashreplace".
264+
263265
- Issue #691291: codecs.open() should not convert end of lines on reading and
264266
writing.
265267

0 commit comments

Comments
 (0)