diff options
author | mcskatkat <mc_skatkat@hotmail.com> | 2023-07-11 00:50:33 +0300 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2023-09-01 14:31:57 +1000 |
commit | e0a148060025a75f4051e5009d9dc3728cb2bc75 (patch) | |
tree | 3fc1045f4bcea605af3ec862bc0378fcc51966d2 /tests/basics/string_format_modulo.py | |
parent | 83f2f36b9e5fea90a947aada5e1c8065468c0e70 (diff) |
py/objstr: Fix `str % {}` edge case.
Eliminate `TypeError` when format string contains no named conversions.
This matches CPython behavior.
Signed-off-by: mcskatkat <mc_skatkat@hotmail.com>
Diffstat (limited to 'tests/basics/string_format_modulo.py')
-rw-r--r-- | tests/basics/string_format_modulo.py | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/tests/basics/string_format_modulo.py b/tests/basics/string_format_modulo.py index 14b4a6a48..7bddd9675 100644 --- a/tests/basics/string_format_modulo.py +++ b/tests/basics/string_format_modulo.py @@ -51,8 +51,9 @@ print('%c' % True) # Should be able to print dicts; in this case they aren't used # to lookup keywords in formats like %(foo)s -print('%s' % {}) -print('%s' % ({},)) +print('%s' % {}) # dict treated as the single (positional) arg to % +print('%s' % ({},)) # dict is the first (and only) arg in the positional arg tuple +print('foo' % {}) # no error, dict treated as an empty map of named args # Cases when "*" used and there's not enough values total try: @@ -65,7 +66,11 @@ except TypeError: print("TypeError") print("%(foo)s" % {"foo": "bar", "baz": False}) -print("%s %(foo)s %(foo)s" % {"foo": 1}) +print("%s %(foo)s %(foo)s" % {"foo": 1}) # dict consumed positionally, then used as map - ok +try: + print("%(foo)s %s %(foo)s" % {"foo": 1}) # used as map, then positionally - not enough args +except TypeError: + print("TypeError") try: print("%(foo)s" % {}) except KeyError: |