Skip to content

Commit ff9abff

Browse files
committed
pythongh-105013: Fix inspect.getsource with parenthesized multiline lambdas
1 parent 949f0f5 commit ff9abff

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

Lib/inspect.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,9 @@ 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
12451248
return lines[:blockfinder.last]
12461249

12471250
def getsourcelines(object):

Lib/test/inspect_fodder2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,11 @@ 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+
281+
# line 281
282+
post_line_parenthesized_lambda = (lambda: ()
283+
)

Lib/test/test_inspect.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,16 @@ 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+
784+
def test_post_line_parenthesized_lambda(self):
785+
# Test inspect.getsource with a parenthesized multi-line lambda
786+
# function.
787+
self.assertSourceEqual(mod2.post_line_parenthesized_lambda, 282, 283)
788+
779789
def test_onelinefunc(self):
780790
# Test inspect.getsource with a regular one-line function.
781791
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)