Skip to content

Commit d4c2bdd

Browse files
committed
py: Raise TypeError when trying to format non-int with %x,%o,%X.
This behaviour follows Python 3.5 standard (in 3.4 it's a DeprecationWarning which we'd rather make a TypeError).
1 parent f675ff3 commit d4c2bdd

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

py/objstr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t
12731273
if (alt) {
12741274
flags |= (PF_FLAG_SHOW_PREFIX | PF_FLAG_SHOW_OCTAL_LETTER);
12751275
}
1276-
pfenv_print_mp_int(&pfenv_vstr, arg_as_int(arg), 1, 8, 'a', flags, fill, width);
1276+
pfenv_print_mp_int(&pfenv_vstr, arg, 1, 8, 'a', flags, fill, width);
12771277
break;
12781278

12791279
case 'r':
@@ -1296,7 +1296,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t
12961296

12971297
case 'X':
12981298
case 'x':
1299-
pfenv_print_mp_int(&pfenv_vstr, arg_as_int(arg), 1, 16, *str - ('X' - 'A'), flags | alt, fill, width);
1299+
pfenv_print_mp_int(&pfenv_vstr, arg, 1, 16, *str - ('X' - 'A'), flags | alt, fill, width);
13001300
break;
13011301

13021302
default:

tests/float/string-format-modulo.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
print("%i" % 1.0)
66
print("%u" % 1.0)
77

8+
# these 3 have different behaviour in Python 3.x versions
9+
# uPy raises a TypeError, following Python 3.5 (earlier versions don't)
10+
#print("%x" % 18.0)
11+
#print("%o" % 18.0)
12+
#print("%X" % 18.0)
13+
814
print("%e" % 1.23456)
915
print("%E" % 1.23456)
1016
print("%f" % 1.23456)

0 commit comments

Comments
 (0)