Skip to content

Commit

Permalink
pythongh-115392: Fix doctest reporting incorrect line numbers for dec…
Browse files Browse the repository at this point in the history
…orated functions (python#115440)
  • Loading branch information
brianschubert committed Feb 14, 2024
1 parent 6755c4e commit bb791c7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/doctest.py
Expand Up @@ -1140,7 +1140,7 @@ def _find_lineno(self, obj, source_lines):
obj = obj.fget
if inspect.isfunction(obj) and getattr(obj, '__doc__', None):
# We don't use `docstring` var here, because `obj` can be changed.
obj = obj.__code__
obj = inspect.unwrap(obj).__code__
if inspect.istraceback(obj): obj = obj.tb_frame
if inspect.isframe(obj): obj = obj.f_code
if inspect.iscode(obj):
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_doctest/decorator_mod.py
@@ -0,0 +1,10 @@
# This module is used in `doctest_lineno.py`.
import functools


def decorator(f):
@functools.wraps(f)
def inner():
return f()

return inner
9 changes: 9 additions & 0 deletions Lib/test/test_doctest/doctest_lineno.py
Expand Up @@ -67,3 +67,12 @@ def property_with_doctest(self):

# https://github.com/python/cpython/issues/99433
str_wrapper = object().__str__


# https://github.com/python/cpython/issues/115392
from test.test_doctest.decorator_mod import decorator

@decorator
@decorator
def func_with_docstring_wrapped():
"""Some unrelated info."""
1 change: 1 addition & 0 deletions Lib/test/test_doctest/test_doctest.py
Expand Up @@ -685,6 +685,7 @@ def basics(): r"""
None test.test_doctest.doctest_lineno.MethodWrapper.method_without_docstring
61 test.test_doctest.doctest_lineno.MethodWrapper.property_with_doctest
4 test.test_doctest.doctest_lineno.func_with_docstring
77 test.test_doctest.doctest_lineno.func_with_docstring_wrapped
12 test.test_doctest.doctest_lineno.func_with_doctest
None test.test_doctest.doctest_lineno.func_without_docstring
Expand Down
@@ -0,0 +1,2 @@
Fix a bug in :mod:`doctest` where incorrect line numbers would be
reported for decorated functions.

0 comments on commit bb791c7

Please sign in to comment.