Skip to content

Commit 32f87fa

Browse files
miss-islingtonericvsmith
authored andcommitted
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. (pythonGH-31365)
(cherry picked from commit ffd9f8f) Co-authored-by: Eric V. Smith <[email protected]>
1 parent 2f0645d commit 32f87fa

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
@@ -1060,6 +1060,8 @@ def test_mismatched_braces(self):
10601060
"f'{{{'",
10611061
"f'{{}}{'",
10621062
"f'{'",
1063+
"f'x{<'", # See bpo-46762.
1064+
"f'x{>'",
10631065
])
10641066

10651067
# 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/pegen/parse_string.c

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

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

0 commit comments

Comments
 (0)