]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-139783: Fix inspect.getsourcelines() for the case when a decorator is followed...
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 10 Oct 2025 07:51:24 +0000 (10:51 +0300)
committerGitHub <noreply@github.com>
Fri, 10 Oct 2025 07:51:24 +0000 (10:51 +0300)
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 b345623b3fa2db4921ad2fb8f02f8c00c4c93d41..bb22bab3040fcb65b4213be7a0337422d2805230 100644 (file)
@@ -1065,7 +1065,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 e32e34c63b53246f86832924eadeeb6cb28962cb..d42f2dbff99cae8ba331a3d968555d1b6027a21a 100644 (file)
@@ -1223,6 +1223,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.