summaryrefslogtreecommitdiffstatshomepage
path: root/py/obj.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/obj.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/obj.c')
-rw-r--r--py/obj.c62
1 files changed, 7 insertions, 55 deletions
diff --git a/py/obj.c b/py/obj.c
index 1b42377ed8..2b5585be42 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -147,61 +147,6 @@ bool mp_obj_is_callable(mp_obj_t o_in) {
return mp_obj_instance_is_callable(o_in);
}
-mp_int_t mp_obj_hash(mp_obj_t o_in) {
- if (o_in == mp_const_false) {
- return 0; // needs to hash to same as the integer 0, since False==0
- } else if (o_in == mp_const_true) {
- return 1; // needs to hash to same as the integer 1, since True==1
- } else if (MP_OBJ_IS_SMALL_INT(o_in)) {
- return MP_OBJ_SMALL_INT_VALUE(o_in);
- } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_int)) {
- return mp_obj_int_hash(o_in);
- } else if (MP_OBJ_IS_STR(o_in) || MP_OBJ_IS_TYPE(o_in, &mp_type_bytes)) {
- return mp_obj_str_get_hash(o_in);
- } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_NoneType)) {
- return (mp_int_t)o_in;
- } else if (MP_OBJ_IS_FUN(o_in)) {
- return (mp_int_t)o_in;
- } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_tuple)) {
- 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_instance_type(mp_obj_get_type(o_in))) {
- // if a valid __hash__ method exists, use it
- 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 classes
-
-error:
- if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
- nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "unhashable type"));
- } else {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
- "unhashable type: '%s'", mp_obj_get_type_str(o_in)));
- }
-}
-
// This function implements the '==' operator (and so the inverse of '!=').
//
// From the Python language reference:
@@ -540,3 +485,10 @@ void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flag
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "object with buffer protocol required"));
}
}
+
+mp_obj_t mp_generic_unary_op(mp_uint_t op, mp_obj_t o_in) {
+ switch (op) {
+ case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT((mp_uint_t)o_in);
+ default: return MP_OBJ_NULL; // op not supported
+ }
+}