From 649397d95c13a8534557a598ba49c66dbec71ba2 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Sat, 27 May 2023 17:10:41 +0100 Subject: [PATCH] fixup! gh-105013: Fix inspect.getsource with parenthesized multiline lambdas --- Lib/inspect.py | 5 +++++ Lib/test/inspect_fodder2.py | 7 ++++++- Lib/test/test_inspect.py | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 4fbba303715a9a0..8b286dd90c5f338 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1245,6 +1245,11 @@ def getblock(lines): except SyntaxError as e: if "unmatched ')" not in e.msg: raise e from None + _, *_token_info = _token + try: + blockfinder.tokeneater(tokenize.NEWLINE, *_token_info) + except (EndOfBlock, IndentationError): + pass return lines[:blockfinder.last] def getsourcelines(object): diff --git a/Lib/test/inspect_fodder2.py b/Lib/test/inspect_fodder2.py index e33e734d2591fc7..963333725a176ff 100644 --- a/Lib/test/inspect_fodder2.py +++ b/Lib/test/inspect_fodder2.py @@ -280,4 +280,9 @@ def complex_decorated(foo=0, bar=lambda: 0): # line 281 post_line_parenthesized_lambda = (lambda: () -) \ No newline at end of file +) + +# line 285 +nested_lambda = ( + lambda right: [].map( + lambda length: ())) \ No newline at end of file diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index df4c0e7dd62acaa..40512b6b08eef1c 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -786,6 +786,10 @@ def test_post_line_parenthesized_lambda(self): # function. self.assertSourceEqual(mod2.post_line_parenthesized_lambda, 282, 283) + def test_nested_lambda(self): + # Test inspect.getsource with a nested lambda function. + self.assertSourceEqual(mod2.nested_lambda, 287, 288) + def test_onelinefunc(self): # Test inspect.getsource with a regular one-line function. self.assertSourceEqual(mod2.onelinefunc, 37, 37)