Skip to content

Commit 649397d

Browse files
committed
fixup! gh-105013: Fix inspect.getsource with parenthesized multiline lambdas
1 parent ff9abff commit 649397d

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

Lib/inspect.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,11 @@ def getblock(lines):
12451245
except SyntaxError as e:
12461246
if "unmatched ')" not in e.msg:
12471247
raise e from None
1248+
_, *_token_info = _token
1249+
try:
1250+
blockfinder.tokeneater(tokenize.NEWLINE, *_token_info)
1251+
except (EndOfBlock, IndentationError):
1252+
pass
12481253
return lines[:blockfinder.last]
12491254

12501255
def getsourcelines(object):

Lib/test/inspect_fodder2.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,9 @@ def complex_decorated(foo=0, bar=lambda: 0):
280280

281281
# line 281
282282
post_line_parenthesized_lambda = (lambda: ()
283-
)
283+
)
284+
285+
# line 285
286+
nested_lambda = (
287+
lambda right: [].map(
288+
lambda length: ()))

Lib/test/test_inspect.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,10 @@ def test_post_line_parenthesized_lambda(self):
786786
# function.
787787
self.assertSourceEqual(mod2.post_line_parenthesized_lambda, 282, 283)
788788

789+
def test_nested_lambda(self):
790+
# Test inspect.getsource with a nested lambda function.
791+
self.assertSourceEqual(mod2.nested_lambda, 287, 288)
792+
789793
def test_onelinefunc(self):
790794
# Test inspect.getsource with a regular one-line function.
791795
self.assertSourceEqual(mod2.onelinefunc, 37, 37)

0 commit comments

Comments
 (0)