Skip to content

Commit 1f2b9aa

Browse files
committed
gh-153569: preserve debug text in format specs
1 parent 3c3f313 commit 1f2b9aa

2 files changed

Lines changed: 8 additions & 10 deletions

File tree

Parser/lexer/lexer.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,10 @@ _PyLexer_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, str
544544
}
545545

546546
/* Punctuation character */
547-
int is_punctuation = (c == ':' || c == '}' || c == '!' || c == '{');
547+
int is_punctuation = (c == ':' || c == '}' || c == '!');
548548
if (is_punctuation && _PyLexer_InsideFString(tok) &&
549549
INSIDE_FSTRING_EXPR(current_tok)) {
550-
int cursor = current_tok->curly_bracket_depth - (c != '{');
550+
int cursor = current_tok->curly_bracket_depth - 1;
551551
int in_format_spec = current_tok->in_format_spec;
552552
int cursor_in_format_with_debug =
553553
cursor == 1 && (current_tok->in_debug || in_format_spec);
@@ -560,14 +560,9 @@ _PyLexer_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, str
560560
tok_backup(tok, c2);
561561
}
562562
if (cursor_valid) {
563-
if (c == '{') {
564-
_PyLexer_begin_ftstring_expr(tok);
565-
}
566-
else {
567-
_PyLexer_finish_ftstring_expr(tok);
568-
}
563+
_PyLexer_finish_ftstring_expr(tok);
569564
}
570-
if (cursor_valid && c != '{' &&
565+
if (cursor_valid &&
571566
_PyLexer_set_ftstring_expr_metadata(tok, token)) {
572567
return MAKE_TOKEN(ERRORTOKEN);
573568
}

Parser/lexer/string.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,15 @@ _PyLexer_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, st
386386
if (start_char == '{') {
387387
int peek1 = tok_nextc(tok);
388388
tok_backup(tok, peek1);
389-
tok_backup(tok, start_char);
390389
if (peek1 != '{') {
390+
_PyLexer_begin_ftstring_expr(tok);
391+
tok_backup(tok, start_char);
391392
if (enter_ftstring_expr(tok, current_tok) < 0) {
392393
return MAKE_TOKEN(ERRORTOKEN);
393394
}
394395
return _PyLexer_get_normal_mode(tok, current_tok, token);
395396
}
397+
tok_backup(tok, start_char);
396398
}
397399
else {
398400
tok_backup(tok, start_char);
@@ -490,6 +492,7 @@ _PyLexer_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, st
490492
int peek = tok_nextc(tok);
491493
if (peek != '{' || in_format_spec) {
492494
tok_backup(tok, peek);
495+
_PyLexer_begin_ftstring_expr(tok);
493496
tok_backup(tok, c);
494497
if (enter_ftstring_expr(tok, current_tok) < 0) {
495498
return MAKE_TOKEN(ERRORTOKEN);

0 commit comments

Comments
 (0)