Skip to content

Commit e06bebb

Browse files
authored
pythongh-130618: Fix parser error when using lambdas inside f-strings (python#130638)
1 parent b26286c commit e06bebb

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Lib/test/test_grammar.py

+12
Original file line numberDiff line numberDiff line change
@@ -1972,6 +1972,18 @@ async def foo():
19721972
with self.assertRaises(Done):
19731973
foo().send(None)
19741974

1975+
def test_complex_lambda(self):
1976+
def test1(foo, bar):
1977+
return ""
1978+
1979+
def test2():
1980+
return f"{test1(
1981+
foo=lambda: '、、、、、、、、、、、、、、、、、',
1982+
bar=lambda: 'abcdefghijklmnopqrstuvwxyz 123456789 123456789',
1983+
)}"
1984+
1985+
self.assertEqual(test2(), "")
1986+
19751987

19761988
if __name__ == '__main__':
19771989
unittest.main()
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

+5-1
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)