summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-02-14 15:58:14 (GMT)
committerGitHub <noreply@github.com>2024-02-14 15:58:14 (GMT)
commit13af281a53bcf522f933afbbe6af4ed2da823943 (patch)
treec77a1b6fe243ccb0debae63b5c856021a72506d7
parent59317167eba49d1378ee082506d5eafbfc29de35 (diff)
downloadcpython-13af281a53bcf522f933afbbe6af4ed2da823943.zip
cpython-13af281a53bcf522f933afbbe6af4ed2da823943.tar.gz
cpython-13af281a53bcf522f933afbbe6af4ed2da823943.tar.bz2
[3.12] gh-115392: Fix doctest reporting incorrect line numbers for decorated functions (GH-115440) (#115459)
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>
-rw-r--r--Lib/doctest.py2
-rw-r--r--Lib/test/test_doctest/decorator_mod.py10
-rw-r--r--Lib/test/test_doctest/doctest_lineno.py9
-rw-r--r--Lib/test/test_doctest/test_doctest.py1
-rw-r--r--Misc/NEWS.d/next/Library/2024-02-13-18-27-03.gh-issue-115392.gle5tp.rst2
5 files changed, 23 insertions, 1 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py
index 4630e40..cda2f5a 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -1124,7 +1124,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
index 0000000..9f10688
--- /dev/null
+++ b/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
diff --git a/Lib/test/test_doctest/doctest_lineno.py b/Lib/test/test_doctest/doctest_lineno.py
index 677c569..0dbcd9a 100644
--- a/Lib/test/test_doctest/doctest_lineno.py
+++ b/Lib/test/test_doctest/doctest_lineno.py
@@ -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."""
diff --git a/Lib/test/test_doctest/test_doctest.py b/Lib/test/test_doctest/test_doctest.py
index 21f27ed..9c8a8ba 100644
--- a/Lib/test/test_doctest/test_doctest.py
+++ b/Lib/test/test_doctest/test_doctest.py
@@ -686,6 +686,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
index 0000000..1c33689
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-02-13-18-27-03.gh-issue-115392.gle5tp.rst
@@ -0,0 +1,2 @@
+Fix a bug in :mod:`doctest` where incorrect line numbers would be
+reported for decorated functions.