From 2d7145decb1acad4636a21b77ab3a9c4f48af36d Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Sat, 1 Mar 2025 18:10:03 +0000 Subject: [PATCH] [3.13] gh-130618: Fix parser error when using lambdas inside f-strings (GH-130638) (#130642) (cherry picked from commit e06bebb87e1b33f7251196e1ddb566f528c3fc98) Signed-off-by: Pablo Galindo --- Lib/test/test_grammar.py | 12 ++++++++++++ .../2025-02-27-15-07-06.gh-issue-130618.JTcsRB.rst | 3 +++ Parser/lexer/lexer.c | 6 +++++- 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-02-27-15-07-06.gh-issue-130618.JTcsRB.rst diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index c72f4387108..46eb5736fe3 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -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 index 00000000000..de67496108e --- /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/lexer/lexer.c b/Parser/lexer/lexer.c index 3ced1be0b00..15e6332ee35 100644 --- a/Parser/lexer/lexer.c +++ b/Parser/lexer/lexer.c @@ -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(); }