summaryrefslogtreecommitdiff
path: root/py/runtime.c
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2023-05-25 10:57:08 +1000
committerDamien George <damien@micropython.org>2023-06-01 13:01:07 +1000
commit48ffd6596e7a4c185a81be233b46d3c99a83a7ac (patch)
tree3a15993f388746d0125b875c621a8508b5c8b731 /py/runtime.c
parent3ae78e803b69c4f1fd5dfe7eb732de7075f12e6c (diff)
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>
Diffstat (limited to 'py/runtime.c')
-rw-r--r--py/runtime.c39
1 files changed, 20 insertions, 19 deletions
diff --git a/py/runtime.c b/py/runtime.c
index 3434d9cc4..99f7f6109 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -277,7 +277,7 @@ mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg) {
case MP_UNARY_OP_HASH:
return arg;
case MP_UNARY_OP_POSITIVE:
- case MP_UNARY_OP_INT:
+ case MP_UNARY_OP_INT_MAYBE:
return arg;
case MP_UNARY_OP_NEGATIVE:
// check for overflow
@@ -324,32 +324,23 @@ mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg) {
// if arg==mp_const_none.
return mp_const_true;
}
- #if MICROPY_PY_BUILTINS_FLOAT
- if (op == MP_UNARY_OP_FLOAT_MAYBE
+ if (op == MP_UNARY_OP_INT_MAYBE
+ #if MICROPY_PY_BUILTINS_FLOAT
+ || op == MP_UNARY_OP_FLOAT_MAYBE
#if MICROPY_PY_BUILTINS_COMPLEX
|| op == MP_UNARY_OP_COMPLEX_MAYBE
#endif
+ #endif
) {
+ // These operators may return MP_OBJ_NULL if they are not supported by the type.
return MP_OBJ_NULL;
}
- #endif
- // With MP_UNARY_OP_INT, mp_unary_op() becomes a fallback for mp_obj_get_int().
- // In this case provide a more focused error message to not confuse, e.g. chr(1.0)
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
- if (op == MP_UNARY_OP_INT) {
- mp_raise_TypeError(MP_ERROR_TEXT("can't convert to int"));
- } else {
- mp_raise_TypeError(MP_ERROR_TEXT("unsupported type for operator"));
- }
+ mp_raise_TypeError(MP_ERROR_TEXT("unsupported type for operator"));
#else
- if (op == MP_UNARY_OP_INT) {
- mp_raise_msg_varg(&mp_type_TypeError,
- MP_ERROR_TEXT("can't convert %s to int"), mp_obj_get_type_str(arg));
- } else {
- mp_raise_msg_varg(&mp_type_TypeError,
- MP_ERROR_TEXT("unsupported type for %q: '%s'"),
- mp_unary_op_method_name[op], mp_obj_get_type_str(arg));
- }
+ mp_raise_msg_varg(&mp_type_TypeError,
+ MP_ERROR_TEXT("unsupported type for %q: '%s'"),
+ mp_unary_op_method_name[op], mp_obj_get_type_str(arg));
#endif
}
}
@@ -1700,6 +1691,16 @@ NORETURN void mp_raise_StopIteration(mp_obj_t arg) {
}
}
+NORETURN void mp_raise_TypeError_int_conversion(mp_const_obj_t arg) {
+ #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
+ (void)arg;
+ mp_raise_TypeError(MP_ERROR_TEXT("can't convert to int"));
+ #else
+ mp_raise_msg_varg(&mp_type_TypeError,
+ MP_ERROR_TEXT("can't convert %s to int"), mp_obj_get_type_str(arg));
+ #endif
+}
+
NORETURN void mp_raise_OSError(int errno_) {
mp_raise_type_arg(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno_));
}