Skip to content

Commit 7f3f57a

Browse files
authored
Merge pull request #550 from python/main
bpo-44704: Make Set._hash consistent with frozenset.__hash__ (pythonGH-27281)
2 parents 20bc4f6 + c878f5d commit 7f3f57a

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Lib/_collections_abc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ def _hash(self):
696696
hx = hash(x)
697697
h ^= (hx ^ (hx << 16) ^ 89869747) * 3644798167
698698
h &= MASK
699+
h ^= (h >> 11) ^ (h >> 25)
699700
h = h * 69069 + 907133923
700701
h &= MASK
701702
if h > MAX:

Lib/test/test_collections.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,18 @@ def __repr__(self):
18011801
self.assertTrue(f1 != l1)
18021802
self.assertTrue(f1 != l2)
18031803

1804+
def test_Set_hash_matches_frozenset(self):
1805+
sets = [
1806+
{}, {1}, {None}, {-1}, {0.0}, {"abc"}, {1, 2, 3},
1807+
{10**100, 10**101}, {"a", "b", "ab", ""}, {False, True},
1808+
{object(), object(), object()}, {float("nan")}, {frozenset()},
1809+
{*range(1000)}, {*range(1000)} - {100, 200, 300},
1810+
{*range(sys.maxsize - 10, sys.maxsize + 10)},
1811+
]
1812+
for s in sets:
1813+
fs = frozenset(s)
1814+
self.assertEqual(hash(fs), Set._hash(fs), msg=s)
1815+
18041816
def test_Mapping(self):
18051817
for sample in [dict]:
18061818
self.assertIsInstance(sample(), Mapping)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The implementation of ``collections.abc.Set._hash()`` now matches that of ``frozenset.__hash__()``.

0 commit comments

Comments
 (0)