summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_hash.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2008-07-15 15:46:38 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2008-07-15 15:46:38 (GMT)
commitd1abd25ed8e14a64da21d17ece73c49390b9b083 (patch)
tree21c0fbf58c0b0a05268ccdf538716f2f75dd5576 /Lib/test/test_hash.py
parente65282114e96efb9e7eee77c57244943b746f6fe (diff)
downloadcpython-d1abd25ed8e14a64da21d17ece73c49390b9b083.zip
cpython-d1abd25ed8e14a64da21d17ece73c49390b9b083.tar.gz
cpython-d1abd25ed8e14a64da21d17ece73c49390b9b083.tar.bz2
Manual forward port of 64962 - use PyObject_HashNotImplemented as a tp_hash level indicator that the default hash implementation has not been inherited
Diffstat (limited to 'Lib/test/test_hash.py')
-rw-r--r--Lib/test/test_hash.py70
1 files changed, 69 insertions, 1 deletions
diff --git a/Lib/test/test_hash.py b/Lib/test/test_hash.py
index fb1699a..56974b8 100644
--- a/Lib/test/test_hash.py
+++ b/Lib/test/test_hash.py
@@ -1,9 +1,11 @@
# test the invariant that
# iff a==b then hash(a)==hash(b)
#
+# Also test that hash implementations are inherited as expected
import unittest
from test import support
+from collections import Hashable
class HashEqualityTestCase(unittest.TestCase):
@@ -37,8 +39,74 @@ class HashEqualityTestCase(unittest.TestCase):
self.same_hash(float(0.5), complex(0.5, 0.0))
+_default_hash = object.__hash__
+class DefaultHash(object): pass
+
+_FIXED_HASH_VALUE = 42
+class FixedHash(object):
+ def __hash__(self):
+ return _FIXED_HASH_VALUE
+
+class OnlyEquality(object):
+ def __eq__(self, other):
+ return self is other
+
+class OnlyInequality(object):
+ def __ne__(self, other):
+ return self is not other
+
+class OnlyCmp(object):
+ def __cmp__(self, other):
+ return cmp(id(self), id(other))
+
+class InheritedHashWithEquality(FixedHash, OnlyEquality): pass
+class InheritedHashWithInequality(FixedHash, OnlyInequality): pass
+class InheritedHashWithCmp(FixedHash, OnlyCmp): pass
+
+class NoHash(object):
+ __hash__ = None
+
+class HashInheritanceTestCase(unittest.TestCase):
+ default_expected = [object(),
+ DefaultHash(),
+ OnlyInequality(),
+ ]
+ fixed_expected = [FixedHash(),
+ InheritedHashWithEquality(),
+ InheritedHashWithInequality(),
+ InheritedHashWithCmp(),
+ ]
+ error_expected = [NoHash(),
+ OnlyEquality(),
+ OnlyCmp(),
+ ]
+
+ def test_default_hash(self):
+ for obj in self.default_expected:
+ self.assertEqual(hash(obj), _default_hash(obj))
+
+ def test_fixed_hash(self):
+ for obj in self.fixed_expected:
+ self.assertEqual(hash(obj), _FIXED_HASH_VALUE)
+
+ def test_error_hash(self):
+ for obj in self.error_expected:
+ self.assertRaises(TypeError, hash, obj)
+
+ def test_hashable(self):
+ objects = (self.default_expected +
+ self.fixed_expected)
+ for obj in objects:
+ self.assert_(isinstance(obj, Hashable), repr(obj))
+
+ def test_not_hashable(self):
+ for obj in self.error_expected:
+ self.assertFalse(isinstance(obj, Hashable), repr(obj))
+
+
def test_main():
- support.run_unittest(HashEqualityTestCase)
+ support.run_unittest(HashEqualityTestCase,
+ HashInheritanceTestCase)
if __name__ == "__main__":