[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 bb791c7728)

Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-02-14 16:55:48 +01:00 committed by GitHub
parent cb941e1264
commit c4e8ffb9a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 23 additions and 1 deletions

View file

@ -1118,7 +1118,7 @@ class DocTestFinder:
obj = obj.fget obj = obj.fget
if inspect.isfunction(obj) and getattr(obj, '__doc__', None): if inspect.isfunction(obj) and getattr(obj, '__doc__', None):
# We don't use `docstring` var here, because `obj` can be changed. # 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.istraceback(obj): obj = obj.tb_frame
if inspect.isframe(obj): obj = obj.f_code if inspect.isframe(obj): obj = obj.f_code
if inspect.iscode(obj): if inspect.iscode(obj):

View file

@ -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

View file

@ -67,3 +67,12 @@ class MethodWrapper:
# https://github.com/python/cpython/issues/99433 # https://github.com/python/cpython/issues/99433
str_wrapper = object().__str__ 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."""

View file

@ -687,6 +687,7 @@ It used to be broken for quite some time until `bpo-28249`.
None test.test_doctest.doctest_lineno.MethodWrapper.method_without_docstring None test.test_doctest.doctest_lineno.MethodWrapper.method_without_docstring
61 test.test_doctest.doctest_lineno.MethodWrapper.property_with_doctest 61 test.test_doctest.doctest_lineno.MethodWrapper.property_with_doctest
4 test.test_doctest.doctest_lineno.func_with_docstring 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 12 test.test_doctest.doctest_lineno.func_with_doctest
None test.test_doctest.doctest_lineno.func_without_docstring None test.test_doctest.doctest_lineno.func_without_docstring

View file

@ -0,0 +1,2 @@
Fix a bug in :mod:`doctest` where incorrect line numbers would be
reported for decorated functions.