Skip to content

[3.9] 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. (GH-31365) #31371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/test/test_fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,8 @@ def test_mismatched_braces(self):
"f'{{{'",
"f'{{}}{'",
"f'{'",
"f'x{<'", # See bpo-46762.
"f'x{>'",
])

# But these are just normal strings.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix an assert failure in debug builds when a '<', '>', or '=' is the last
character in an f-string that's missing a closing right brace.
20 changes: 10 additions & 10 deletions Parser/pegen/parse_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -668,12 +668,12 @@ fstring_find_expr(Parser *p, const char **str, const char *end, int raw, int rec
*str += 1;
continue;
}
/* Don't get out of the loop for these, if they're single
chars (not part of 2-char tokens). If by themselves, they
don't end an expression (unlike say '!'). */
if (ch == '>' || ch == '<') {
continue;
}
}
/* Don't get out of the loop for these, if they're single
chars (not part of 2-char tokens). If by themselves, they
don't end an expression (unlike say '!'). */
if (ch == '>' || ch == '<') {
continue;
}

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