summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Lechner <david@pybricks.com>2023-01-18 13:59:54 -0600
committerDamien George <damien@micropython.org>2023-05-19 12:06:17 +1000
commit2fe6d4eb86a8496620a5db0958972ad5573932fb (patch)
tree7b3db441a51da677356689ce2f92e87542bdd245
parent8491eb190f2ea27f113c0cc7c0e619807a84f7ed (diff)
py/objdict: Fix __hash__ for dict_view types.
This adds a unary_op implementation for the dict_view type that makes the implementation of `hash()` for these types compatible with CPython. Signed-off-by: David Lechner <david@pybricks.com>
-rw-r--r--py/objdict.c10
-rw-r--r--tests/basics/dict_views.py18
2 files changed, 28 insertions, 0 deletions
diff --git a/py/objdict.c b/py/objdict.c
index baad8a1b9..a621c4fea 100644
--- a/py/objdict.c
+++ b/py/objdict.c
@@ -515,6 +515,15 @@ STATIC void dict_view_print(const mp_print_t *print, mp_obj_t self_in, mp_print_
mp_print_str(print, "])");
}
+STATIC mp_obj_t dict_view_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
+ mp_obj_dict_view_t *o = MP_OBJ_TO_PTR(o_in);
+ // only dict.values() supports __hash__.
+ if (op == MP_UNARY_OP_HASH && o->kind == MP_DICT_VIEW_VALUES) {
+ return MP_OBJ_NEW_SMALL_INT((mp_uint_t)o_in);
+ }
+ return MP_OBJ_NULL;
+}
+
STATIC mp_obj_t dict_view_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
// only supported for the 'keys' kind until sets and dicts are refactored
mp_obj_dict_view_t *o = MP_OBJ_TO_PTR(lhs_in);
@@ -532,6 +541,7 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE(
MP_QSTR_dict_view,
MP_TYPE_FLAG_ITER_IS_GETITER,
print, dict_view_print,
+ unary_op, dict_view_unary_op,
binary_op, dict_view_binary_op,
iter, dict_view_getiter
);
diff --git a/tests/basics/dict_views.py b/tests/basics/dict_views.py
index 7ebcc1f56..a82f47b6b 100644
--- a/tests/basics/dict_views.py
+++ b/tests/basics/dict_views.py
@@ -18,4 +18,22 @@ try:
except TypeError:
print('TypeError')
+# keys dict_view is not hashable
+
+try:
+ hash({}.keys())
+except TypeError:
+ print('TypeError')
+
+# values dict_view is hashable
+
+print(type(hash({}.values())))
+
+# items dict_view is not hashable
+
+try:
+ hash({}.items())
+except TypeError:
+ print('TypeError')
+
# set operations still to come