summaryrefslogtreecommitdiffstatshomepage
path: root/py/objbool.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-05-11 12:25:19 +0000
committerDamien George <damien.p.george@gmail.com>2015-05-12 22:46:02 +0100
commitc2a4e4effc81d8ab21bb014e34355643e5ca0da2 (patch)
treed08f5921b2053c5d49ba7b10688eb8a771ddd7d0 /py/objbool.c
parent6738c1dded8e436686f85008ec0a4fc47406ab7a (diff)
downloadmicropython-c2a4e4effc81d8ab21bb014e34355643e5ca0da2.tar.gz
micropython-c2a4e4effc81d8ab21bb014e34355643e5ca0da2.zip
py: Convert hash API to use MP_UNARY_OP_HASH instead of ad-hoc function.
Hashing is now done using mp_unary_op function with MP_UNARY_OP_HASH as the operator argument. Hashing for int, str and bytes still go via fast-path in mp_unary_op since they are the most common objects which need to be hashed. This lead to quite a bit of code cleanup, and should be more efficient if anything. It saves 176 bytes code space on Thumb2, and 360 bytes on x86. The only loss is that the error message "unhashable type" is now the more generic "unsupported type for __hash__".
Diffstat (limited to 'py/objbool.c')
-rw-r--r--py/objbool.c5
1 files changed, 4 insertions, 1 deletions
diff --git a/py/objbool.c b/py/objbool.c
index 478491bfc2..fe33e961b6 100644
--- a/py/objbool.c
+++ b/py/objbool.c
@@ -69,18 +69,21 @@ STATIC mp_obj_t bool_unary_op(mp_uint_t op, mp_obj_t o_in) {
mp_int_t value = ((mp_obj_bool_t*)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);
// only bool needs to implement MP_UNARY_OP_NOT
case MP_UNARY_OP_NOT:
- default: // no other cases
if (value) {
return mp_const_false;
} else {
return mp_const_true;
}
+
+ default: return MP_OBJ_NULL; // op not supported
}
}