summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2022-10-19 11:30:28 +1100
committerDamien George <damien@micropython.org>2023-01-20 17:54:32 +1100
commitfb8792c095802a9fcc8b38c1d8cbc3f641918433 (patch)
treea86efbafbde109013aa697f5c6f2480b4d27fdba /py
parent5c4153ea379550bf595bf0bfa0e3711afea17aa0 (diff)
py/lexer: Wrap in parenthesis all f-string arguments passed to format.
This is important for literal tuples, e.g. f"{a,b,}, {c}" --> "{}".format((a,b), (c),) which would otherwise result in either a syntax error or the wrong result. Fixes issue #9635. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Diffstat (limited to 'py')
-rw-r--r--py/lexer.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/py/lexer.c b/py/lexer.c
index 39e9662f6..e7d0e8144 100644
--- a/py/lexer.c
+++ b/py/lexer.c
@@ -356,6 +356,9 @@ STATIC void parse_string_literal(mp_lexer_t *lex, bool is_raw, bool is_fstring)
vstr_add_byte(&lex->vstr, '{');
next_char(lex);
} else {
+ // wrap each argument in (), e.g.
+ // f"{a,b,}, {c}" --> "{}".format((a,b), (c),)
+ vstr_add_byte(&lex->fstring_args, '(');
// remember the start of this argument (if we need it for f'{a=}').
size_t i = lex->fstring_args.len;
// extract characters inside the { until we reach the
@@ -382,7 +385,9 @@ STATIC void parse_string_literal(mp_lexer_t *lex, bool is_raw, bool is_fstring)
// remove the trailing '='
lex->fstring_args.len--;
}
- // comma-separate args
+ // close the paren-wrapped arg to .format().
+ vstr_add_byte(&lex->fstring_args, ')');
+ // comma-separate args to .format().
vstr_add_byte(&lex->fstring_args, ',');
}
vstr_add_byte(&lex->vstr, '{');