mirror of
https://github.com/micropython/micropython.git
synced 2026-01-08 21:20:13 +01:00
objstr: Implement "%(key)s" % {} formatting for strings and dicts.
Also, make sure that args to "*" format specifiers are bounds-checked properly and don't lead for segfaults in case of mismatch.
This commit is contained in:
@@ -48,3 +48,29 @@ print("%#X" % 18)
|
||||
print("%#6o" % 18)
|
||||
print("%#6x" % 18)
|
||||
print("%#06x" % 18)
|
||||
|
||||
print("%*d" % (5, 10))
|
||||
print("%*.*d" % (2, 2, 20))
|
||||
# TODO: Formatted incorrectly
|
||||
#print("%*.*d" % (5, 8, 20))
|
||||
|
||||
# Cases when "*" used and there's not enough values total
|
||||
try:
|
||||
print("%*s" % 5)
|
||||
except TypeError:
|
||||
print("TypeError")
|
||||
try:
|
||||
print("%*.*s" % (1, 15))
|
||||
except TypeError:
|
||||
print("TypeError")
|
||||
|
||||
print("%(foo)s" % {"foo": "bar", "baz": False})
|
||||
try:
|
||||
print("%(foo)s" % {})
|
||||
except KeyError:
|
||||
print("KeyError")
|
||||
# Using in "*" with dict got to fail
|
||||
try:
|
||||
print("%(foo)*s" % {"foo": "bar"})
|
||||
except TypeError:
|
||||
print("TypeError")
|
||||
|
||||
Reference in New Issue
Block a user