Skip to content

Commit 43cd39d

Browse files
committed
pythongh-130618: Fix parser error when using lambdas inside f-strings (python#130638)
(cherry picked from commit e06bebb)
1 parent 676ceca commit 43cd39d

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-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()

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)