You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
--doctest-modules uses GBs of memory on modules importing unittest.mock.call when the recursion limit is raised #15333
On Python 3.10+, the stdlib DocTestFinder._is_routine calls inspect.unwrap(obj) on every module attribute and only catches ValueError. mock.call.__wrapped__ keeps returning a new _Call with a longer name, so unwrapping runs until sys.getrecursionlimit(). Memory grows roughly quadratically with the limit.
pytest used to guard against this. MockAwareDocTestFinder._find skipped _is_mocked objects and ran the stdlib _find under _patch_unwrap_mock_aware() (#3456, python/cpython#69718). That override was limited to Python < 3.10 and was removed along with 3.9 support in bb712f1. _patch_unwrap_mock_aware itself is now only used by its own test.
Possible fix
Restore the _find override for all Python versions (about 14 lines in src/_pytest/doctest.py). I prototyped it locally: the reproducer drops to 0.09 s / 33 MB and the full test suite passes. A regression test is still needed.
I traced this back to commit bb712f151 ("Remove support for Python 3.9"), which removed the _find override under the assumption that the DocTestFinder._find workaround was only needed on Python < 3.10. Because CPython's stdlib DocTestFinder._is_routine unwraps module attributes using inspect.unwrap across all Python versions (3.10+), _patch_unwrap_mock_aware() became inactive in practice during doctest collection.
Restoring _find unconditionally inside MockAwareDocTestFinder in src/_pytest/doctest.py:
restores the stop guard _is_mocked(obj) on inspect.unwrap, preventing the quadratic unwrap loop on unittest.mock.call.
I would love to pick this up and submit the PR along with a regression test (e.g. running --doctest-modules with elevated recursion limit and asserting immediate completion without runaway recursion).
Activity
Abdur-Rafay-AR commentedon Nov 20, 2026
Hi @RonnyPfannschmidt, thanks for pinpointing this!
I traced this back to commit
bb712f151("Remove support for Python 3.9"), which removed the_findoverride under the assumption that theDocTestFinder._findworkaround was only needed on Python < 3.10. Because CPython's stdlibDocTestFinder._is_routineunwraps module attributes usinginspect.unwrapacross all Python versions (3.10+),_patch_unwrap_mock_aware()became inactive in practice during doctest collection.Restoring
_findunconditionally insideMockAwareDocTestFinderinsrc/_pytest/doctest.py:restores the stop guard
_is_mocked(obj)oninspect.unwrap, preventing the quadratic unwrap loop onunittest.mock.call.I would love to pick this up and submit the PR along with a regression test (e.g. running
--doctest-moduleswith elevated recursion limit and asserting immediate completion without runaway recursion).