Browse Source

py/objbool: Defer bool's unary op implementation to small int.

Similar to how binary op already works.  Common unary operations already
have fast paths for bool so there's no need to have explicit handling of
ops in bool_unary_op, especially since they have the same behaviour as
integers.
pull/2441/merge
Damien George 8 years ago
parent
commit
eca1408f16
  1. 13
      py/objbool.c

13
py/objbool.c

@ -66,16 +66,11 @@ STATIC mp_obj_t bool_make_new(const mp_obj_type_t *type_in, size_t n_args, size_
}
STATIC mp_obj_t bool_unary_op(mp_uint_t op, mp_obj_t o_in) {
mp_int_t value = ((mp_obj_bool_t*)MP_OBJ_TO_PTR(o_in))->value;
switch (op) {
case MP_UNARY_OP_BOOL: return o_in;
// needs to hash to the same value as if converting to an integer
case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT(value);
case MP_UNARY_OP_POSITIVE: return MP_OBJ_NEW_SMALL_INT(value);
case MP_UNARY_OP_NEGATIVE: return MP_OBJ_NEW_SMALL_INT(-value);
case MP_UNARY_OP_INVERT: return MP_OBJ_NEW_SMALL_INT(~value);
default: return MP_OBJ_NULL; // op not supported
if (op == MP_UNARY_OP_LEN) {
return MP_OBJ_NULL;
}
mp_obj_bool_t *self = MP_OBJ_TO_PTR(o_in);
return mp_unary_op(op, MP_OBJ_NEW_SMALL_INT(self->value));
}
STATIC mp_obj_t bool_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {

Loading…
Cancel
Save