summaryrefslogtreecommitdiffstatshomepage
path: root/tests/basics
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 /tests/basics
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 'tests/basics')
-rw-r--r--tests/basics/builtin_hash.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/tests/basics/builtin_hash.py b/tests/basics/builtin_hash.py
index d7615c3ec0..b6b2ad15cb 100644
--- a/tests/basics/builtin_hash.py
+++ b/tests/basics/builtin_hash.py
@@ -20,11 +20,12 @@ class A:
print(hash(A()))
print({A():1})
+# all user-classes have default __hash__
class B:
pass
hash(B())
-
+# if __eq__ is defined then default __hash__ is not used
class C:
def __eq__(self, another):
return True
@@ -32,3 +33,12 @@ try:
hash(C())
except TypeError:
print("TypeError")
+
+# __hash__ must return an int
+class D:
+ def __hash__(self):
+ return None
+try:
+ hash(D())
+except TypeError:
+ print("TypeError")