diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-01-30 04:37:19 +0200 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-01-30 04:37:19 +0200 |
commit | c1d9bbc3453454aceb28f51e72e4aeb8ef1c12eb (patch) | |
tree | db7cd93c99583a858ef94b3dab95b882832da251 /py/runtime.c | |
parent | cdd2c62e07549e36dba00bc37d7ba7a4cd41ad50 (diff) | |
download | micropython-c1d9bbc3453454aceb28f51e72e4aeb8ef1c12eb.tar.gz micropython-c1d9bbc3453454aceb28f51e72e4aeb8ef1c12eb.zip |
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.
Diffstat (limited to 'py/runtime.c')
-rw-r--r-- | py/runtime.c | 15 |
1 files changed, 10 insertions, 5 deletions
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; |