]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-105013: Fix inspect.getsource with parenthesized multiline lambdas (#105021)
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Sat, 27 May 2023 23:20:42 +0000 (00:20 +0100)
committerGitHub <noreply@github.com>
Sat, 27 May 2023 23:20:42 +0000 (00:20 +0100)
Lib/inspect.py
Lib/test/inspect_fodder2.py
Lib/test/test_inspect.py
Misc/NEWS.d/next/Core and Builtins/2023-05-27-16-57-11.gh-issue-105013.IsDgDY.rst [new file with mode: 0644]

index 7709a95003efbd751bd41716ad5b69750c27128b..55530fc780b35c96a9dc68972a12db6b3981f76a 100644 (file)
@@ -1242,6 +1242,14 @@ def getblock(lines):
             blockfinder.tokeneater(*_token)
     except (EndOfBlock, IndentationError):
         pass
+    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):
index 2dc49817087c44e2e71d37cb9b509b71f69eefca..03464613694605377f9fd10ed8825ef320c2a164 100644 (file)
@@ -273,3 +273,20 @@ def deco_factory(**kwargs):
 @deco_factory(foo=(1 + 2), bar=lambda: 1)
 def complex_decorated(foo=0, bar=lambda: 0):
     return foo + bar()
+
+# line 276
+parenthesized_lambda = (
+    lambda: ())
+parenthesized_lambda2 = [
+    lambda: ()][0]
+parenthesized_lambda3 = {0:
+    lambda: ()}[0]
+
+# line 285
+post_line_parenthesized_lambda1 = (lambda: ()
+)
+
+# line 289
+nested_lambda = (
+    lambda right: [].map(
+        lambda length: ()))
index ade32151eaf2339ffea11a6c4dfe2dc24a5d7c63..a7bd680d0f5bcc4d10246e302c41e5734c84f5c3 100644 (file)
@@ -776,6 +776,22 @@ class TestOneliners(GetSourceBase):
         # 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)
+        self.assertSourceEqual(mod2.parenthesized_lambda2, 281, 281)
+        self.assertSourceEqual(mod2.parenthesized_lambda3, 283, 283)
+
+    def test_post_line_parenthesized_lambda(self):
+        # Test inspect.getsource with a parenthesized multi-line lambda
+        # function.
+        self.assertSourceEqual(mod2.post_line_parenthesized_lambda1, 286, 287)
+
+    def test_nested_lambda(self):
+        # Test inspect.getsource with a nested lambda function.
+        self.assertSourceEqual(mod2.nested_lambda, 291, 292)
+
     def test_onelinefunc(self):
         # Test inspect.getsource with a regular one-line function.
         self.assertSourceEqual(mod2.onelinefunc, 37, 37)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-05-27-16-57-11.gh-issue-105013.IsDgDY.rst b/Misc/NEWS.d/next/Core and Builtins/2023-05-27-16-57-11.gh-issue-105013.IsDgDY.rst
new file mode 100644 (file)
index 0000000..a9917c2
--- /dev/null
@@ -0,0 +1,2 @@
+Fix handling of multiline parenthesized lambdas in
+:func:`inspect.getsource`. Patch by Pablo Galindo