]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-130618: Fix parser error when using lambdas inside f-strings (#130638)
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Thu, 27 Feb 2025 15:51:17 +0000 (15:51 +0000)
committerGitHub <noreply@github.com>
Thu, 27 Feb 2025 15:51:17 +0000 (15:51 +0000)
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/lexer/lexer.c

index 6a841587f49166ca908a7b25c2f6c134eb0eb5d7..994c8a0e15ac40555841cb6c5ec46a7395f602f8 100644 (file)
@@ -1972,6 +1972,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 8c5ae37fa9086055b0d1f6428f013d2c2ffe1b1c..207fb6b363766c27e3e052ee61cce8d1b4e07042 100644 (file)
@@ -211,9 +211,13 @@ _PyLexer_update_fstring_expr(struct tok_state *tok, char cur)
             break;
         case '}':
         case '!':
-        case ':':
             tok_mode->last_expr_end = strlen(tok->start);
             break;
+        case ':':
+            if (tok_mode->last_expr_end == -1) {
+               tok_mode->last_expr_end = strlen(tok->start);
+            }
+            break;
         default:
             Py_UNREACHABLE();
     }