|
3 | 3 | # |
4 | 4 | # Also test that hash implementations are inherited as expected |
5 | 5 |
|
| 6 | +import os |
| 7 | +import sys |
| 8 | +import struct |
| 9 | +import datetime |
6 | 10 | import unittest |
| 11 | +import subprocess |
| 12 | + |
7 | 13 | from test import test_support |
8 | 14 | from collections import Hashable |
9 | 15 |
|
| 16 | +IS_64BIT = (struct.calcsize('l') == 8) |
| 17 | + |
10 | 18 |
|
11 | 19 | class HashEqualityTestCase(unittest.TestCase): |
12 | 20 |
|
@@ -134,10 +142,100 @@ def test_hashes(self): |
134 | 142 | for obj in self.hashes_to_check: |
135 | 143 | self.assertEqual(hash(obj), _default_hash(obj)) |
136 | 144 |
|
| 145 | +class HashRandomizationTests(unittest.TestCase): |
| 146 | + |
| 147 | + # Each subclass should define a field "repr_", containing the repr() of |
| 148 | + # an object to be tested |
| 149 | + |
| 150 | + def get_hash_command(self, repr_): |
| 151 | + return 'print(hash(%s))' % repr_ |
| 152 | + |
| 153 | + def get_hash(self, repr_, seed=None): |
| 154 | + env = os.environ.copy() |
| 155 | + if seed is not None: |
| 156 | + env['PYTHONHASHSEED'] = str(seed) |
| 157 | + else: |
| 158 | + env.pop('PYTHONHASHSEED', None) |
| 159 | + cmd_line = [sys.executable, '-c', self.get_hash_command(repr_)] |
| 160 | + p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE, |
| 161 | + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
| 162 | + env=env) |
| 163 | + out, err = p.communicate() |
| 164 | + out = test_support.strip_python_stderr(out) |
| 165 | + return int(out.strip()) |
| 166 | + |
| 167 | + def test_randomized_hash(self): |
| 168 | + # two runs should return different hashes |
| 169 | + run1 = self.get_hash(self.repr_, seed='random') |
| 170 | + run2 = self.get_hash(self.repr_, seed='random') |
| 171 | + self.assertNotEqual(run1, run2) |
| 172 | + |
| 173 | +class StringlikeHashRandomizationTests(HashRandomizationTests): |
| 174 | + def test_null_hash(self): |
| 175 | + # PYTHONHASHSEED=0 disables the randomized hash |
| 176 | + if IS_64BIT: |
| 177 | + known_hash_of_obj = 1453079729188098211 |
| 178 | + else: |
| 179 | + known_hash_of_obj = -1600925533 |
| 180 | + |
| 181 | + # Randomization is disabled by default: |
| 182 | + self.assertEqual(self.get_hash(self.repr_), known_hash_of_obj) |
| 183 | + |
| 184 | + # It can also be disabled by setting the seed to 0: |
| 185 | + self.assertEqual(self.get_hash(self.repr_, seed=0), known_hash_of_obj) |
| 186 | + |
| 187 | + def test_fixed_hash(self): |
| 188 | + # test a fixed seed for the randomized hash |
| 189 | + # Note that all types share the same values: |
| 190 | + if IS_64BIT: |
| 191 | + h = -4410911502303878509 |
| 192 | + else: |
| 193 | + h = -206076799 |
| 194 | + self.assertEqual(self.get_hash(self.repr_, seed=42), h) |
| 195 | + |
| 196 | +class StrHashRandomizationTests(StringlikeHashRandomizationTests): |
| 197 | + repr_ = repr('abc') |
| 198 | + |
| 199 | + def test_empty_string(self): |
| 200 | + self.assertEqual(hash(""), 0) |
| 201 | + |
| 202 | +class UnicodeHashRandomizationTests(StringlikeHashRandomizationTests): |
| 203 | + repr_ = repr(u'abc') |
| 204 | + |
| 205 | + def test_empty_string(self): |
| 206 | + self.assertEqual(hash(u""), 0) |
| 207 | + |
| 208 | +class BufferHashRandomizationTests(StringlikeHashRandomizationTests): |
| 209 | + repr_ = 'buffer("abc")' |
| 210 | + |
| 211 | + def test_empty_string(self): |
| 212 | + self.assertEqual(hash(buffer("")), 0) |
| 213 | + |
| 214 | +class DatetimeTests(HashRandomizationTests): |
| 215 | + def get_hash_command(self, repr_): |
| 216 | + return 'import datetime; print(hash(%s))' % repr_ |
| 217 | + |
| 218 | +class DatetimeDateTests(DatetimeTests): |
| 219 | + repr_ = repr(datetime.date(1066, 10, 14)) |
| 220 | + |
| 221 | +class DatetimeDatetimeTests(DatetimeTests): |
| 222 | + repr_ = repr(datetime.datetime(1, 2, 3, 4, 5, 6, 7)) |
| 223 | + |
| 224 | +class DatetimeTimeTests(DatetimeTests): |
| 225 | + repr_ = repr(datetime.time(0)) |
| 226 | + |
| 227 | + |
137 | 228 | def test_main(): |
138 | 229 | test_support.run_unittest(HashEqualityTestCase, |
139 | 230 | HashInheritanceTestCase, |
140 | | - HashBuiltinsTestCase) |
| 231 | + HashBuiltinsTestCase, |
| 232 | + StrHashRandomizationTests, |
| 233 | + UnicodeHashRandomizationTests, |
| 234 | + BufferHashRandomizationTests, |
| 235 | + DatetimeDateTests, |
| 236 | + DatetimeDatetimeTests, |
| 237 | + DatetimeTimeTests) |
| 238 | + |
141 | 239 |
|
142 | 240 |
|
143 | 241 | if __name__ == "__main__": |
|
0 commit comments