Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
micropython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
micropython
Commits
11ed9479
Commit
11ed9479
authored
Nov 19, 2021
by
Damien George
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
py/lexer: Support nested [] and {} characters within f-string params.
Signed-off-by:
Damien George
<
damien@micropython.org
>
parent
196d2684
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
20 additions
and
6 deletions
+20
-6
py/lexer.c
py/lexer.c
+9
-2
tests/basics/string_fstring.py
tests/basics/string_fstring.py
+7
-0
tests/cpydiff/core_fstring_parser.py
tests/cpydiff/core_fstring_parser.py
+4
-4
No files found.
py/lexer.c
View file @
11ed9479
...
...
@@ -363,9 +363,16 @@ STATIC void parse_string_literal(mp_lexer_t *lex, bool is_raw, bool is_fstring)
// (MicroPython limitation) note: this is completely unaware of
// Python syntax and will not handle any expression containing '}' or ':'.
// e.g. f'{"}"}' or f'{foo({})}'.
while
(
!
is_end
(
lex
)
&&
!
is_char_or
(
lex
,
':'
,
'}'
))
{
unsigned
int
nested_bracket_level
=
0
;
while
(
!
is_end
(
lex
)
&&
(
nested_bracket_level
!=
0
||
!
is_char_or
(
lex
,
':'
,
'}'
)))
{
unichar
c
=
CUR_CHAR
(
lex
);
if
(
c
==
'['
||
c
==
'{'
)
{
nested_bracket_level
+=
1
;
}
else
if
(
c
==
']'
||
c
==
'}'
)
{
nested_bracket_level
-=
1
;
}
// like the default case at the end of this function, stay 8-bit clean
vstr_add_byte
(
&
lex
->
fstring_args
,
CUR_CHAR
(
lex
)
);
vstr_add_byte
(
&
lex
->
fstring_args
,
c
);
next_char
(
lex
);
}
if
(
lex
->
fstring_args
.
buf
[
lex
->
fstring_args
.
len
-
1
]
==
'='
)
{
...
...
tests/basics/string_fstring.py
View file @
11ed9479
...
...
@@ -22,6 +22,13 @@ def foo(a, b):
return
f'
{
x
}{
y
}{
a
}{
b
}
'
print
(
foo
(
7
,
8
))
# ':' character within {...} that should not be interpreted as format specifiers.
print
(
f"a
{
[
0
,
1
,
2
][
0
:
2
]
}
"
)
print
(
f"a
{
[
0
,
15
,
2
][
0
:
2
][
-
1
]:
04
x
}
"
)
# Nested '{' and '}' characters.
print
(
f"a
{
{
0
,
1
,
2
}}
"
)
# PEP-0498 specifies that '\\' and '#' must be disallowed explicitly, whereas
# MicroPython relies on the syntax error as a result of the substitution.
...
...
tests/cpydiff/core_fstring_parser.py
View file @
11ed9479
"""
categories: Core
description: f-strings cannot support expressions that require parsing to resolve
nested brace
s
description: f-strings cannot support expressions that require parsing to resolve
unbalanced nested braces and bracket
s
cause: MicroPython is optimised for code space.
workaround:
Only use simple
expressions inside f-strings
workaround:
Always use balanced braces and brackets in
expressions inside f-strings
"""
f'
{
"hello
{}
world
"
}
'
f"
{
repr
(
{}
)
}
"
print
(
f'
{
"hello
{
world
"
}
')
print(f'
{
"hello ] world"
}
')
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment