Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
gh-105013: Fix inspect.getsource with parenthesized multiline lambdas
  • Loading branch information
pablogsal committed May 27, 2023
commit ff9abfff660efc5dc6c664bcd07046b652c45751
3 changes: 3 additions & 0 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,9 @@ def getblock(lines):
blockfinder.tokeneater(*_token)
except (EndOfBlock, IndentationError):
pass
except SyntaxError as e:
if "unmatched ')" not in e.msg:
raise e from None
return lines[:blockfinder.last]

def getsourcelines(object):
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/inspect_fodder2.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,11 @@ def wrapper(*a, **kwd):
@deco_factory(foo=(1 + 2), bar=lambda: 1)
def complex_decorated(foo=0, bar=lambda: 0):
return foo + bar()

# line 276
parenthesized_lambda = (
lambda: ())

# line 281
post_line_parenthesized_lambda = (lambda: ()
)
10 changes: 10 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,16 @@ def test_twoline_indented_lambda(self):
# where the second line _is_ indented.
self.assertSourceEqual(mod2.tlli, 33, 34)

def test_parenthesized_multiline_lambda(self):
# Test inspect.getsource with a parenthesized multi-line lambda
# function.
self.assertSourceEqual(mod2.parenthesized_lambda, 279, 279)

def test_post_line_parenthesized_lambda(self):
# Test inspect.getsource with a parenthesized multi-line lambda
# function.
self.assertSourceEqual(mod2.post_line_parenthesized_lambda, 282, 283)

def test_onelinefunc(self):
# Test inspect.getsource with a regular one-line function.
self.assertSourceEqual(mod2.onelinefunc, 37, 37)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix handling of multiline parenthesized lambdas in
:func:`inspect.getsource`. Patch by Pablo Galindo