8000 bpo-42116: Fix inspect.getsource handling of trailing comments (GH-23… · python/cpython@6e1eec7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6e1eec7

Browse files
authored
bpo-42116: Fix inspect.getsource handling of trailing comments (GH-23630)
1 parent 0663940 commit 6e1eec7

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

Lib/inspect.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,7 @@ def __init__(self):
930930
self.indecorator = False
931931
self.decoratorhasargs = False
932932
self.last = 1
933+
self.body_col0 = None
933934

934935
def tokeneater(self, type, token, srowcol, erowcol, line):
935936
if not self.started and not self.indecorator:
@@ -961,6 +962,8 @@ def tokeneater(self, type, token, srowcol, erowcol, line):
961962
elif self.passline:
962963
pass
963964
elif type == tokenize.INDENT:
965+
if self.body_col0 is None and self.started:
966+
self.body_col0 = erowcol[1]
964967
self.indent = self.indent + 1
965968
self.passline = True
966969
elif type == tokenize.DEDENT:
@@ -970,6 +973,10 @@ def tokeneater(self, type, token, srowcol, erowcol, line):
970973
# not e.g. for "if: else:" or "try: finally:" blocks)
971974
if self.indent <= 0:
972975
raise EndOfBlock
976+
elif type == tokenize.COMMENT:
977+
if self.body_col0 is not None and srowcol[1] >= self.body_col0:
978+
# Include comments if indented at least as much as the block
979+
self.last = srowcol[0]
973980
elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
974981
# any other token on the same indentation level end the previous
975982
# block as well, except the pseudo-tokens COMMENT and NL.

Lib/test/inspect_fodder.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,25 @@ def as_method_of(self, obj):
9191

9292
custom_method = Callable().as_method_of(42)
9393
del Callable
94+
95+
# line 95
96+
class WhichComments:
97+
# line 97
98+
# before f
99+
def f(self):
100+
# line 100
101+
# start f
102+
return 1
103+
# line 103
104+
# end f
105+
# line 105
106+
# after f
107+
108+
# before asyncf - line 108
109+
async def asyncf(self):
110+
# start asyncf
111+
return 2
112+
# end asyncf
113+
# after asyncf - line 113
114+
# end of WhichComments - line 114
115+
# after WhichComments - line 115

Lib/test/test_inspect.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ def test_getclasses(self):
390390
('ParrotDroppings', mod.ParrotDroppings),
391391
('StupidGit', mod.StupidGit),
392392
('Tit', mod.MalodorousPervert),
393+
('WhichComments', mod.WhichComments),
393394
])
394395
tree = inspect.getclasstree([cls[1] for cls in classes])
395396
self.assertEqual(tree,
@@ -403,7 +404,8 @@ def test_getclasses(self):
403404
[(mod.FesteringGob, (mod.MalodorousPervert,
404405
mod.ParrotDroppings))
405406
]
406-
]
407+
],
408+
(mod.WhichComments, (object,),)
407409
]
408410
])
409411
tree = inspect.getclasstree([cls[1] for cls in classes], True)
@@ -415,7 +417,8 @@ def test_getclasses(self):
415417
[(mod.FesteringGob, (mod.MalodorousPervert,
416418
mod.ParrotDroppings))
417419
]
418-
]
420+
],
421+
(mod.WhichComments, (object,),)
419422
]
420423
])
421424

@@ -646,6 +649,18 @@ def test_anonymous(self):
646649
# as argument to another function.
647650
self.assertSourceEqual(mod2.anonymous, 55, 55)
648651

652+
class TestBlockComments(GetSourceBase):
653+
fodderModule = mod
654+
655+
def test_toplevel_class(self):
656+
self.assertSourceEqual(mod.WhichComments, 96, 114)
657+
658+
def test_class_method(self):
659+
self.assertSourceEqual(mod.WhichComments.f, 99, 104)
660+
661+
def test_class_async_method(self):
662+
self.assertSourceEqual(mod.WhichComments.asyncf, 109, 112)
663+
649664
class TestBuggyCases(GetSourceBase):
650665
fodderModule = mod2
651666

@@ -4014,8 +4029,8 @@ def test_getsource_reload(self):
40144029

40154030
def test_main():
40164031
run_unittest(
4017-
TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBuggyCases,
4018-
TestInterpreterStack, TestClassesAndFunctions, TestPredicates,
4032+
TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBlockComments,
4033+
TestBuggyCases, TestInterpreterStack, TestClassesAndFunctions, TestPredicates,
40194034
TestGetcallargsFunctions, TestGetcallargsMethods,
40204035
TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState,
40214036
TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix handling of trailing comments by :func:`inspect.getsource`.

0 commit comments

Comments
 (0)
0