Skip to content

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

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 4 commits into from
Apr 30, 2025
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
16 changes: 16 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,22 @@
Traceback (most recent call last):
SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='?

>>> ft'abc'
Traceback (most recent call last):
SyntaxError: can't use 'f' and 't' string prefixes together

>>> tf"{x=}"
Traceback (most recent call last):
SyntaxError: can't use 'f' and 't' string prefixes together

>>> tb''
Traceback (most recent call last):
SyntaxError: can't use 'b' and 't' string prefixes together

>>> bt"text"
Traceback (most recent call last):
SyntaxError: can't use 'b' and 't' string prefixes together

>>> t'{x}' = 42
Traceback (most recent call last):
SyntaxError: cannot assign to t-string expression here. Maybe you meant '==' instead of '='?
Expand Down
20 changes: 17 additions & 3 deletions Parser/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -660,17 +660,31 @@ 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 {
break;
}
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;
}
Expand Down
Loading