]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-115392: Fix doctest reporting incorrect line numbers for decorated function...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 14 Feb 2024 15:55:48 +0000 (16:55 +0100)
committerGitHub <noreply@github.com>
Wed, 14 Feb 2024 15:55:48 +0000 (15:55 +0000)
gh-115392: Fix doctest reporting incorrect line numbers for decorated functions (GH-115440)
(cherry picked from commit bb791c7728e0508ad5df28a90b27e202d66a9cfa)

Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
Lib/doctest.py
Lib/test/test_doctest/decorator_mod.py [new file with mode: 0644]
Lib/test/test_doctest/doctest_lineno.py
Lib/test/test_doctest/test_doctest.py
Misc/NEWS.d/next/Library/2024-02-13-18-27-03.gh-issue-115392.gle5tp.rst [new file with mode: 0644]

index fc9ec65805346e5a4de8605baee68eef65acf9cf..f4396c8f7ad93fcf5909827b231c6187f2e23d48 100644 (file)
@@ -1118,7 +1118,7 @@ class DocTestFinder:
             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):
diff --git a/Lib/test/test_doctest/decorator_mod.py b/Lib/test/test_doctest/decorator_mod.py
new file mode 100644 (file)
index 0000000..9f10688
--- /dev/null
@@ -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
index 677c569cf710ebd2e395d8e93463672c92373e13..0dbcd9a11eaba2fa1d5306b06b297e56d79bacd8 100644 (file)
@@ -67,3 +67,12 @@ class MethodWrapper:
 
 # 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."""
index 1ba7c3d7a6447a693b89aa2e2ae4ed02188f9339..bec2c72299719e2483fdca2f8916b53d3522ef5f 100644 (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
        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
 
diff --git a/Misc/NEWS.d/next/Library/2024-02-13-18-27-03.gh-issue-115392.gle5tp.rst b/Misc/NEWS.d/next/Library/2024-02-13-18-27-03.gh-issue-115392.gle5tp.rst
new file mode 100644 (file)
index 0000000..1c33689
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a bug in :mod:`doctest` where incorrect line numbers would be
+reported for decorated functions.