From c1d9bbc3453454aceb28f51e72e4aeb8ef1c12eb Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 30 Jan 2014 04:37:19 +0200 Subject: [PATCH] Implement __bool__ and __len__ via unary_op virtual method for all types. __bool__() and __len__() are just the same as __neg__() or __invert__(), and require efficient dispatching implementation (not requiring search/lookup). type->unary_op() is just the right choice for this short of adding standalone virtual method(s) to already big mp_obj_type_t structure. --- py/obj.c | 10 ++++++++-- py/objarray.c | 10 ++++++++++ py/objbool.c | 2 +- py/objcomplex.c | 2 +- py/objdict.c | 3 ++- py/objfloat.c | 2 +- py/objint_longlong.c | 2 +- py/objlist.c | 3 ++- py/objnone.c | 2 +- py/objtuple.c | 3 ++- py/runtime.c | 15 ++++++++++----- py/runtime0.h | 5 ++++- tests/basics/true-value.py | 9 +++++++++ 13 files changed, 52 insertions(+), 16 deletions(-) diff --git a/py/obj.c b/py/obj.c index ce555efa6d..983718004b 100644 --- a/py/obj.c +++ b/py/obj.c @@ -251,9 +251,15 @@ mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) { len = seq_len; } else if (MP_OBJ_IS_TYPE(o_in, &dict_type)) { len = mp_obj_dict_len(o_in); - } else if (MP_OBJ_IS_TYPE(o_in, &array_type)) { - len = mp_obj_array_len(o_in); } else { + mp_obj_type_t *type = mp_obj_get_type(o_in); + if (type->unary_op != NULL) { + mp_obj_t result = type->unary_op(RT_UNARY_OP_LEN, o_in); + if (result != MP_OBJ_NULL) { + return result; + } + } + return MP_OBJ_NULL; } return MP_OBJ_NEW_SMALL_INT(len); diff --git a/py/objarray.c b/py/objarray.c index 42dbfcda05..c595d217c1 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -189,6 +189,15 @@ static mp_obj_t mp_builtin_bytearray(mp_obj_t arg) { } MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bytearray_obj, mp_builtin_bytearray); +static mp_obj_t array_unary_op(int op, mp_obj_t o_in) { + mp_obj_array_t *o = o_in; + switch (op) { + case RT_UNARY_OP_BOOL: return MP_BOOL(o->len != 0); + case RT_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len); + default: return MP_OBJ_NULL; // op not supported + } +} + static mp_obj_t array_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { mp_obj_array_t *o = lhs; switch (op) { @@ -245,6 +254,7 @@ const mp_obj_type_t array_type = { .print = array_print, .make_new = array_make_new, .getiter = array_iterator_new, + .unary_op = array_unary_op, .binary_op = array_binary_op, .store_item = array_store_item, .methods = array_type_methods, diff --git a/py/objbool.c b/py/objbool.c index 53b2bf8ed7..3a3ac3eb87 100644 --- a/py/objbool.c +++ b/py/objbool.c @@ -36,7 +36,7 @@ static mp_obj_t bool_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp static mp_obj_t bool_unary_op(int op, mp_obj_t o_in) { machine_int_t value = ((mp_obj_bool_t*)o_in)->value; switch (op) { - case RT_UNARY_OP_NOT: if (value) { return mp_const_false; } else { return mp_const_true; } + case RT_UNARY_OP_BOOL: return o_in; case RT_UNARY_OP_POSITIVE: return MP_OBJ_NEW_SMALL_INT(value); case RT_UNARY_OP_NEGATIVE: return MP_OBJ_NEW_SMALL_INT(-value); case RT_UNARY_OP_INVERT: diff --git a/py/objcomplex.c b/py/objcomplex.c index 24762e8b11..5b4df0da66 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -73,7 +73,7 @@ static mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const static mp_obj_t complex_unary_op(int op, mp_obj_t o_in) { mp_obj_complex_t *o = o_in; switch (op) { - case RT_UNARY_OP_NOT: if (o->real != 0 || o->imag != 0) { return mp_const_true;} else { return mp_const_false; } + case RT_UNARY_OP_BOOL: return MP_BOOL(o->real != 0 || o->imag != 0); case RT_UNARY_OP_POSITIVE: return o_in; case RT_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag); default: return MP_OBJ_NULL; // op not supported diff --git a/py/objdict.c b/py/objdict.c index 93ff1af90a..f78a7358b2 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -46,7 +46,8 @@ static mp_obj_t dict_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp static mp_obj_t dict_unary_op(int op, mp_obj_t self_in) { mp_obj_dict_t *self = self_in; switch (op) { - case RT_UNARY_OP_NOT: if (self->map.used == 0) { return mp_const_true; } else { return mp_const_false; } + case RT_UNARY_OP_BOOL: return MP_BOOL(self->map.used != 0); + case RT_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->map.used); default: return MP_OBJ_NULL; // op not supported for None } } diff --git a/py/objfloat.c b/py/objfloat.c index 69fd65e199..9caeaf7686 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -47,7 +47,7 @@ static mp_obj_t float_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m static mp_obj_t float_unary_op(int op, mp_obj_t o_in) { mp_obj_float_t *o = o_in; switch (op) { - case RT_UNARY_OP_NOT: if (o->value != 0) { return mp_const_true;} else { return mp_const_false; } + case RT_UNARY_OP_BOOL: return MP_BOOL(o->value != 0); case RT_UNARY_OP_POSITIVE: return o_in; case RT_UNARY_OP_NEGATIVE: return mp_obj_new_float(-o->value); default: return NULL; // op not supported diff --git a/py/objint_longlong.c b/py/objint_longlong.c index 38a01837e1..a59bcf061e 100644 --- a/py/objint_longlong.c +++ b/py/objint_longlong.c @@ -35,7 +35,7 @@ void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj mp_obj_t int_unary_op(int op, mp_obj_t o_in) { mp_obj_int_t *o = o_in; switch (op) { - case RT_UNARY_OP_NOT: return MP_BOOL(o->val != 0); // TODO: implements RT_UNARY_OP_BOOL + case RT_UNARY_OP_BOOL: return MP_BOOL(o->val != 0); case RT_UNARY_OP_POSITIVE: return o_in; case RT_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val); case RT_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val); diff --git a/py/objlist.c b/py/objlist.c index fb68e2c566..81040e3b8e 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -125,7 +125,8 @@ static bool list_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) { static mp_obj_t list_unary_op(int op, mp_obj_t self_in) { mp_obj_list_t *self = self_in; switch (op) { - case RT_UNARY_OP_NOT: if (self->len == 0) { return mp_const_true; } else { return mp_const_false; } + case RT_UNARY_OP_BOOL: return MP_BOOL(self->len != 0); + case RT_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len); default: return MP_OBJ_NULL; // op not supported for None } } diff --git a/py/objnone.c b/py/objnone.c index 4151403b21..7af0b29b2d 100644 --- a/py/objnone.c +++ b/py/objnone.c @@ -18,7 +18,7 @@ static void none_print(void (*print)(void *env, const char *fmt, ...), void *env static mp_obj_t none_unary_op(int op, mp_obj_t o_in) { switch (op) { - case RT_UNARY_OP_NOT: return mp_const_true; + case RT_UNARY_OP_BOOL: return mp_const_false; default: return MP_OBJ_NULL; // op not supported for None } } diff --git a/py/objtuple.c b/py/objtuple.c index 754fe4b662..5f1744ea30 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -76,7 +76,8 @@ static mp_obj_t tuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m static mp_obj_t tuple_unary_op(int op, mp_obj_t self_in) { mp_obj_tuple_t *self = self_in; switch (op) { - case RT_UNARY_OP_NOT: if (self->len == 0) { return mp_const_true; } else { return mp_const_false; } + case RT_UNARY_OP_BOOL: return MP_BOOL(self->len != 0); + case RT_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len); default: return MP_OBJ_NULL; // op not supported for None } } diff --git a/py/runtime.c b/py/runtime.c index 6dd6921599..6b3c8dc1d0 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -317,15 +317,20 @@ int rt_is_true(mp_obj_t arg) { } else if (arg == mp_const_true) { return 1; } else { + mp_obj_type_t *type = mp_obj_get_type(arg); + if (type->unary_op != NULL) { + mp_obj_t result = type->unary_op(RT_UNARY_OP_BOOL, arg); + if (result != NULL) { + return result == mp_const_true; + } + } + mp_obj_t len = mp_obj_len_maybe(arg); if (len != MP_OBJ_NULL) { // obj has a length, truth determined if len != 0 return len != MP_OBJ_NEW_SMALL_INT(0); } else { - // TODO check for __bool__ method - // TODO check floats and complex numbers - - // any other obj is true (TODO is that correct?) + // any other obj is true per Python semantics return 1; } } @@ -476,7 +481,7 @@ mp_obj_t rt_unary_op(int op, mp_obj_t arg) { if (MP_OBJ_IS_SMALL_INT(arg)) { mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg); switch (op) { - case RT_UNARY_OP_NOT: if (val == 0) { return mp_const_true;} else { return mp_const_false; } + case RT_UNARY_OP_BOOL: return MP_BOOL(val != 0); case RT_UNARY_OP_POSITIVE: break; case RT_UNARY_OP_NEGATIVE: val = -val; break; case RT_UNARY_OP_INVERT: val = ~val; break; diff --git a/py/runtime0.h b/py/runtime0.h index cd82b1412b..a0f553a896 100644 --- a/py/runtime0.h +++ b/py/runtime0.h @@ -1,8 +1,11 @@ typedef enum { - RT_UNARY_OP_NOT, // TODO remove this op since it's no longer needed + RT_UNARY_OP_BOOL, // __bool__ + RT_UNARY_OP_LEN, // __len__ RT_UNARY_OP_POSITIVE, RT_UNARY_OP_NEGATIVE, RT_UNARY_OP_INVERT, + // Used only for CPython-compatible codegeneration + RT_UNARY_OP_NOT, } rt_unary_op_t; typedef enum { diff --git a/tests/basics/true-value.py b/tests/basics/true-value.py index 6ba410ddc2..1dd547f326 100644 --- a/tests/basics/true-value.py +++ b/tests/basics/true-value.py @@ -3,9 +3,18 @@ if not False: print("False") +if not None: + print("None") + if not 0: print("0") +if not 0.0: + print("float 0") + +if not 0+0j: + print("complex 0") + if not "": print("Empty string") if "foo":