gh-133197: Improve error message for ft"" and bt"" cases (#133202)

This commit is contained in:
sobolevn 2025-04-30 19:20:44 +03:00 committed by GitHub
parent c78216e42c
commit b451516aa6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 3 deletions

View file

@ -648,7 +648,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
/* Process the various legal combinations of b"", r"", u"", and f"". */
int saw_b = 0, saw_r = 0, saw_u = 0, saw_f = 0, saw_t = 0;
while (1) {
if (!(saw_b || saw_u || saw_f || saw_t) && (c == 'b' || c == 'B'))
if (!(saw_b || saw_u || saw_f) && (c == 'b' || c == 'B'))
saw_b = 1;
/* Since this is a backwards compatibility support literal we don't
want to support it in arbitrary order like byte literals. */
@ -660,10 +660,10 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
else if (!(saw_r || saw_u) && (c == 'r' || c == 'R')) {
saw_r = 1;
}
else if (!(saw_f || saw_b || saw_u || saw_t) && (c == 'f' || c == 'F')) {
else if (!(saw_f || saw_b || saw_u) && (c == 'f' || c == 'F')) {
saw_f = 1;
}
else if (!(saw_t || saw_b || saw_u || saw_f) && (c == 't' || c == 'T')) {
else if (!(saw_t || saw_u) && (c == 't' || c == 'T')) {
saw_t = 1;
}
else {
@ -671,6 +671,20 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
}
c = tok_nextc(tok);
if (c == '"' || c == '\'') {
if (saw_b && saw_t) {
return MAKE_TOKEN(_PyTokenizer_syntaxerror_known_range(
tok, (int)(tok->start + 1 - tok->line_start),
(int)(tok->cur - tok->line_start),
"can't use 'b' and 't' string prefixes together"));
}
if (saw_f && saw_t) {
return MAKE_TOKEN(_PyTokenizer_syntaxerror_known_range(
tok, (int)(tok->start + 1 - tok->line_start),
(int)(tok->cur - tok->line_start),
"can't use 'f' and 't' string prefixes together"));
}
// Handle valid f or t string creation:
if (saw_f || saw_t) {
goto f_string_quote;
}