Skip to content

Commit bc2c8d7

Browse files
miss-islingtonserhiy-storchaka
authored andcommitted
[3.13] gh-139783: Fix inspect.getsourcelines() for the case when a decorator is followed by a comment or an empty line (GH-139836) (GH-139890)
(cherry picked from commit f4104f5) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent a15ae61 commit bc2c8d7

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
@@ -1168,7 +1168,9 @@ def __init__(self):
11681168

11691169
def tokeneater(self, type, token, srowcol, erowcol, line):
11701170
if not self.started and not self.indecorator:
1171-
if type == tokenize.INDENT or token == "async":
1171+
if type in (tokenize.INDENT, tokenize.COMMENT, tokenize.NL):
1172+
pass
1173+
elif token == "async":
11721174
pass
11731175
# skip any decorators
11741176
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
@@ -1221,6 +1221,10 @@ def test_generator_expression(self):
12211221
self.assertSourceEqual(next(mod2.ge377), 377, 380)
12221222
self.assertSourceEqual(next(mod2.func383()), 385, 388)
12231223

1224+
def test_comment_or_empty_line_after_decorator(self):
1225+
self.assertSourceEqual(mod2.func394, 392, 395)
1226+
self.assertSourceEqual(mod2.func400, 398, 401)
1227+
12241228

12251229
class TestNoEOL(GetSourceBase):
12261230
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)