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.
This commit is contained in:
Damien George 2016-09-30 16:46:31 +10:00
parent 3be4f886ce
commit eca1408f16
1 changed files with 4 additions and 9 deletions

View File

@ -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) { 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; if (op == MP_UNARY_OP_LEN) {
switch (op) { return MP_OBJ_NULL;
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
} }
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) { STATIC mp_obj_t bool_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {