summaryrefslogtreecommitdiffstats
path: root/Lib/inspect.py
diff options
context:
space:
mode:
authorIrit Katriel <iritkatriel@yahoo.com>2020-12-04 16:45:38 (GMT)
committerGitHub <noreply@github.com>2020-12-04 16:45:38 (GMT)
commit6e1eec71f59c344fb23c7977061dc2c97b77d51b (patch)
tree42d94caf698e4c38085599b36b4ccc153e16397d /Lib/inspect.py
parent066394018a8463643cc63d933493f0afa99d72cc (diff)
downloadcpython-6e1eec71f59c344fb23c7977061dc2c97b77d51b.zip
cpython-6e1eec71f59c344fb23c7977061dc2c97b77d51b.tar.gz
cpython-6e1eec71f59c344fb23c7977061dc2c97b77d51b.tar.bz2
bpo-42116: Fix inspect.getsource handling of trailing comments (GH-23630)
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r--Lib/inspect.py7
1 files changed, 7 insertions, 0 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 7412d0e..073a79d 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -930,6 +930,7 @@ class BlockFinder:
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:
@@ -961,6 +962,8 @@ class BlockFinder:
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:
@@ -970,6 +973,10 @@ class BlockFinder:
# 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.