Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_hash.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | 5f6d225e3c1d |
| children | 6768b2350c57 |
| rev | line source |
|---|---|
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
1 # test the invariant that |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
2 # iff a==b then hash(a)==hash(b) |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
3 # |
|
47961
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
4 # Also test that hash implementations are inherited as expected |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
5 |
| 17859 | 6 import unittest |
|
24364
aa0d2d51fbde
Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
7 from test import test_support |
|
47961
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
8 from collections import Hashable |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
9 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
10 |
| 17859 | 11 class HashEqualityTestCase(unittest.TestCase): |
| 12 | |
| 13 def same_hash(self, *objlist): | |
| 17860 | 14 # Hash each object given and fail if |
| 15 # the hash values are not all the same. | |
| 17859 | 16 hashed = map(hash, objlist) |
| 17 for h in hashed[1:]: | |
| 18 if h != hashed[0]: | |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
19 self.fail("hashed values differ: %r" % (objlist,)) |
| 17859 | 20 |
| 21 def test_numeric_literals(self): | |
| 22 self.same_hash(1, 1L, 1.0, 1.0+0.0j) | |
|
43406
357f54015a2d
Issue #1772851. Alters long.__hash__ from being *almost* completely
Facundo Batista <facundobatista@gmail.com>
parents:
31515
diff
changeset
|
23 self.same_hash(0, 0L, 0.0, 0.0+0.0j) |
|
357f54015a2d
Issue #1772851. Alters long.__hash__ from being *almost* completely
Facundo Batista <facundobatista@gmail.com>
parents:
31515
diff
changeset
|
24 self.same_hash(-1, -1L, -1.0, -1.0+0.0j) |
|
357f54015a2d
Issue #1772851. Alters long.__hash__ from being *almost* completely
Facundo Batista <facundobatista@gmail.com>
parents:
31515
diff
changeset
|
25 self.same_hash(-2, -2L, -2.0, -2.0+0.0j) |
| 17859 | 26 |
| 27 def test_coerced_integers(self): | |
| 28 self.same_hash(int(1), long(1), float(1), complex(1), | |
| 29 int('1'), float('1.0')) | |
|
43406
357f54015a2d
Issue #1772851. Alters long.__hash__ from being *almost* completely
Facundo Batista <facundobatista@gmail.com>
parents:
31515
diff
changeset
|
30 self.same_hash(int(-2**31), long(-2**31), float(-2**31)) |
|
357f54015a2d
Issue #1772851. Alters long.__hash__ from being *almost* completely
Facundo Batista <facundobatista@gmail.com>
parents:
31515
diff
changeset
|
31 self.same_hash(int(1-2**31), long(1-2**31), float(1-2**31)) |
|
357f54015a2d
Issue #1772851. Alters long.__hash__ from being *almost* completely
Facundo Batista <facundobatista@gmail.com>
parents:
31515
diff
changeset
|
32 self.same_hash(int(2**31-1), long(2**31-1), float(2**31-1)) |
|
357f54015a2d
Issue #1772851. Alters long.__hash__ from being *almost* completely
Facundo Batista <facundobatista@gmail.com>
parents:
31515
diff
changeset
|
33 # for 64-bit platforms |
|
357f54015a2d
Issue #1772851. Alters long.__hash__ from being *almost* completely
Facundo Batista <facundobatista@gmail.com>
parents:
31515
diff
changeset
|
34 self.same_hash(int(2**31), long(2**31), float(2**31)) |
|
357f54015a2d
Issue #1772851. Alters long.__hash__ from being *almost* completely
Facundo Batista <facundobatista@gmail.com>
parents:
31515
diff
changeset
|
35 self.same_hash(int(-2**63), long(-2**63), float(-2**63)) |
|
357f54015a2d
Issue #1772851. Alters long.__hash__ from being *almost* completely
Facundo Batista <facundobatista@gmail.com>
parents:
31515
diff
changeset
|
36 self.same_hash(int(1-2**63), long(1-2**63)) |
|
357f54015a2d
Issue #1772851. Alters long.__hash__ from being *almost* completely
Facundo Batista <facundobatista@gmail.com>
parents:
31515
diff
changeset
|
37 self.same_hash(int(2**63-1), long(2**63-1)) |
|
51208
5f6d225e3c1d
Add an extra test for long <-> float hash equivalence.
Mark Dickinson <dickinsm@gmail.com>
parents:
50370
diff
changeset
|
38 self.same_hash(long(2**63), float(2**63)) |
| 17859 | 39 |
| 40 def test_coerced_floats(self): | |
| 41 self.same_hash(long(1.23e300), float(1.23e300)) | |
| 42 self.same_hash(float(0.5), complex(0.5, 0.0)) | |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
43 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
44 |
|
47961
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
45 _default_hash = object.__hash__ |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
46 class DefaultHash(object): pass |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
47 |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
48 _FIXED_HASH_VALUE = 42 |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
49 class FixedHash(object): |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
50 def __hash__(self): |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
51 return _FIXED_HASH_VALUE |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
52 |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
53 class OnlyEquality(object): |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
54 def __eq__(self, other): |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
55 return self is other |
|
48213
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47961
diff
changeset
|
56 # Trick to suppress Py3k warning in 2.x |
|
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47961
diff
changeset
|
57 __hash__ = None |
|
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47961
diff
changeset
|
58 del OnlyEquality.__hash__ |
|
47961
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
59 |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
60 class OnlyInequality(object): |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
61 def __ne__(self, other): |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
62 return self is not other |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
63 |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
64 class OnlyCmp(object): |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
65 def __cmp__(self, other): |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
66 return cmp(id(self), id(other)) |
|
48213
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47961
diff
changeset
|
67 # Trick to suppress Py3k warning in 2.x |
|
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47961
diff
changeset
|
68 __hash__ = None |
|
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47961
diff
changeset
|
69 del OnlyCmp.__hash__ |
|
47961
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
70 |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
71 class InheritedHashWithEquality(FixedHash, OnlyEquality): pass |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
72 class InheritedHashWithInequality(FixedHash, OnlyInequality): pass |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
73 class InheritedHashWithCmp(FixedHash, OnlyCmp): pass |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
74 |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
75 class NoHash(object): |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
76 __hash__ = None |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
77 |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
78 class HashInheritanceTestCase(unittest.TestCase): |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
79 default_expected = [object(), |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
80 DefaultHash(), |
|
48213
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47961
diff
changeset
|
81 OnlyEquality(), |
|
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47961
diff
changeset
|
82 OnlyInequality(), |
|
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47961
diff
changeset
|
83 OnlyCmp(), |
|
47961
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
84 ] |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
85 fixed_expected = [FixedHash(), |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
86 InheritedHashWithEquality(), |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
87 InheritedHashWithInequality(), |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
88 InheritedHashWithCmp(), |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
89 ] |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
90 error_expected = [NoHash()] |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
91 |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
92 def test_default_hash(self): |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
93 for obj in self.default_expected: |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
94 self.assertEqual(hash(obj), _default_hash(obj)) |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
95 |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
96 def test_fixed_hash(self): |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
97 for obj in self.fixed_expected: |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
98 self.assertEqual(hash(obj), _FIXED_HASH_VALUE) |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
99 |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
100 def test_error_hash(self): |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
101 for obj in self.error_expected: |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
102 self.assertRaises(TypeError, hash, obj) |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
103 |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
104 def test_hashable(self): |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
105 objects = (self.default_expected + |
|
48213
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47961
diff
changeset
|
106 self.fixed_expected) |
|
47961
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
107 for obj in objects: |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
108 self.assert_(isinstance(obj, Hashable), repr(obj)) |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
109 |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
110 def test_not_hashable(self): |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
111 for obj in self.error_expected: |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
112 self.assertFalse(isinstance(obj, Hashable), repr(obj)) |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
113 |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
114 |
|
50370
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
115 # Issue #4701: Check that some builtin types are correctly hashable |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
116 # (This test only used to fail in Python 3.0, but has been included |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
117 # in 2.x along with the lazy call to PyType_Ready in PyObject_Hash) |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
118 class DefaultIterSeq(object): |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
119 seq = range(10) |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
120 def __len__(self): |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
121 return len(self.seq) |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
122 def __getitem__(self, index): |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
123 return self.seq[index] |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
124 |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
125 class HashBuiltinsTestCase(unittest.TestCase): |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
126 hashes_to_check = [xrange(10), |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
127 enumerate(xrange(10)), |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
128 iter(DefaultIterSeq()), |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
129 iter(lambda: 0, 0), |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
130 ] |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
131 |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
132 def test_hashes(self): |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
133 _default_hash = object.__hash__ |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
134 for obj in self.hashes_to_check: |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
135 self.assertEqual(hash(obj), _default_hash(obj)) |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
136 |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17860
diff
changeset
|
137 def test_main(): |
|
47961
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
43406
diff
changeset
|
138 test_support.run_unittest(HashEqualityTestCase, |
|
50370
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
139 HashInheritanceTestCase, |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
48213
diff
changeset
|
140 HashBuiltinsTestCase) |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17860
diff
changeset
|
141 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17860
diff
changeset
|
142 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17860
diff
changeset
|
143 if __name__ == "__main__": |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17860
diff
changeset
|
144 test_main() |
