diff options
Diffstat (limited to 'py/runtime.c')
-rw-r--r-- | py/runtime.c | 39 |
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_)); } |