Skip to content

Commit 2d7145d

Browse files
authored
[3.13] gh-130618: Fix parser error when using lambdas inside f-strings (GH-130638) (#130642)
(cherry picked from commit e06bebb) Signed-off-by: Pablo Galindo <[email protected]>
1 parent a0ea87c commit 2d7145d

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-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/lexer/lexer.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,13 @@ _PyLexer_update_fstring_expr(struct tok_state *tok, char cur)
211211
break;
212212
case '}':
213213
case '!':
214-
case ':':
215214
tok_mode->last_expr_end = strlen(tok->start);
216215
break;
216+
case ':':
217+
if (tok_mode->last_expr_end == -1) {
218+
tok_mode->last_expr_end = strlen(tok->start);
219+
}
220+
break;
217221
default:
218222
Py_UNREACHABLE();
219223
}

0 commit comments

Comments
 (0)