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

Skip to content
8000

Commit cbe1707

Browse files
committed
bpo-35753: fix doctest on unwrappables funcs 2/2
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. This commit adds the fix to the module. We simply skip any object that throws this exception. This should handle the majority of cases. Future thoughts: 1. Should catching this be optional defaulting to true, but allowing for folks to rethrow the exception to insist on truly clean code? 2. Should we break out _find() into more sub functions sp that we can more easily derive our own versions for each sub function that _find does making it easier to fix future issues? 3. Should we include a way to denylist by `id` some objects so that future objects that cause problems with doctest can be passed in to ignore? Test Plan: Reviewers:
1 parent 4604ba4 commit cbe1707

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Lib/doctest.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -994,9 +994,19 @@ 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 believe there
999+
# is a cycle.
1000+
# example: https://bugs.python.org/issue35753
1001+
# how to trigger:
1002+
# from unittest.mock import call
1003+
isroutine = False
1004+
try:
1005+
isroutine = inspect.isroutine(inspect.unwrap(val))
1006+
except ValueError:
1007+
pass
9971008
# Recurse to functions & classes.
998-
if ((inspect.isroutine(inspect.unwrap(val))
999-
or inspect.isclass(val)) and
1009+
if ((isroutine or inspect.isclass(val)) and
10001010
self._from_module(module, val)):
10011011
self._find(tests, val, valname, module, source_lines,
10021012
globs, seen)

0 commit comments

Comments
 (0)
0