Skip to content

[3.13] gh-137477: Fix inspect.getblock() for generator expressions (GH-137488) #137995

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

Merged
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
17 changes: 10 additions & 7 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,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.singleline = False
self.started = False
self.passline = False
self.indecorator = False
Expand All @@ -1168,19 +1168,22 @@ def __init__(self):

def tokeneater(self, type, token, srowcol, erowcol, line):
if not self.started and not self.indecorator:
if type == tokenize.INDENT or token == "async":
pass
# skip any decorators
if token == "@":
elif token == "@":
self.indecorator = True
# look for the first "def", "class" or "lambda"
elif token in ("def", "class", "lambda"):
if token == "lambda":
self.islambda = 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.singleline = 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]
if self.islambda: # lambdas always end at the first NEWLINE
if self.singleline:
raise EndOfBlock
# hitting a NEWLINE when in a decorator without args
# ends the decorator
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_inspect/inspect_fodder2.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,23 @@ class dc364:
# line 369
dc370 = dataclasses.make_dataclass('dc370', (('x', int), ('y', int)))
dc371 = dataclasses.make_dataclass('dc370', (('x', int), ('y', int)), module=__name__)

import inspect
import itertools

# line 376
ge377 = (
inspect.currentframe()
for i in itertools.count()
)

# line 382
def func383():
# line 384
ge385 = (
inspect.currentframe()
for i in itertools.count()
)
return ge385

pass # end of file
6 changes: 6 additions & 0 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1187,12 +1187,18 @@ def test_nested_class_definition_inside_async_function(self):
self.addCleanup(asyncio.set_event_loop_policy, None)
self.assertSourceEqual(asyncio.run(mod2.func225()), 226, 227)
self.assertSourceEqual(mod2.cls226, 231, 235)
self.assertSourceEqual(mod2.cls226.func232, 232, 235)
self.assertSourceEqual(asyncio.run(mod2.cls226().func232()), 233, 234)

def test_class_definition_same_name_diff_methods(self):
self.assertSourceEqual(mod2.cls296, 296, 298)
self.assertSourceEqual(mod2.cls310, 310, 312)

def test_generator_expression(self):
self.assertSourceEqual(next(mod2.ge377), 377, 380)
self.assertSourceEqual(next(mod2.func383()), 385, 388)


class TestNoEOL(GetSourceBase):
def setUp(self):
self.tempdir = TESTFN + '_dir'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :func:`!inspect.getblock`, :func:`inspect.getsourcelines` and
:func:`inspect.getsource` for generator expressions.
Loading