https://github.com/python/cpython/commit/08738ce52109a5a1ee9baaa59b3e2263442be99a
commit: 08738ce52109a5a1ee9baaa59b3e2263442be99a
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2025-10-10T08:18:32Z
summary:

[3.14] gh-139783: Fix inspect.getsourcelines() for the case when a decorator is 
followed by a comment or an empty line (GH-139836) (GH-139889)

(cherry picked from commit f4104f5d74b99712253fceb39a4460ee3f7a281c)

Co-authored-by: Serhiy Storchaka <[email protected]>

files:
A Misc/NEWS.d/next/Library/2025-10-09-13-48-28.gh-issue-139783.__NUgo.rst
M Lib/inspect.py
M Lib/test/test_inspect/inspect_fodder2.py
M Lib/test/test_inspect/test_inspect.py

diff --git a/Lib/inspect.py b/Lib/inspect.py
index c642b5308ed384..385fbc686b6233 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -1065,7 +1065,9 @@ def __init__(self):
 
     def tokeneater(self, type, token, srowcol, erowcol, line):
         if not self.started and not self.indecorator:
-            if type == tokenize.INDENT or token == "async":
+            if type in (tokenize.INDENT, tokenize.COMMENT, tokenize.NL):
+                pass
+            elif token == "async":
                 pass
             # skip any decorators
             elif token == "@":
diff --git a/Lib/test/test_inspect/inspect_fodder2.py 
b/Lib/test/test_inspect/inspect_fodder2.py
index 1de283f672d362..157e12167b5d27 100644
--- a/Lib/test/test_inspect/inspect_fodder2.py
+++ b/Lib/test/test_inspect/inspect_fodder2.py
@@ -388,4 +388,16 @@ def func383():
     )
     return ge385
 
+# line 391
+@decorator
+# comment
+def func394():
+    return 395
+
+# line 397
+@decorator
+
+def func400():
+    return 401
+
 pass # end of file
diff --git a/Lib/test/test_inspect/test_inspect.py 
b/Lib/test/test_inspect/test_inspect.py
index 94804eb135418a..1ba253db48151e 100644
--- a/Lib/test/test_inspect/test_inspect.py
+++ b/Lib/test/test_inspect/test_inspect.py
@@ -1223,6 +1223,10 @@ def test_generator_expression(self):
         self.assertSourceEqual(next(mod2.ge377), 377, 380)
         self.assertSourceEqual(next(mod2.func383()), 385, 388)
 
+    def test_comment_or_empty_line_after_decorator(self):
+        self.assertSourceEqual(mod2.func394, 392, 395)
+        self.assertSourceEqual(mod2.func400, 398, 401)
+
 
 class TestNoEOL(GetSourceBase):
     def setUp(self):
diff --git 
a/Misc/NEWS.d/next/Library/2025-10-09-13-48-28.gh-issue-139783.__NUgo.rst 
b/Misc/NEWS.d/next/Library/2025-10-09-13-48-28.gh-issue-139783.__NUgo.rst
new file mode 100644
index 00000000000000..336653e73bfa98
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-10-09-13-48-28.gh-issue-139783.__NUgo.rst
@@ -0,0 +1,2 @@
+Fix :func:`inspect.getsourcelines` for the case when a decorator is followed
+by a comment or an empty line.

_______________________________________________
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]

Reply via email to