Skip to content

Commit eca1408

Browse files
committed
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.
1 parent 3be4f88 commit eca1408

1 file changed

Lines changed: 4 additions & 9 deletions

File tree

py/objbool.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,11 @@ STATIC mp_obj_t bool_make_new(const mp_obj_type_t *type_in, size_t n_args, size_
6666
}
6767

6868
STATIC mp_obj_t bool_unary_op(mp_uint_t op, mp_obj_t o_in) {
69-
mp_int_t value = ((mp_obj_bool_t*)MP_OBJ_TO_PTR(o_in))->value;
70-
switch (op) {
71-
case MP_UNARY_OP_BOOL: return o_in;
72-
// needs to hash to the same value as if converting to an integer
73-
case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT(value);
74-
case MP_UNARY_OP_POSITIVE: return MP_OBJ_NEW_SMALL_INT(value);
75-
case MP_UNARY_OP_NEGATIVE: return MP_OBJ_NEW_SMALL_INT(-value);
76-
case MP_UNARY_OP_INVERT: return MP_OBJ_NEW_SMALL_INT(~value);
77-
default: return MP_OBJ_NULL; // op not supported
69+
if (op == MP_UNARY_OP_LEN) {
70+
return MP_OBJ_NULL;
7871
}
72+
mp_obj_bool_t *self = MP_OBJ_TO_PTR(o_in);
73+
return mp_unary_op(op, MP_OBJ_NEW_SMALL_INT(self->value));
7974
}
8075

8176
STATIC mp_obj_t bool_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {

0 commit comments

Comments
 (0)