]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-139783: Fix inspect.getsourcelines() for the case when a decorator is follo...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 10 Oct 2025 21:26:36 +0000 (23:26 +0200)
committerGitHub <noreply@github.com>
Fri, 10 Oct 2025 21:26:36 +0000 (21:26 +0000)
(cherry picked from commit f4104f5d74b99712253fceb39a4460ee3f7a281c)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/inspect.py
Lib/test/test_inspect/inspect_fodder2.py
Lib/test/test_inspect/test_inspect.py
Misc/NEWS.d/next/Library/2025-10-09-13-48-28.gh-issue-139783.__NUgo.rst [new file with mode: 0644]

index c5653f693f10809ee78422f47bad13b8dce477a6..5a814f97b5b89c1be60c6e45b55e8851b2650584 100644 (file)
@@ -1168,7 +1168,9 @@ class BlockFinder:
 
     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 == "@":
index 1de283f672d362cdb60e36388c0e9822dbbd52cf..157e12167b5d270400e834146e42f2ccb2ef2808 100644 (file)
@@ -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
index 691c2ffd5cc1db8e347fb1f9795a8b04f2213b53..49996bba2ca9edf98ca3caa2c9961a2bbc5ec83a 100644 (file)
@@ -1221,6 +1221,10 @@ class TestBuggyCases(GetSourceBase):
         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 (file)
index 0000000..336653e
--- /dev/null
@@ -0,0 +1,2 @@
+Fix :func:`inspect.getsourcelines` for the case when a decorator is followed
+by a comment or an empty line.