gh-130618: Fix parser error when using lambdas inside f-strings (#130638)

This commit is contained in:
Pablo Galindo Salgado 2025-02-27 15:51:17 +00:00 committed by GitHub
parent b26286ca49
commit e06bebb87e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 20 additions and 1 deletions

View 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()

View file

@ -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

View 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();
}