Skip to content

Commit 7cdd725

Browse files
committed
gh-139516: fix lambda colon start format spec in f-string
1 parent af01b46 commit 7cdd725

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Lib/test/test_tokenize.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,23 @@ def test_multiline_non_ascii_fstring_with_expr(self):
12161216
FSTRING_END "\'\'\'" (3, 1) (3, 4)
12171217
""")
12181218

1219+
# gh-139516, the '\n' is explicit to ensure no trailing whitespace which would invalidate the test
1220+
self.check_tokenize('''f"{f(a=lambda: 'à'\n)}"''', """\
1221+
FSTRING_START \'f"\' (1, 0) (1, 2)
1222+
OP '{' (1, 2) (1, 3)
1223+
NAME 'f' (1, 3) (1, 4)
1224+
OP '(' (1, 4) (1, 5)
1225+
NAME 'a' (1, 5) (1, 6)
1226+
OP '=' (1, 6) (1, 7)
1227+
NAME 'lambda' (1, 7) (1, 13)
1228+
OP ':' (1, 13) (1, 14)
1229+
STRING "\'à\'" (1, 15) (1, 18)
1230+
NL '\\n' (1, 18) (1, 19)
1231+
OP ')' (2, 0) (2, 1)
1232+
OP '}' (2, 1) (2, 2)
1233+
FSTRING_END \'"\' (2, 2) (2, 3)
1234+
""")
1235+
12191236
class GenerateTokensTest(TokenizeTest):
12201237
def check_tokenize(self, s, expected):
12211238
# Format the tokens in s in a table format.

Parser/lexer/lexer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
13761376
return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok, "invalid non-printable character U+%04X", c));
13771377
}
13781378

1379-
if( c == '=' && INSIDE_FSTRING_EXPR(current_tok)) {
1379+
if( c == '=' && INSIDE_FSTRING_EXPR_AT_TOP(current_tok)) {
13801380
current_tok->in_debug = 1;
13811381
}
13821382

Parser/lexer/state.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#define INSIDE_FSTRING(tok) (tok->tok_mode_stack_index > 0)
1111
#define INSIDE_FSTRING_EXPR(tok) (tok->curly_bracket_expr_start_depth >= 0)
12+
#define INSIDE_FSTRING_EXPR_AT_TOP(tok) \
13+
(tok->curly_bracket_depth - tok->curly_bracket_expr_start_depth == 1)
1214

1315
enum decoding_state {
1416
STATE_INIT,

0 commit comments

Comments
 (0)