Skip to content

Commit 41b622b

Browse files
[3.12] gh-105013: Fix inspect.getsource with parenthesized multiline lambdas (GH-105021) (#105032)
gh-105013: Fix inspect.getsource with parenthesized multiline lambdas (GH-105021) (cherry picked from commit 3a5be87) Co-authored-by: Pablo Galindo Salgado <[email protected]>
1 parent 36a4227 commit 41b622b

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

Lib/inspect.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,14 @@ def getblock(lines):
12421242
blockfinder.tokeneater(*_token)
12431243
except (EndOfBlock, IndentationError):
12441244
pass
1245+
except SyntaxError as e:
1246+
if "unmatched" not in e.msg:
1247+
raise e from None
1248+
_, *_token_info = _token
1249+
try:
1250+
blockfinder.tokeneater(tokenize.NEWLINE, *_token_info)
1251+
except (EndOfBlock, IndentationError):
1252+
pass
12451253
return lines[:blockfinder.last]
12461254

12471255
def getsourcelines(object):

Lib/test/inspect_fodder2.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,20 @@ def wrapper(*a, **kwd):
273273
@deco_factory(foo=(1 + 2), bar=lambda: 1)
274274
def complex_decorated(foo=0, bar=lambda: 0):
275275
return foo + bar()
276+
277+
# line 276
278+
parenthesized_lambda = (
279+
lambda: ())
280+
parenthesized_lambda2 = [
281+
lambda: ()][0]
282+
parenthesized_lambda3 = {0:
283+
lambda: ()}[0]
284+
285+
# line 285
286+
post_line_parenthesized_lambda1 = (lambda: ()
287+
)
288+
289+
# line 289
290+
nested_lambda = (
291+
lambda right: [].map(
292+
lambda length: ()))

Lib/test/test_inspect.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,22 @@ def test_twoline_indented_lambda(self):
776776
# where the second line _is_ indented.
777777
self.assertSourceEqual(mod2.tlli, 33, 34)
778778

779+
def test_parenthesized_multiline_lambda(self):
780+
# Test inspect.getsource with a parenthesized multi-line lambda
781+
# function.
782+
self.assertSourceEqual(mod2.parenthesized_lambda, 279, 279)
783+
self.assertSourceEqual(mod2.parenthesized_lambda2, 281, 281)
784+
self.assertSourceEqual(mod2.parenthesized_lambda3, 283, 283)
785+
786+
def test_post_line_parenthesized_lambda(self):
787+
# Test inspect.getsource with a parenthesized multi-line lambda
788+
# function.
789+
self.assertSourceEqual(mod2.post_line_parenthesized_lambda1, 286, 287)
790+
791+
def test_nested_lambda(self):
792+
# Test inspect.getsource with a nested lambda function.
793+
self.assertSourceEqual(mod2.nested_lambda, 291, 292)
794+
779795
def test_onelinefunc(self):
780796
# Test inspect.getsource with a regular one-line function.
781797
self.assertSourceEqual(mod2.onelinefunc, 37, 37)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix handling of multiline parenthesized lambdas in
2+
:func:`inspect.getsource`. Patch by Pablo Galindo

0 commit comments

Comments
 (0)