8000 [3.11] gh-115392: Fix doctest reporting incorrect line numbers for de… · python/cpython@c4e8ffb · GitHub
[go: up one dir, main page]

Skip to content

Commit c4e8ffb

Browse files
[3.11] gh-115392: Fix doctest reporting incorrect line numbers for decorated functions (GH-115440) (#115458)
gh-115392: Fix doctest reporting incorrect line numbers for decorated functions (GH-115440) (cherry picked from commit bb791c7) Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
1 parent cb941e1 commit c4e8ffb

File tree

5 files changed

+23
-1
lines changed

5 files changed

+23
-1
lines changed

Lib/doctest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ def _find_lineno(self, obj, source_lines):
11181118
obj = obj.fget
11191119
if inspect.isfunction(obj) and getattr(obj, '__doc__', None):
11201120
# We don't use `docstring` var here, because `obj` can be changed.
1121-
obj = obj.__code__
1121+
obj = inspect.unwrap(obj).__code__
11221122
if inspect.istraceback(obj): obj = obj.tb_frame
11231123
if inspect.isframe(obj): obj = obj.f_code
11241124
if inspect.iscode(obj):
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This module is used in `doctest_lineno.py`.
2+
import functools
3+
4+
5+
def decorator(f):
6+
@functools.wraps(f)
7+
def inner():
8+
return f()
9+
10+
return inner

Lib/test/test_doctest/doctest_lineno.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,12 @@ def property_with_doctest(self):
6767

6868
# https://github.com/python/cpython/issues/99433
6969
str_wrapper = object().__str__
70+
71+
72+
# https://github.com/python/cpython/issues/115392
73+
from test.test_doctest.decorator_mod import decorator
74+
75+
@decorator
76+
@decorator
77+
def func_with_docstring_wrapped():
78+
"""Some unrelated info."""

Lib/test/test_doctest/test_doctest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ def basics(): r"""
687687
None test.test_doctest.doctest_lineno.MethodWrapper.method_without_docstring
688688
61 test.test_doctest.doctest_lineno.MethodWrapper.property_with_doctest
689689
4 test.test_doctest.doctest_lineno.func_with_docstring
690+
77 test.test_doctest.doctest_lineno.func_with_docstring_wrapped
690691
12 test.test_doctest.doctest_lineno.func_with_doctest
691692
None test.test_doctest.doctest_lineno.func_without_docstring
692693
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a bug in :mod:`doctest` where incorrect line numbers would be
2+
reported for decorated functions.

0 commit comments

Comments
 (0)
0