summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--py/lexer.c7
-rw-r--r--tests/basics/string_fstring.py5
-rw-r--r--tests/basics/string_fstring_debug.py4
-rw-r--r--tests/basics/string_fstring_debug.py.exp3
4 files changed, 18 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, '{');
diff --git a/tests/basics/string_fstring.py b/tests/basics/string_fstring.py
index 7e8a97fd3..8907a5c47 100644
--- a/tests/basics/string_fstring.py
+++ b/tests/basics/string_fstring.py
@@ -56,3 +56,8 @@ try:
except (ValueError, SyntaxError):
# MicroPython incorrectly raises ValueError here.
print('SyntaxError')
+
+# Allow literal tuples
+print(f"a {1,} b")
+print(f"a {x,y,} b")
+print(f"a {x,1} b")
diff --git a/tests/basics/string_fstring_debug.py b/tests/basics/string_fstring_debug.py
index 76a448ca0..95abd4d6d 100644
--- a/tests/basics/string_fstring_debug.py
+++ b/tests/basics/string_fstring_debug.py
@@ -21,3 +21,7 @@ print(f"a {x=:08x} b {y} c")
print(f'a {f() + g("foo") + h()=} b')
print(f'a {f() + g("foo") + h()=:08x} b')
+
+print(f"a {1,=} b")
+print(f"a {x,y,=} b")
+print(f"a {x,1=} b")
diff --git a/tests/basics/string_fstring_debug.py.exp b/tests/basics/string_fstring_debug.py.exp
index 563030f40..f0309e1c9 100644
--- a/tests/basics/string_fstring_debug.py.exp
+++ b/tests/basics/string_fstring_debug.py.exp
@@ -4,3 +4,6 @@ a x=1 b 2 c
a x=00000001 b 2 c
a f() + g("foo") + h()=15 b
a f() + g("foo") + h()=0000000f b
+a 1,=(1,) b
+a x,y,=(1, 2) b
+a x,1=(1, 1) b