Skip to content

Commit ffd9f8f

Browse files
authored
bpo-46762: Fix an assert failure in f-strings where > or < is the last character if the f-string is missing a trailing right brace. (#31365)
1 parent e59309b commit ffd9f8f

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

Lib/test/test_fstring.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,8 @@ def test_mismatched_braces(self):
10531053
"f'{{{'",
10541054
"f'{{}}{'",
10551055
"f'{'",
1056+
"f'x{<'", # See bpo-46762.
1057+
"f'x{>'",
10561058
])
10571059

10581060
# But these are just normal strings.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an assert failure in debug builds when a '<', '>', or '=' is the last
2+
character in an f-string that's missing a closing right brace.

Parser/string_parser.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -666,12 +666,12 @@ fstring_find_expr(Parser *p, const char **str, const char *end, int raw, int rec
666666
*str += 1;
667667
continue;
668668
}
669-
/* Don't get out of the loop for these, if they're single
670-
chars (not part of 2-char tokens). If by themselves, they
671-
don't end an expression (unlike say '!'). */
672-
if (ch == '>' || ch == '<') {
673-
continue;
674-
}
669+
}
670+
/* Don't get out of the loop for these, if they're single
671+
chars (not part of 2-char tokens). If by themselves, they
672+
don't end an expression (unlike say '!'). */
673+
if (ch == '>' || ch == '<') {
674+
continue;
675675
}
676676

677677
/* Normal way out of this loop. */
@@ -698,10 +698,10 @@ fstring_find_expr(Parser *p, const char **str, const char *end, int raw, int rec
698698
}
699699
}
700700
expr_end = *str;
701-
/* If we leave this loop in a string or with mismatched parens, we
702-
don't care. We'll get a syntax error when compiling the
703-
expression. But, we can produce a better error message, so
704-
let's just do that.*/
701+
/* If we leave the above loop in a string or with mismatched parens, we
702+
don't really care. We'll get a syntax error when compiling the
703+
expression. But, we can produce a better error message, so let's just
704+
do that.*/
705705
if (quote_char) {
706706
RAISE_SYNTAX_ERROR("f-string: unterminated string");
707707
goto error;

0 commit comments

Comments
 (0)