Skip to content

[3.8] bpo-35753: Fix crash in doctest with unwrap-able functions #22834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,9 +994,15 @@ def _find(self, tests, obj, name, module, source_lines, globs, seen):
if inspect.ismodule(obj) and self._recurse:
for valname, val in obj.__dict__.items():
valname = '%s.%s' % (name, valname)

# Protect against objects that cause unwrap to throw
isroutine = False
try:
isroutine = inspect.isroutine(inspect.unwrap(val))
except ValueError:
pass
# Recurse to functions & classes.
if ((inspect.isroutine(inspect.unwrap(val))
or inspect.isclass(val)) and
if ((isroutine or inspect.isclass(val)) and
self._from_module(module, val)):
self._find(tests, val, valname, module, source_lines,
globs, seen)
Expand Down
33 changes: 33 additions & 0 deletions Lib/test/test_doctest5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""A module to test whether doctest works properly with
un-unwrappable functions.

See: https://bugs.python.org/issue35753
"""

# This should trigger issue35753 when doctest called
from unittest.mock import call

import sys
import unittest
from test import support
if sys.flags.optimize >= 2:
raise unittest.SkipTest("Cannot test docstrings with -O2")


def test_main():
from test import test_doctest5
EXPECTED = 0
try:
f, t = support.run_doctest(test_doctest5)
if t != EXPECTED:
raise support.TestFailed("expected %d tests to run, not %d" %
(EXPECTED, t))
except ValueError as e:
raise support.TestFailed("Doctest unwrap failed") from e

# Pollute the namespace with a bunch of imported functions and classes,
# to make sure they don't get tested.
# from doctest import *

if __name__ == '__main__':
test_main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash in doctest when doctest parses modules that include unwrappable
functions by skipping those functions.