py: Change MP_UNARY_OP_INT to MP_UNARY_OP_INT_MAYBE.

To be consistent with MP_UNARY_OP_INT_FLOAT and MP_UNARY_OP_INT_COMPLEX,
and allow int() to first check if a type supports __int__ before trying
other things (as per CPython).

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George
2023-05-25 10:57:08 +10:00
parent 3ae78e803b
commit 48ffd6596e
11 changed files with 50 additions and 39 deletions

View File

@@ -48,10 +48,10 @@ STATIC mp_obj_t mp_obj_int_make_new(const mp_obj_type_t *type_in, size_t n_args,
case 0:
return MP_OBJ_NEW_SMALL_INT(0);
case 1:
if (mp_obj_is_int(args[0])) {
// already an int (small or long), just return it
return args[0];
case 1: {
mp_obj_t o = mp_unary_op(MP_UNARY_OP_INT_MAYBE, args[0]);
if (o != MP_OBJ_NULL) {
return o;
} else if (mp_obj_is_str_or_bytes(args[0])) {
// a string, parse it
size_t l;
@@ -62,8 +62,9 @@ STATIC mp_obj_t mp_obj_int_make_new(const mp_obj_type_t *type_in, size_t n_args,
return mp_obj_new_int_from_float(mp_obj_float_get(args[0]));
#endif
} else {
return mp_unary_op(MP_UNARY_OP_INT, args[0]);
mp_raise_TypeError_int_conversion(args[0]);
}
}
case 2:
default: {