summaryrefslogtreecommitdiffstatshomepage
path: root/py/objtype.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/objtype.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/objtype.c')
-rw-r--r--py/objtype.c24
1 files changed, 23 insertions, 1 deletions
diff --git a/py/objtype.c b/py/objtype.c
index c2c8d2e6df..7b293c9598 100644
--- a/py/objtype.c
+++ b/py/objtype.c
@@ -327,6 +327,7 @@ mp_obj_t mp_obj_instance_make_new(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t
const qstr mp_unary_op_method_name[] = {
[MP_UNARY_OP_BOOL] = MP_QSTR___bool__,
[MP_UNARY_OP_LEN] = MP_QSTR___len__,
+ [MP_UNARY_OP_HASH] = MP_QSTR___hash__,
#if MICROPY_PY_ALL_SPECIAL_METHODS
[MP_UNARY_OP_POSITIVE] = MP_QSTR___pos__,
[MP_UNARY_OP_NEGATIVE] = MP_QSTR___neg__,
@@ -355,8 +356,28 @@ STATIC mp_obj_t instance_unary_op(mp_uint_t op, mp_obj_t self_in) {
if (member[0] == MP_OBJ_SENTINEL) {
return mp_unary_op(op, self->subobj[0]);
} else if (member[0] != MP_OBJ_NULL) {
- return mp_call_function_1(member[0], self_in);
+ mp_obj_t val = mp_call_function_1(member[0], self_in);
+ // __hash__ must return a small int
+ if (op == MP_UNARY_OP_HASH) {
+ val = MP_OBJ_NEW_SMALL_INT(mp_obj_int_get_truncated(val));
+ }
+ return val;
} else {
+ if (op == MP_UNARY_OP_HASH) {
+ lookup.attr = MP_QSTR___eq__;
+ mp_obj_class_lookup(&lookup, self->base.type);
+ if (member[0] == MP_OBJ_NULL) {
+ // https://docs.python.org/3/reference/datamodel.html#object.__hash__
+ // "User-defined classes have __eq__() and __hash__() methods by default;
+ // with them, all objects compare unequal (except with themselves) and
+ // x.__hash__() returns an appropriate value such that x == y implies
+ // both that x is y and hash(x) == hash(y)."
+ return MP_OBJ_NEW_SMALL_INT((mp_uint_t)self_in);
+ }
+ // "A class that overrides __eq__() and does not define __hash__() will have its __hash__() implicitly set to None.
+ // When the __hash__() method of a class is None, instances of the class will raise an appropriate TypeError"
+ }
+
return MP_OBJ_NULL; // op not supported
}
}
@@ -835,6 +856,7 @@ const mp_obj_type_t mp_type_type = {
.print = type_print,
.make_new = type_make_new,
.call = type_call,
+ .unary_op = mp_generic_unary_op,
.attr = type_attr,
};