Skip to content

Commit 08738ce

Browse files
[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 f4104f5) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 5d34830 commit 08738ce

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

Lib/inspect.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,9 @@ def __init__(self):
10651065

10661066
def tokeneater(self, type, token, srowcol, erowcol, line):
10671067
if not self.started and not self.indecorator:
1068-
if type == tokenize.INDENT or token == "async":
1068+
if type in (tokenize.INDENT, tokenize.COMMENT, tokenize.NL):
1069+
pass
1070+
elif token == "async":
10691071
pass
10701072
# skip any decorators
10711073
elif token == "@":

Lib/test/test_inspect/inspect_fodder2.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,4 +388,16 @@ def func383():
388388
)
389389
return ge385
390390

391+
# line 391
392+
@decorator
393+
# comment
394+
def func394():
395+
return 395
396+
397+
# line 397
398+
@decorator
399+
400+
def func400():
401+
return 401
402+
391403
pass # end of file

Lib/test/test_inspect/test_inspect.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,10 @@ def test_generator_expression(self):
12231223
self.assertSourceEqual(next(mod2.ge377), 377, 380)
12241224
self.assertSourceEqual(next(mod2.func383()), 385, 388)
12251225

1226+
def test_comment_or_empty_line_after_decorator(self):
1227+
self.assertSourceEqual(mod2.func394, 392, 395)
1228+
self.assertSourceEqual(mod2.func400, 398, 401)
1229+
12261230

12271231
class TestNoEOL(GetSourceBase):
12281232
def setUp(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :func:`inspect.getsourcelines` for the case when a decorator is followed
2+
by a comment or an empty line.

0 commit comments

Comments
 (0)