py/obj: Add support for __float__ and __complex__ functions.

This commit is contained in:
Andrew Leech
2022-07-04 17:35:46 +10:00
committed by Damien George
parent fa15aed0f7
commit 1e87b56219
9 changed files with 130 additions and 3 deletions

View File

@@ -319,6 +319,15 @@ 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 MICROPY_PY_BUILTINS_COMPLEX
|| op == MP_UNARY_OP_COMPLEX_MAYBE
#endif
) {
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