diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/basics/string_fstring.py | 19 | ||||
| -rw-r--r-- | tests/cpydiff/core_fstring_repr.py | 18 |
2 files changed, 23 insertions, 14 deletions
diff --git a/tests/basics/string_fstring.py b/tests/basics/string_fstring.py index 8907a5c47..1a3960680 100644 --- a/tests/basics/string_fstring.py +++ b/tests/basics/string_fstring.py @@ -61,3 +61,22 @@ except (ValueError, SyntaxError): print(f"a {1,} b") print(f"a {x,y,} b") print(f"a {x,1} b") + +# f-strings with conversion specifiers (only support !r and !s). +a = "123" +print(f"{a!r}") +print(f"{a!s}") +try: + eval('print(f"{a!x}")') +except (ValueError, SyntaxError): + # CPython detects this at compile time, MicroPython fails with ValueError + # when the str.format is executed. + print("ValueError") + +# Mixing conversion specifiers with formatting. +print(f"{a!r:8s}") +print(f"{a!s:8s}") + +# Still allow ! in expressions. +print(f"{'1' if a != '456' else '0'!r:8s}") +print(f"{'1' if a != '456' else '0'!s:8s}") diff --git a/tests/cpydiff/core_fstring_repr.py b/tests/cpydiff/core_fstring_repr.py index df80abf79..d37fb48db 100644 --- a/tests/cpydiff/core_fstring_repr.py +++ b/tests/cpydiff/core_fstring_repr.py @@ -1,18 +1,8 @@ """ categories: Core -description: f-strings don't support the !r, !s, and !a conversions -cause: MicroPython is optimised for code space. -workaround: Use repr(), str(), and ascii() explicitly. +description: f-strings don't support !a conversions +cause: MicropPython does not implement ascii() +workaround: None """ - -class X: - def __repr__(self): - return "repr" - - def __str__(self): - return "str" - - -print(f"{X()!r}") -print(f"{X()!s}") +f"{'unicode text'!a}" |
