mirror of
https://github.com/python/cpython.git
synced 2025-08-09 19:38:42 +00:00
[3.12] gh-110259: Fix f-strings with multiline expressions and format specs (GH-110271) (#110396)
gh-110259: Fix f-strings with multiline expressions and format specs (GH-110271)
(cherry picked from commit cc389ef627
)
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
This commit is contained in:
parent
7bfcfcf656
commit
09ec8153c6
5 changed files with 128 additions and 10 deletions
|
@ -2768,11 +2768,28 @@ f_string_middle:
|
|||
if (tok->done == E_ERROR) {
|
||||
return MAKE_TOKEN(ERRORTOKEN);
|
||||
}
|
||||
if (c == EOF || (current_tok->f_string_quote_size == 1 && c == '\n')) {
|
||||
int in_format_spec = (
|
||||
current_tok->last_expr_end != -1
|
||||
&&
|
||||
INSIDE_FSTRING_EXPR(current_tok)
|
||||
);
|
||||
|
||||
if (c == EOF || (current_tok->f_string_quote_size == 1 && c == '\n')) {
|
||||
if (tok->decoding_erred) {
|
||||
return MAKE_TOKEN(ERRORTOKEN);
|
||||
}
|
||||
|
||||
// If we are in a format spec and we found a newline,
|
||||
// it means that the format spec ends here and we should
|
||||
// return to the regular mode.
|
||||
if (in_format_spec && c == '\n') {
|
||||
tok_backup(tok, c);
|
||||
TOK_GET_MODE(tok)->kind = TOK_REGULAR_MODE;
|
||||
p_start = tok->start;
|
||||
p_end = tok->cur;
|
||||
return MAKE_TOKEN(FSTRING_MIDDLE);
|
||||
}
|
||||
|
||||
assert(tok->multi_line_start != NULL);
|
||||
// shift the tok_state's location into
|
||||
// the start of string, and report the error
|
||||
|
@ -2804,11 +2821,6 @@ f_string_middle:
|
|||
end_quote_size = 0;
|
||||
}
|
||||
|
||||
int in_format_spec = (
|
||||
current_tok->last_expr_end != -1
|
||||
&&
|
||||
INSIDE_FSTRING_EXPR(current_tok)
|
||||
);
|
||||
if (c == '{') {
|
||||
int peek = tok_nextc(tok);
|
||||
if (peek != '{' || in_format_spec) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue