Skip to content

Commit f2b796e

Browse files
committed
str.format: Don't assume that '}' immediately follows '{', skip insides.
That at least makes stuff like "{:x}".format(1) to produce not completely broken output.
1 parent b99d9ea commit f2b796e

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

py/objstr.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ mp_obj_t str_format(int n_args, const mp_obj_t *args) {
270270
str++;
271271
if (*str == '{') {
272272
vstr_add_char(vstr, '{');
273-
} else if (*str == '}') {
273+
} else {
274+
while (*str != '}') str++;
274275
if (arg_i >= n_args) {
275276
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "tuple index out of range"));
276277
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
print("{}-{}".format(1, [4, 5]))
2+
print("{0}-{1}".format(1, [4, 5]))
3+
print("{:x}".format(1))
4+
print("{!r}".format(2))
5+
# TODO
6+
#print("{1}-{0}".format(1, [4, 5]))
7+
#print("{:x}".format(0x10))
8+
#print("{!r}".format("foo"))

0 commit comments

Comments
 (0)