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

@@ -374,7 +374,7 @@ const byte mp_unary_op_method_name[MP_UNARY_OP_NUM_RUNTIME] = {
[MP_UNARY_OP_BOOL] = MP_QSTR___bool__,
[MP_UNARY_OP_LEN] = MP_QSTR___len__,
[MP_UNARY_OP_HASH] = MP_QSTR___hash__,
[MP_UNARY_OP_INT] = MP_QSTR___int__,
[MP_UNARY_OP_INT_MAYBE] = MP_QSTR___int__,
#if MICROPY_PY_ALL_SPECIAL_METHODS
[MP_UNARY_OP_POSITIVE] = MP_QSTR___pos__,
[MP_UNARY_OP_NEGATIVE] = MP_QSTR___neg__,
@@ -432,7 +432,7 @@ STATIC mp_obj_t instance_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
// __hash__ must return a small int
val = MP_OBJ_NEW_SMALL_INT(mp_obj_get_int_truncated(val));
break;
case MP_UNARY_OP_INT:
case MP_UNARY_OP_INT_MAYBE:
// Must return int
if (!mp_obj_is_int(val)) {
mp_raise_TypeError(NULL);