annotate Lib/test/test_hash.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 7549dd74e08c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
75100
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
6 import os
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
7 import sys
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
8 import struct
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
9 import datetime
17859
b77c4888d7cc Simple conversion to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
10 import unittest
75100
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
11 import subprocess
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
12
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
13 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
14 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
15
75100
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
16 IS_64BIT = (struct.calcsize('l') == 8)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
17
13141
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
diff changeset
18
17859
b77c4888d7cc Simple conversion to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
19 class HashEqualityTestCase(unittest.TestCase):
b77c4888d7cc Simple conversion to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
20
b77c4888d7cc Simple conversion to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
21 def same_hash(self, *objlist):
17860
b53b60f1ed45 Update a comment.
Fred Drake <fdrake@acm.org>
parents: 17859
diff changeset
22 # Hash each object given and fail if
b53b60f1ed45 Update a comment.
Fred Drake <fdrake@acm.org>
parents: 17859
diff changeset
23 # the hash values are not all the same.
17859
b77c4888d7cc Simple conversion to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
24 hashed = map(hash, objlist)
b77c4888d7cc Simple conversion to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
25 for h in hashed[1:]:
b77c4888d7cc Simple conversion to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
26 if h != hashed[0]:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
27 self.fail("hashed values differ: %r" % (objlist,))
17859
b77c4888d7cc Simple conversion to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
28
b77c4888d7cc Simple conversion to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
29 def test_numeric_literals(self):
b77c4888d7cc Simple conversion to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
30 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
31 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
32 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
33 self.same_hash(-2, -2L, -2.0, -2.0+0.0j)
17859
b77c4888d7cc Simple conversion to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
34
b77c4888d7cc Simple conversion to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
35 def test_coerced_integers(self):
b77c4888d7cc Simple conversion to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
36 self.same_hash(int(1), long(1), float(1), complex(1),
b77c4888d7cc Simple conversion to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
37 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
38 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
39 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
40 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
41 # for 64-bit platforms
357f54015a2d Issue #1772851. Alters long.__hash__ from being *almost* completely
Facundo Batista <facundobatista@gmail.com>
parents: 31515
diff changeset
42 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
43 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
44 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
45 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
46 self.same_hash(long(2**63), float(2**63))
17859
b77c4888d7cc Simple conversion to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
47
b77c4888d7cc Simple conversion to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
48 def test_coerced_floats(self):
b77c4888d7cc Simple conversion to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
49 self.same_hash(long(1.23e300), float(1.23e300))
b77c4888d7cc Simple conversion to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
50 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
51
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
diff changeset
52
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
53 _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
54 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
55
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
56 _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
57 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
58 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
59 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
60
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 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
62 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
63 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
64 # 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
65 __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
66 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
67
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
68 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
69 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
70 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
71
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 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
73 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
74 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
75 # 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
76 __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
77 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
78
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 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
80 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
81 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
82
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
83 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
84 __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
85
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 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
87 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
88 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
89 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
90 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
91 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
92 ]
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 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
94 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
95 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
96 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
97 ]
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 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
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_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
101 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
102 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
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_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
105 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
106 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
107
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 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
109 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
110 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
111
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 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
113 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
114 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
115 for obj in objects:
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
116 self.assertIsInstance(obj, Hashable)
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
117
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
118 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
119 for obj in self.error_expected:
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
120 self.assertNotIsInstance(obj, Hashable)
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
121
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
122
50370
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
123 # 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
124 # (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
125 # 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
126 class DefaultIterSeq(object):
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
127 seq = range(10)
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
128 def __len__(self):
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
129 return len(self.seq)
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
130 def __getitem__(self, index):
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
131 return self.seq[index]
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
132
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
133 class HashBuiltinsTestCase(unittest.TestCase):
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
134 hashes_to_check = [xrange(10),
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
135 enumerate(xrange(10)),
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
136 iter(DefaultIterSeq()),
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
137 iter(lambda: 0, 0),
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
138 ]
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
139
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
140 def test_hashes(self):
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
141 _default_hash = object.__hash__
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
142 for obj in self.hashes_to_check:
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
143 self.assertEqual(hash(obj), _default_hash(obj))
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
144
75100
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
145 class HashRandomizationTests(unittest.TestCase):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
146
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
147 # Each subclass should define a field "repr_", containing the repr() of
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
148 # an object to be tested
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
149
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
150 def get_hash_command(self, repr_):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
151 return 'print(hash(%s))' % repr_
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
152
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
153 def get_hash(self, repr_, seed=None):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
154 env = os.environ.copy()
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
155 if seed is not None:
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
156 env['PYTHONHASHSEED'] = str(seed)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
157 else:
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
158 env.pop('PYTHONHASHSEED', None)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
159 cmd_line = [sys.executable, '-c', self.get_hash_command(repr_)]
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
160 p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
161 stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
162 env=env)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
163 out, err = p.communicate()
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
164 out = test_support.strip_python_stderr(out)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
165 return int(out.strip())
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
166
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
167 def test_randomized_hash(self):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
168 # two runs should return different hashes
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
169 run1 = self.get_hash(self.repr_, seed='random')
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
170 run2 = self.get_hash(self.repr_, seed='random')
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
171 self.assertNotEqual(run1, run2)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
172
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
173 class StringlikeHashRandomizationTests(HashRandomizationTests):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
174 def test_null_hash(self):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
175 # PYTHONHASHSEED=0 disables the randomized hash
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
176 if IS_64BIT:
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
177 known_hash_of_obj = 1453079729188098211
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
178 else:
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
179 known_hash_of_obj = -1600925533
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
180
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
181 # Randomization is disabled by default:
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
182 self.assertEqual(self.get_hash(self.repr_), known_hash_of_obj)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
183
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
184 # It can also be disabled by setting the seed to 0:
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
185 self.assertEqual(self.get_hash(self.repr_, seed=0), known_hash_of_obj)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
186
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
187 def test_fixed_hash(self):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
188 # test a fixed seed for the randomized hash
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
189 # Note that all types share the same values:
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
190 if IS_64BIT:
75181
c7baaf0cde8d Backport from 2.7 branch.
Barry Warsaw <barry@python.org>
parents: 75101
diff changeset
191 if sys.byteorder == 'little':
c7baaf0cde8d Backport from 2.7 branch.
Barry Warsaw <barry@python.org>
parents: 75101
diff changeset
192 h = -4410911502303878509
c7baaf0cde8d Backport from 2.7 branch.
Barry Warsaw <barry@python.org>
parents: 75101
diff changeset
193 else:
c7baaf0cde8d Backport from 2.7 branch.
Barry Warsaw <barry@python.org>
parents: 75101
diff changeset
194 h = -3570150969479994130
75100
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
195 else:
75181
c7baaf0cde8d Backport from 2.7 branch.
Barry Warsaw <barry@python.org>
parents: 75101
diff changeset
196 if sys.byteorder == 'little':
c7baaf0cde8d Backport from 2.7 branch.
Barry Warsaw <barry@python.org>
parents: 75101
diff changeset
197 h = -206076799
c7baaf0cde8d Backport from 2.7 branch.
Barry Warsaw <barry@python.org>
parents: 75101
diff changeset
198 else:
c7baaf0cde8d Backport from 2.7 branch.
Barry Warsaw <barry@python.org>
parents: 75101
diff changeset
199 h = -1024014457
75100
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
200 self.assertEqual(self.get_hash(self.repr_, seed=42), h)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
201
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
202 class StrHashRandomizationTests(StringlikeHashRandomizationTests):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
203 repr_ = repr('abc')
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
204
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
205 def test_empty_string(self):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
206 self.assertEqual(hash(""), 0)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
207
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
208 class UnicodeHashRandomizationTests(StringlikeHashRandomizationTests):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
209 repr_ = repr(u'abc')
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
210
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
211 def test_empty_string(self):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
212 self.assertEqual(hash(u""), 0)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
213
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
214 class BufferHashRandomizationTests(StringlikeHashRandomizationTests):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
215 repr_ = 'buffer("abc")'
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
216
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
217 def test_empty_string(self):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
218 self.assertEqual(hash(buffer("")), 0)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
219
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
220 class DatetimeTests(HashRandomizationTests):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
221 def get_hash_command(self, repr_):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
222 return 'import datetime; print(hash(%s))' % repr_
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
223
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
224 class DatetimeDateTests(DatetimeTests):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
225 repr_ = repr(datetime.date(1066, 10, 14))
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
226
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
227 class DatetimeDatetimeTests(DatetimeTests):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
228 repr_ = repr(datetime.datetime(1, 2, 3, 4, 5, 6, 7))
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
229
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
230 class DatetimeTimeTests(DatetimeTests):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
231 repr_ = repr(datetime.time(0))
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
232
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
233
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17860
diff changeset
234 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
235 test_support.run_unittest(HashEqualityTestCase,
50370
e782f97116dc Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 48213
diff changeset
236 HashInheritanceTestCase,
75100
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
237 HashBuiltinsTestCase,
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
238 StrHashRandomizationTests,
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
239 UnicodeHashRandomizationTests,
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
240 BufferHashRandomizationTests,
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
241 DatetimeDateTests,
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
242 DatetimeDatetimeTests,
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 50370
diff changeset
243 DatetimeTimeTests)
75101
19e6e55f09f3 Whitespace normalization
Barry Warsaw <barry@python.org>
parents: 75100
diff changeset
244
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17860
diff changeset
245
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17860
diff changeset
246
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17860
diff changeset
247 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
248 test_main()