Skip to content

Commit

Permalink
py/objstr: In str.format, handle case of no format spec for string arg.
Browse files Browse the repository at this point in the history
Handles, eg, "{:>20}".format("foo"), where there is no explicit spec for
the type of the argument.
  • Loading branch information
dpgeorge committed Jan 4, 2016
1 parent 824f83f commit d4df8f4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 1 addition & 4 deletions py/objstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1259,10 +1259,7 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa
}

switch (type) {
case '\0':
mp_obj_print_helper(&print, arg, PRINT_STR);
break;

case '\0': // no explicit format type implies 's'
case 's': {
mp_uint_t slen;
const char *s = mp_obj_str_get_data(arg, &slen);
Expand Down
4 changes: 4 additions & 0 deletions tests/basics/string_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def test(fmt, *args):
test("{:@=6d}", -123)
test("{:06d}", -123)

test("{:>20}", "foo")
test("{:^20}", "foo")
test("{:<20}", "foo")

print("{foo}/foo".format(foo="bar"))
print("{}".format(123, foo="bar"))
print("{}-{foo}".format(123, foo="bar"))
Expand Down

0 comments on commit d4df8f4

Please sign in to comment.