diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2008-07-15 15:46:38 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2008-07-15 15:46:38 (GMT) |
commit | d1abd25ed8e14a64da21d17ece73c49390b9b083 (patch) | |
tree | 21c0fbf58c0b0a05268ccdf538716f2f75dd5576 /Lib/test/test_richcmp.py | |
parent | e65282114e96efb9e7eee77c57244943b746f6fe (diff) | |
download | cpython-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_richcmp.py')
-rw-r--r-- | Lib/test/test_richcmp.py | 64 |
1 files changed, 2 insertions, 62 deletions
diff --git a/Lib/test/test_richcmp.py b/Lib/test/test_richcmp.py index 30dac8f..435de75 100644 --- a/Lib/test/test_richcmp.py +++ b/Lib/test/test_richcmp.py @@ -48,8 +48,7 @@ class Vector: def __setitem__(self, i, v): self.data[i] = v - def __hash__(self): - raise TypeError("Vectors cannot be hashed") + __hash__ = None # Vectors cannot be hashed def __bool__(self): raise TypeError("Vectors cannot be used in Boolean contexts") @@ -85,35 +84,6 @@ class Vector: raise ValueError("Cannot compare vectors of different length") return other - -class SimpleOrder(object): - """ - A simple class that defines order but not full comparison. - """ - - def __init__(self, value): - self.value = value - - def __lt__(self, other): - if not isinstance(other, SimpleOrder): - return True - return self.value < other.value - - def __gt__(self, other): - if not isinstance(other, SimpleOrder): - return False - return self.value > other.value - - -class DumbEqualityWithoutHash(object): - """ - A class that define __eq__, but no __hash__: it shouldn't be hashable. - """ - - def __eq__(self, other): - return False - - opmap = { "lt": (lambda a,b: a< b, operator.lt, operator.__lt__), "le": (lambda a,b: a<=b, operator.le, operator.__le__), @@ -360,38 +330,8 @@ class ListTest(unittest.TestCase): self.assertIs(op(x, y), True) -class HashableTest(unittest.TestCase): - """ - Test hashability of classes with rich operators defined. - """ - - def test_simpleOrderHashable(self): - """ - A class that only defines __gt__ and/or __lt__ should be hashable. - """ - a = SimpleOrder(1) - b = SimpleOrder(2) - self.assert_(a < b) - self.assert_(b > a) - self.assert_(a.__hash__ is not None) - - def test_notHashableException(self): - """ - If a class is not hashable, it should raise a TypeError with an - understandable message. - """ - a = DumbEqualityWithoutHash() - try: - hash(a) - except TypeError as e: - self.assertEquals(str(e), - "unhashable type: 'DumbEqualityWithoutHash'") - else: - raise support.TestFailed("Should not be here") - - def test_main(): - support.run_unittest(VectorTest, NumberTest, MiscTest, DictTest, ListTest, HashableTest) + support.run_unittest(VectorTest, NumberTest, MiscTest, DictTest, ListTest) if __name__ == "__main__": test_main() |