summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2015-05-04 22:41:32 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2015-05-05 22:48:19 +0300
commit37c6555b4419eb6027a345ee0c3e759d50da771f (patch)
tree1a0d27a9b002c97380e7a185b38e7481908350a7 /py
parent76677270218fb5732296e4fe1a0acbe05c8f818c (diff)
downloadmicropython-37c6555b4419eb6027a345ee0c3e759d50da771f.tar.gz
micropython-37c6555b4419eb6027a345ee0c3e759d50da771f.zip
obj: Handle user instance hash based on Python adhoc rules.
User instances are hashable by default (using __hash__ inherited from "object"). But if __eq__ is defined and __hash__ not defined in particular class, instance is not hashable.
Diffstat (limited to 'py')
-rw-r--r--py/obj.c26
1 files changed, 20 insertions, 6 deletions
diff --git a/py/obj.c b/py/obj.c
index 81adbe3933..1b42377ed8 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -166,20 +166,34 @@ mp_int_t mp_obj_hash(mp_obj_t o_in) {
return mp_obj_tuple_hash(o_in);
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_type)) {
return (mp_int_t)o_in;
- } else if (MP_OBJ_IS_OBJ(o_in)) {
+ } else if (mp_obj_is_instance_type(mp_obj_get_type(o_in))) {
// if a valid __hash__ method exists, use it
- mp_obj_t hash_method[2];
- mp_load_method_maybe(o_in, MP_QSTR___hash__, hash_method);
- if (hash_method[0] != MP_OBJ_NULL) {
- mp_obj_t hash_val = mp_call_method_n_kw(0, 0, hash_method);
+ mp_obj_t method[2];
+ mp_load_method_maybe(o_in, MP_QSTR___hash__, method);
+ if (method[0] != MP_OBJ_NULL) {
+ mp_obj_t hash_val = mp_call_method_n_kw(0, 0, method);
if (MP_OBJ_IS_INT(hash_val)) {
return mp_obj_int_get_truncated(hash_val);
}
+ goto error;
}
+
+ mp_load_method_maybe(o_in, MP_QSTR___eq__, method);
+ if (method[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_int_t)o_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"
}
- // TODO hash class and instances - in CPython by default user created classes' __hash__ resolves to their id
+ // TODO hash classes
+error:
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "unhashable type"));
} else {