summaryrefslogtreecommitdiff
path: root/tests
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
parent6738c1dded8e436686f85008ec0a4fc47406ab7a (diff)
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')
-rw-r--r--tests/basics/builtin_hash.py12
-rw-r--r--tests/cmdline/cmd_showbc.py.exp2
2 files changed, 12 insertions, 2 deletions
diff --git a/tests/basics/builtin_hash.py b/tests/basics/builtin_hash.py
index d7615c3ec..b6b2ad15c 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")
diff --git a/tests/cmdline/cmd_showbc.py.exp b/tests/cmdline/cmd_showbc.py.exp
index 02e367654..6231d3fe9 100644
--- a/tests/cmdline/cmd_showbc.py.exp
+++ b/tests/cmdline/cmd_showbc.py.exp
@@ -83,7 +83,7 @@ arg names:
59 BINARY_OP 5 __add__
60 STORE_FAST 8
61 LOAD_FAST 0
-62 UNARY_OP 3
+62 UNARY_OP 4
63 STORE_FAST 9
64 LOAD_FAST 0
65 UNARY_OP 0