From 91cbe6033a8fec6b11c414670f7ed75e9598857c Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 5 Apr 2014 12:50:43 +0300 Subject: [PATCH] py: Allow types to be hashable. Quite natural to have d[int] = handle_int . --- py/obj.c | 2 ++ tests/basics/types_hash.py | 6 ++++++ 2 files changed, 8 insertions(+) create mode 100644 tests/basics/types_hash.py diff --git a/py/obj.c b/py/obj.c index e177782a8b..34a48cc681 100644 --- a/py/obj.c +++ b/py/obj.c @@ -118,6 +118,8 @@ machine_int_t mp_obj_hash(mp_obj_t o_in) { return (machine_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 (machine_int_t)o_in; // TODO hash class and instances // TODO delegate to __hash__ method if it exists diff --git a/tests/basics/types_hash.py b/tests/basics/types_hash.py new file mode 100644 index 0000000000..2595097d2d --- /dev/null +++ b/tests/basics/types_hash.py @@ -0,0 +1,6 @@ +# Types are hashable +print(hash(type) != 0) +print(hash(int) != 0) +print(hash(list) != 0) +class Foo: pass +print(hash(Foo) != 0)