diff --git a/py/obj.c b/py/obj.c index dc69194672..64c52eeb76 100644 --- a/py/obj.c +++ b/py/obj.c @@ -326,7 +326,12 @@ bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value) { } else if (mp_obj_is_exact_type(arg, &mp_type_int)) { *value = mp_obj_int_get_checked(arg); } else { - return false; + arg = mp_unary_op(MP_UNARY_OP_INT_MAYBE, (mp_obj_t)arg); + if (arg != MP_OBJ_NULL) { + *value = mp_obj_int_get_checked(arg); + } else { + return false; + } } return true; } diff --git a/tests/basics/string_format_modulo.py b/tests/basics/string_format_modulo.py index 01f8e7ed24..14b4a6a484 100644 --- a/tests/basics/string_format_modulo.py +++ b/tests/basics/string_format_modulo.py @@ -13,6 +13,12 @@ print("=%s=" % [1, 2]) print("=%s=" % "str") print("=%r=" % "str") +# test calling __int__ +class A: + def __int__(self): + return 123 +print("%d" % A()) + try: print("=%s=%s=" % 1) except TypeError: