Skip to content

Commit 327a3e2

Browse files
committed
Merge pull request adafruit#435 from dhylands/str-modulo-float
Allow floating point arguments with %d,i,u,o,x,X formats
2 parents 23419a2 + f81a49e commit 327a3e2

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

py/objstr.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,15 @@ static bool arg_looks_numeric(mp_obj_t arg) {
528528
;
529529
}
530530

531+
static machine_int_t arg_as_int(mp_obj_t arg) {
532+
#if MICROPY_ENABLE_FLOAT
533+
if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
534+
return mp_obj_get_float(arg);
535+
}
536+
#endif
537+
return mp_obj_get_int(arg);
538+
}
539+
531540
mp_obj_t str_format(uint n_args, const mp_obj_t *args) {
532541
assert(MP_OBJ_IS_STR(args[0]));
533542

@@ -991,7 +1000,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t
9911000
case 'd':
9921001
case 'i':
9931002
case 'u':
994-
pfenv_print_int(&pfenv_vstr, mp_obj_get_int(arg), 1, 10, 'a', flags, fill, width);
1003+
pfenv_print_int(&pfenv_vstr, arg_as_int(arg), 1, 10, 'a', flags, fill, width);
9951004
break;
9961005

9971006
#if MICROPY_ENABLE_FLOAT
@@ -1009,7 +1018,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t
10091018
if (alt) {
10101019
flags |= PF_FLAG_SHOW_PREFIX;
10111020
}
1012-
pfenv_print_int(&pfenv_vstr, mp_obj_get_int(arg), 1, 8, 'a', flags, fill, width);
1021+
pfenv_print_int(&pfenv_vstr, arg_as_int(arg), 1, 8, 'a', flags, fill, width);
10131022
break;
10141023

10151024
case 'r':
@@ -1034,14 +1043,14 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t
10341043
if (alt) {
10351044
flags |= PF_FLAG_SHOW_PREFIX;
10361045
}
1037-
pfenv_print_int(&pfenv_vstr, mp_obj_get_int(arg), 1, 16, 'a', flags, fill, width);
1046+
pfenv_print_int(&pfenv_vstr, arg_as_int(arg), 1, 16, 'a', flags, fill, width);
10381047
break;
10391048

10401049
case 'X':
10411050
if (alt) {
10421051
flags |= PF_FLAG_SHOW_PREFIX;
10431052
}
1044-
pfenv_print_int(&pfenv_vstr, mp_obj_get_int(arg), 1, 16, 'A', flags, fill, width);
1053+
pfenv_print_int(&pfenv_vstr, arg_as_int(arg), 1, 16, 'A', flags, fill, width);
10451054
break;
10461055

10471056
default:

tests/basics/string-format-modulo.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
except TypeError:
2222
print("TypeError")
2323

24+
print("%s" % True)
25+
print("%s" % 1)
26+
print("%s" % 1.0)
27+
28+
print("%r" % True)
29+
print("%r" % 1)
30+
print("%r" % 1.0)
31+
2432
print("%c" % 48)
2533
print("%c" % 'a')
2634
print("%10s" % 'abc')
@@ -29,9 +37,20 @@
2937
print("%+d" % 10)
3038
print("% d" % 10)
3139
print("%d" % -10)
40+
print("%d" % 1.0)
41+
print("%d" % True)
42+
print("%i" % -10)
43+
print("%i" % 1.0)
44+
print("%i" % True)
45+
print("%u" % -10)
46+
print("%u" % 1.0)
47+
print("%u" % True)
3248
print("%x" % 18)
49+
print("%x" % 18.0)
3350
print("%o" % 18)
51+
print("%o" % 18.0)
3452
print("%X" % 18)
53+
print("%X" % 18.0)
3554
print("%#x" % 18)
3655
print("%#X" % 18)
3756
print("%#6x" % 18)

0 commit comments

Comments
 (0)