]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-130618: Fix parser error when using lambdas inside f-strings (GH-130638...
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Sat, 1 Mar 2025 18:10:07 +0000 (18:10 +0000)
committerGitHub <noreply@github.com>
Sat, 1 Mar 2025 18:10:07 +0000 (18:10 +0000)
(cherry picked from commit e06bebb87e1b33f7251196e1ddb566f528c3fc98)

Lib/test/test_grammar.py
Misc/NEWS.d/next/Core_and_Builtins/2025-02-27-15-07-06.gh-issue-130618.JTcsRB.rst [new file with mode: 0644]
Parser/tokenizer.c

index c72f4387108ca81fa82feb1043ba74651817fad9..46eb5736fe3a8900bac905c9940379e59fd4f4b2 100644 (file)
@@ -2030,6 +2030,18 @@ class GrammarTests(unittest.TestCase):
         with self.assertRaises(Done):
             foo().send(None)
 
+    def test_complex_lambda(self):
+        def test1(foo, bar):
+            return ""
+
+        def test2():
+            return f"{test1(
+                foo=lambda: '、、、、、、、、、、、、、、、、、',
+                bar=lambda: 'abcdefghijklmnopqrstuvwxyz 123456789 123456789',
+            )}"
+
+        self.assertEqual(test2(), "")
+
 
 if __name__ == '__main__':
     unittest.main()
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-02-27-15-07-06.gh-issue-130618.JTcsRB.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-02-27-15-07-06.gh-issue-130618.JTcsRB.rst
new file mode 100644 (file)
index 0000000..de67496
--- /dev/null
@@ -0,0 +1,3 @@
+Fix a bug that was causing ``UnicodeDecodeError`` or ``SystemError`` to be
+raised when using f-strings with ``lambda`` expressions with non-ASCII
+characters. Patch by Pablo Galindo
index 7ba717d909124e0bd42e2d937f72b9bb30c730cc..d2a0eaca701b78cb770785209721870fa5b3a142 100644 (file)
@@ -479,8 +479,11 @@ static int update_fstring_expr(struct tok_state *tok, char cur) {
     break;
   case '}':
   case '!':
-  case ':':
     tok_mode->last_expr_end = strlen(tok->start);
+  case ':':
+    if (tok_mode->last_expr_end == -1) {
+        tok_mode->last_expr_end = strlen(tok->start);
+    }
     break;
   default:
     Py_UNREACHABLE();