bpo-44704: Make Set._hash consistent with frozenset.__hash__ (GH-27281)

This commit is contained in:
Dennis Sweeney 2021-07-21 19:49:03 -04:00 committed by GitHub
parent ab7fcc8fbd
commit c878f5d817
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 0 deletions

View File

@ -696,6 +696,7 @@ class Set(Collection):
hx = hash(x)
h ^= (hx ^ (hx << 16) ^ 89869747) * 3644798167
h &= MASK
h ^= (h >> 11) ^ (h >> 25)
h = h * 69069 + 907133923
h &= MASK
if h > MAX:

View File

@ -1801,6 +1801,18 @@ class TestCollectionABCs(ABCTestCase):
self.assertTrue(f1 != l1)
self.assertTrue(f1 != l2)
def test_Set_hash_matches_frozenset(self):
sets = [
{}, {1}, {None}, {-1}, {0.0}, {"abc"}, {1, 2, 3},
{10**100, 10**101}, {"a", "b", "ab", ""}, {False, True},
{object(), object(), object()}, {float("nan")}, {frozenset()},
{*range(1000)}, {*range(1000)} - {100, 200, 300},
{*range(sys.maxsize - 10, sys.maxsize + 10)},
]
for s in sets:
fs = frozenset(s)
self.assertEqual(hash(fs), Set._hash(fs), msg=s)
def test_Mapping(self):
for sample in [dict]:
self.assertIsInstance(sample(), Mapping)

View File

@ -0,0 +1 @@
The implementation of ``collections.abc.Set._hash()`` now matches that of ``frozenset.__hash__()``.