Skip to content

Commit 0860d9c

Browse files
authored
[3.12] gh-130618: Fix parser error when using lambdas inside f-strings (GH-130638) (#130644)
(cherry picked from commit e06bebb)
1 parent 99e18ab commit 0860d9c

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

Lib/test/test_grammar.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,18 @@ async def foo():
20302030
with self.assertRaises(Done):
20312031
foo().send(None)
20322032

2033+
def test_complex_lambda(self):
2034+
def test1(foo, bar):
2035+
return ""
2036+
2037+
def test2():
2038+
return f"{test1(
2039+
foo=lambda: '、、、、、、、、、、、、、、、、、',
2040+
bar=lambda: 'abcdefghijklmnopqrstuvwxyz 123456789 123456789',
2041+
)}"
2042+
2043+
self.assertEqual(test2(), "")
2044+
20332045

20342046
if __name__ == '__main__':
20352047
unittest.main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a bug that was causing ``UnicodeDecodeError`` or ``SystemError`` to be
2+
raised when using f-strings with ``lambda`` expressions with non-ASCII
3+
characters. Patch by Pablo Galindo

Parser/tokenizer.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,11 @@ static int update_fstring_expr(struct tok_state *tok, char cur) {
479479
break;
480480
case '}':
481481
case '!':
482-
case ':':
483482
tok_mode->last_expr_end = strlen(tok->start);
483+
case ':':
484+
if (tok_mode->last_expr_end == -1) {
485+
tok_mode->last_expr_end = strlen(tok->start);
486+
}
484487
break;
485488
default:
486489
Py_UNREACHABLE();

0 commit comments

Comments
 (0)