Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,7 @@ def __init__(self):
self.indecorator = False
self.decoratorhasargs = False
self.last = 1
self.body_col0 = None

def tokeneater(self, type, token, srowcol, erowcol, line):
if not self.started and not self.indecorator:
Expand Down Expand Up @@ -930,6 +931,8 @@ def tokeneater(self, type, token, srowcol, erowcol, line):
elif self.passline:
pass
elif type == tokenize.INDENT:
if self.body_col0 is None and self.started:
self.body_col0 = erowcol[1]
self.indent = self.indent + 1
self.passline = True
elif type == tokenize.DEDENT:
Expand All @@ -939,6 +942,10 @@ def tokeneater(self, type, token, srowcol, erowcol, line):
# not e.g. for "if: else:" or "try: finally:" blocks)
if self.indent <= 0:
raise EndOfBlock
elif type == tokenize.COMMENT:
if self.body_col0 is not None and srowcol[1] >= self.body_col0:
# Include comments if indented at least as much as the block
self.last = srowcol[0]
elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
# any other token on the same indentation level end the previous
# block as well, except the pseudo-tokens COMMENT and NL.
Expand Down
22 changes: 22 additions & 0 deletions Lib/test/inspect_fodder.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,25 @@ def as_method_of(self, obj):

custom_method = Callable().as_method_of(42)
del Callable

# line 95
class WhichComments:
# line 97
# before f
def f(self):
# line 100
# start f
return 1
# line 103
# end f
# line 105
# after f

# before asyncf - line 108
async def asyncf(self):
# start asyncf
return 2
# end asyncf
# after asyncf - line 113
# end of WhichComments - line 114
# after WhichComments - line 115
23 changes: 19 additions & 4 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ def test_getclasses(self):
('ParrotDroppings', mod.ParrotDroppings),
('StupidGit', mod.StupidGit),
('Tit', mod.MalodorousPervert),
('WhichComments', mod.WhichComments),
])
tree = inspect.getclasstree([cls[1] for cls in classes])
self.assertEqual(tree,
Expand All @@ -405,7 +406,8 @@ def test_getclasses(self):
[(mod.FesteringGob, (mod.MalodorousPervert,
mod.ParrotDroppings))
]
]
],
(mod.WhichComments, (object,),)
]
])
tree = inspect.getclasstree([cls[1] for cls in classes], True)
Expand All @@ -417,7 +419,8 @@ def test_getclasses(self):
[(mod.FesteringGob, (mod.MalodorousPervert,
mod.ParrotDroppings))
]
]
],
(mod.WhichComments, (object,),)
]
])

Expand Down Expand Up @@ -647,6 +650,18 @@ def test_anonymous(self):
# as argument to another function.
self.assertSourceEqual(mod2.anonymous, 55, 55)

class TestBlockComments(GetSourceBase):
fodderModule = mod

def test_toplevel_class(self):
self.assertSourceEqual(mod.WhichComments, 96, 114)

def test_class_method(self):
self.assertSourceEqual(mod.WhichComments.f, 99, 104)

def test_class_async_method(self):
self.assertSourceEqual(mod.WhichComments.asyncf, 109, 112)

class TestBuggyCases(GetSourceBase):
fodderModule = mod2

Expand Down Expand Up @@ -3970,8 +3985,8 @@ def test_getsource_reload(self):

def test_main():
run_unittest(
TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBuggyCases,
TestInterpreterStack, TestClassesAndFunctions, TestPredicates,
TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBlockComments,
TestBuggyCases, TestInterpreterStack, TestClassesAndFunctions, TestPredicates,
TestGetcallargsFunctions, TestGetcallargsMethods,
TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState,
TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix handling of trailing comments by :func:`inspect.getsource`.