From f2b796e7c76e43b10258b6a87bd84884fa879984 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 15 Jan 2014 22:45:20 +0200 Subject: [PATCH] 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. --- py/objstr.c | 3 ++- tests/basics/tests/string-format.py | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 tests/basics/tests/string-format.py diff --git a/py/objstr.c b/py/objstr.c index 0621a8df75..7c1be50cc0 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -270,7 +270,8 @@ mp_obj_t str_format(int n_args, const mp_obj_t *args) { str++; if (*str == '{') { vstr_add_char(vstr, '{'); - } else if (*str == '}') { + } else { + while (*str != '}') str++; if (arg_i >= n_args) { nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "tuple index out of range")); } diff --git a/tests/basics/tests/string-format.py b/tests/basics/tests/string-format.py new file mode 100644 index 0000000000..ba51e0890b --- /dev/null +++ b/tests/basics/tests/string-format.py @@ -0,0 +1,8 @@ +print("{}-{}".format(1, [4, 5])) +print("{0}-{1}".format(1, [4, 5])) +print("{:x}".format(1)) +print("{!r}".format(2)) +# TODO +#print("{1}-{0}".format(1, [4, 5])) +#print("{:x}".format(0x10)) +#print("{!r}".format("foo"))