Skip to content

Commit f080ac3

Browse files
gh-137477: Fix inspect.getblock() for generator expressions
This fixes also inspect.getsourcelines() and inspect.getsource().
1 parent 54a5fdf commit f080ac3

4 files changed

Lines changed: 42 additions & 8 deletions

File tree

Lib/inspect.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,15 +1065,21 @@ def __init__(self):
10651065

10661066
def tokeneater(self, type, token, srowcol, erowcol, line):
10671067
if not self.started and not self.indecorator:
1068-
# skip any decorators
1069-
if token == "@":
1070-
self.indecorator = True
1071-
# look for the first "def", "class" or "lambda"
1072-
elif token in ("def", "class", "lambda"):
1073-
if token == "lambda":
1068+
if type != tokenize.INDENT:
1069+
# skip any decorators
1070+
if token == "@":
1071+
self.indecorator = True
1072+
elif token == "async":
1073+
pass
1074+
# look for the first "def", "class" or "lambda"
1075+
elif token in ("def", "class", "lambda"):
1076+
if token == "lambda":
1077+
self.islambda = True
1078+
self.started = True
1079+
else:
10741080
self.islambda = True
1075-
self.started = True
1076-
self.passline = True # skip to the end of the line
1081+
self.started = True
1082+
self.passline = True # skip to the end of the line
10771083
elif type == tokenize.NEWLINE:
10781084
self.passline = False # stop skipping when a NEWLINE is seen
10791085
self.last = srowcol[0]

Lib/test/test_inspect/inspect_fodder2.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,23 @@ class dc364:
369369
# line 369
370370
dc370 = dataclasses.make_dataclass('dc370', (('x', int), ('y', int)))
371371
dc371 = dataclasses.make_dataclass('dc370', (('x', int), ('y', int)), module=__name__)
372+
373+
import inspect
374+
import itertools
375+
376+
# line 376
377+
ge377 = (
378+
inspect.currentframe()
379+
for i in itertools.count()
380+
)
381+
382+
# line 382
383+
def func383():
384+
# line 384
385+
ge385 = (
386+
inspect.currentframe()
387+
for i in itertools.count()
388+
)
389+
return ge385
390+
391+
pass # end of file

Lib/test/test_inspect/test_inspect.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,12 +1189,18 @@ def test_nested_class_definition_inside_async_function(self):
11891189

11901190
self.assertSourceEqual(run(mod2.func225), 226, 227)
11911191
self.assertSourceEqual(mod2.cls226, 231, 235)
1192+
self.assertSourceEqual(mod2.cls226.func232, 232, 235)
11921193
self.assertSourceEqual(run(mod2.cls226().func232), 233, 234)
11931194

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

1199+
def test_generator_expression(self):
1200+
self.assertSourceEqual(next(mod2.ge377), 377, 380)
1201+
self.assertSourceEqual(next(mod2.func383()), 385, 388)
1202+
1203+
11981204
class TestNoEOL(GetSourceBase):
11991205
def setUp(self):
12001206
self.tempdir = TESTFN + '_dir'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :func:`inspect.getblock`, :func:`inspect.getsourcelines` and
2+
:func:`inspect.getsource` for generator expressions.

0 commit comments

Comments
 (0)