summaryrefslogtreecommitdiffstats
path: root/Parser
diff options
context:
space:
mode:
authorPablo Galindo Salgado <Pablogsal@gmail.com>2023-10-05 13:26:44 (GMT)
committerGitHub <noreply@github.com>2023-10-05 13:26:44 (GMT)
commitcc389ef627b2a486ab89d9a11245bef48224efb1 (patch)
treef6d4d943438d07fc3d37b905a921cd3f3fd2c3d0 /Parser
parentaf29282fce117cb10f00907fd46d56c2fa6142f5 (diff)
downloadcpython-cc389ef627b2a486ab89d9a11245bef48224efb1.zip
cpython-cc389ef627b2a486ab89d9a11245bef48224efb1.tar.gz
cpython-cc389ef627b2a486ab89d9a11245bef48224efb1.tar.bz2
gh-110259: Fix f-strings with multiline expressions and format specs (#110271)
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
Diffstat (limited to 'Parser')
-rw-r--r--Parser/tokenizer.c24
1 files changed, 18 insertions, 6 deletions
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c
index 41d0d16..5e3816f 100644
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -2690,11 +2690,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
@@ -2726,11 +2743,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) {