https://github.com/python/cpython/commit/bb791c7728e0508ad5df28a90b27e202d66a9cfa
commit: bb791c7728e0508ad5df28a90b27e202d66a9cfa
branch: main
author: Brian Schubert <[email protected]>
committer: AlexWaygood <[email protected]>
date: 2024-02-14T15:01:27Z
summary:
gh-115392: Fix doctest reporting incorrect line numbers for decorated functions
(#115440)
files:
A Lib/test/test_doctest/decorator_mod.py
A Misc/NEWS.d/next/Library/2024-02-13-18-27-03.gh-issue-115392.gle5tp.rst
M Lib/doctest.py
M Lib/test/test_doctest/doctest_lineno.py
M Lib/test/test_doctest/test_doctest.py
diff --git a/Lib/doctest.py b/Lib/doctest.py
index 114aac62a34e95..1969777b667787 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -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):
diff --git a/Lib/test/test_doctest/decorator_mod.py
b/Lib/test/test_doctest/decorator_mod.py
new file mode 100644
index 00000000000000..9f106888411202
--- /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 677c569cf710eb..0dbcd9a11eaba2 100644
--- a/Lib/test/test_doctest/doctest_lineno.py
+++ b/Lib/test/test_doctest/doctest_lineno.py
@@ -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."""
diff --git a/Lib/test/test_doctest/test_doctest.py
b/Lib/test/test_doctest/test_doctest.py
index 7015255db1f7f0..43be200b983227 100644
--- a/Lib/test/test_doctest/test_doctest.py
+++ b/Lib/test/test_doctest/test_doctest.py
@@ -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
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 00000000000000..1c3368968e4cf0
--- /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.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]