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

Skip to content

Commit 58641de

Browse files
committed
Issue #6195: fix doctest to no longer try to read 'source' data from
binary files.
1 parent 554290d commit 58641de

3 files changed

Lines changed: 34 additions & 12 deletions

File tree

Lib/doctest.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -812,20 +812,28 @@ def find(self, obj, name=None, module=None, globs=None, extraglobs=None):
812812
# DocTestFinder._find_lineno to find the line number for a
813813
# given object's docstring.
814814
try:
815-
file = inspect.getsourcefile(obj) or inspect.getfile(obj)
816-
if module is not None:
817-
# Supply the module globals in case the module was
818-
# originally loaded via a PEP 302 loader and
819-
# file is not a valid filesystem path
820-
source_lines = linecache.getlines(file, module.__dict__)
821-
else:
822-
# No access to a loader, so assume it's a normal
823-
# filesystem path
824-
source_lines = linecache.getlines(file)
825-
if not source_lines:
826-
source_lines = None
815+
file = inspect.getsourcefile(obj)
827816
except TypeError:
828817
source_lines = None
818+
else:
819+
if not file:
820+
# Check to see if it's one of our special internal "files"
821+
# (see __patched_linecache_getlines).
822+
file = inspect.getfile(obj)
823+
if not file[0]+file[-2:] == '<]>': file = None
824+
if file is None: source_lines = None
825+
else:
826+
if module is not None:
827+
# Supply the module globals in case the module was
828+
# originally loaded via a PEP 302 loader and
829+
# file is not a valid filesystem path
830+
source_lines = linecache.getlines(file, module.__dict__)
831+
else:
832+
# No access to a loader, so assume it's a normal
833+
# filesystem path
834+
source_lines = linecache.getlines(file)
835+
if not source_lines:
836+
source_lines = None
829837

830838
# Initialize globals, and merge in extraglobs.
831839
if globs is None:

Lib/test/test_doctest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,17 @@ def test_testfile(): r"""
22882288
>>> doctest.master = None # Reset master.
22892289
"""
22902290

2291+
def test_testmod(): r"""
2292+
Tests for the testmod function. More might be useful, but for now we're just
2293+
testing the case raised by Issue 6195, where trying to doctest a C module would
2294+
fail with a UnicodeDecodeError because doctest tried to read the "source" lines
2295+
out of the binary module.
2296+
2297+
>>> import unicodedata
2298+
>>> doctest.testmod(unicodedata)
2299+
TestResults(failed=0, attempted=0)
2300+
"""
2301+
22912302
######################################################################
22922303
## Main
22932304
######################################################################

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Core and Builtins
2424
Library
2525
-------
2626

27+
- Issue #6195: fixed doctest to no longer try to read 'source' data from
28+
binary files.
29+
2730
- Issue #5262: Fixed bug in next rollover time computation in
2831
TimedRotatingFileHandler.
2932

0 commit comments

Comments
 (0)