Skip to content

gh-137477: Fix inspect.getblock() for generator expressions #137488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Add comments and simplify the code.
  • Loading branch information
serhiy-storchaka committed Aug 8, 2025
commit 70ddc909043e36aa27a3d8dca72c64a80902d3a7
29 changes: 13 additions & 16 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ class BlockFinder:
"""Provide a tokeneater() method to detect the end of a code block."""
def __init__(self):
self.indent = 0
self.islambda = False
self.islambda = False # used also for generator expressions
self.started = False
self.passline = False
self.indecorator = False
Expand All @@ -1065,21 +1065,18 @@ def __init__(self):

def tokeneater(self, type, token, srowcol, erowcol, line):
if not self.started and not self.indecorator:
if type != tokenize.INDENT:
# skip any decorators
if token == "@":
self.indecorator = True
elif token == "async":
pass
# look for the first "def", "class" or "lambda"
elif token in ("def", "class", "lambda"):
if token == "lambda":
self.islambda = True
self.started = True
else:
self.islambda = True
self.started = True
self.passline = True # skip to the end of the line
if type == tokenize.INDENT or token == "async":
pass
# skip any decorators
elif token == "@":
self.indecorator = True
else:
# For "def" and "class" scan to the end of the block.
# For "lambda" and generator expression scan to
# the end of the logical line.
self.islambda = token not in ("def", "class")
self.started = True
self.passline = True # skip to the end of the line
elif type == tokenize.NEWLINE:
self.passline = False # stop skipping when a NEWLINE is seen
self.last = srowcol[0]
Expand Down
Loading