8000 [3.8] bpo-35753: fix doctest on unwrappables funcs · python/cpython@3b6a384 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3b6a384

Browse files
committed
[3.8] bpo-35753: fix doctest on unwrappables funcs
Summary: Ignore objects that inspect.unwrap throws due to too many wrappers. This is a very rare case, however it can easily be surfaced when a module under doctest imports unitest.mock.call into its namespace.
1 parent 82bfe36 commit 3b6a384

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

Lib/doctest.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -994,9 +994,15 @@ def _find(self, tests, obj, name, module, source_lines, globs, seen):
994994
if inspect.ismodule(obj) and self._recurse:
995995
for valname, val in obj.__dict__.items():
996996
valname = '%s.%s' % (name, valname)
997+
998+
# Protect against objects that cause unwrap to throw
999+
isroutine = False
1000+
try:
1001+
isroutine = inspect.isroutine(inspect.< 8000 span class=pl-c1>unwrap(val))
1002+
except ValueError:
1003+
pass
9971004
# Recurse to functions & classes.
998-
if ((inspect.isroutine(inspect.unwrap(val))
999-
or inspect.isclass(val)) and
1005+
if ((isroutine or inspect.isclass(val)) and
10001006
self._from_module(module, val)):
10011007
self._find(tests, val, valname, module, source_lines,
10021008
globs, seen)

Lib/test/test_doctest5.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""A module to test whether doctest works properly with
2+
un-unwrappable functions.
3+
4+
See: https://bugs.python.org/issue35753
5+
"""
6+
7+
# This should trigger issue35753 when doctest called
8+
from unittest.mock import call
9+
10+
import sys
11+
import unittest
12+
from test import support
13+
if sys.flags.optimize >= 2:
14+
raise unittest.SkipTest("Cannot test docstrings with -O2")
15+
16+
17+
def test_main():
18+
from test import test_doctest5
19+
EXPECTED = 0
20+
try:
21+
f, t = support.run_doctest(test_doctest5)
22+
if t != EXPECTED:
23+
raise support.TestFailed("expected %d tests to run, not %d" %
24+
(EXPECTED, t))
25+
except ValueError as e:
26+
raise support.TestFailed("Doctest unwrap failed") from e
27+
28+
# Pollute the namespace with a bunch of imported functions and classes,
29+
# to make sure they don't get tested.
30+
# from doctest import *
31+
32+
if __name__ == '__main__':
33+
test_main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix crash in doctest when doctest parses modules that include unwrappable
2+
functions by skipping those functions.

0 commit comments

Comments
 (0)
0