https://github.com/python/cpython/commit/0860d9c72fdf72f8dc7acde832a4e61532f2caf7
commit: 0860d9c72fdf72f8dc7acde832a4e61532f2caf7
branch: 3.12
author: Pablo Galindo Salgado <[email protected]>
committer: pablogsal <[email protected]>
date: 2025-03-01T18:10:07Z
summary:
[3.12] gh-130618: Fix parser error when using lambdas inside f-strings
(GH-130638) (#130644)
(cherry picked from commit e06bebb87e1b33f7251196e1ddb566f528c3fc98)
files:
A
Misc/NEWS.d/next/Core_and_Builtins/2025-02-27-15-07-06.gh-issue-130618.JTcsRB.rst
M Lib/test/test_grammar.py
M Parser/tokenizer.c
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index c72f4387108ca8..46eb5736fe3a89 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -2030,6 +2030,18 @@ async def foo():
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
index 00000000000000..de67496108e2a7
--- /dev/null
+++
b/Misc/NEWS.d/next/Core_and_Builtins/2025-02-27-15-07-06.gh-issue-130618.JTcsRB.rst
@@ -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
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c
index 7ba717d909124e..d2a0eaca701b78 100644
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -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();
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]