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>
pull/10348/head
David Lechner 2023-01-18 13:59:54 -06:00 zatwierdzone przez Damien George
rodzic 8491eb190f
commit 2fe6d4eb86
2 zmienionych plików z 28 dodań i 0 usunięć

Wyświetl plik

@ -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
);

Wyświetl plik

@ -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