py/objfloat: Fix binary ops with incompatible objects.
These are now returned as "operation not supported" instead of raising TypeError. In particular, this fixes equality for float vs incompatible types, which now properly results in False instead of exception. This also paves the road to support reverse operation (e.g. __radd__) with float objects. This is achieved by introducing mp_obj_get_float_maybe(), similar to existing mp_obj_get_int_maybe().
This commit is contained in:
@@ -240,7 +240,11 @@ STATIC void mp_obj_float_divmod(mp_float_t *x, mp_float_t *y) {
|
||||
}
|
||||
|
||||
mp_obj_t mp_obj_float_binary_op(mp_binary_op_t op, mp_float_t lhs_val, mp_obj_t rhs_in) {
|
||||
mp_float_t rhs_val = mp_obj_get_float(rhs_in); // can be any type, this function will convert to float (if possible)
|
||||
mp_float_t rhs_val;
|
||||
if (!mp_obj_get_float_maybe(rhs_in, &rhs_val)) {
|
||||
return MP_OBJ_NULL; // op not supported
|
||||
}
|
||||
|
||||
switch (op) {
|
||||
case MP_BINARY_OP_ADD:
|
||||
case MP_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
|
||||
|
||||
Reference in New Issue
Block a user