summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDennis Sweeney <36520290+sweeneyde@users.noreply.github.com>2021-07-21 23:49:03 (GMT)
committerGitHub <noreply@github.com>2021-07-21 23:49:03 (GMT)
commitc878f5d81772dc6f718d6608c78baa4be9a4f176 (patch)
treee0cb481a646370ab2f18eae81d27d81252148edc
parentab7fcc8fbdc11091370deeb000a787fb02f9b13d (diff)
downloadcpython-c878f5d81772dc6f718d6608c78baa4be9a4f176.zip
cpython-c878f5d81772dc6f718d6608c78baa4be9a4f176.tar.gz
cpython-c878f5d81772dc6f718d6608c78baa4be9a4f176.tar.bz2
bpo-44704: Make Set._hash consistent with frozenset.__hash__ (GH-27281)
-rw-r--r--Lib/_collections_abc.py1
-rw-r--r--Lib/test/test_collections.py12
-rw-r--r--Misc/NEWS.d/next/Library/2021-07-21-23-16-30.bpo-44704.iqHLxQ.rst1
3 files changed, 14 insertions, 0 deletions
diff --git a/Lib/_collections_abc.py b/Lib/_collections_abc.py
index d92b848..bff58ad 100644
--- a/Lib/_collections_abc.py
+++ b/Lib/_collections_abc.py
@@ -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:
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index f98048b..1f659d7 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -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)
diff --git a/Misc/NEWS.d/next/Library/2021-07-21-23-16-30.bpo-44704.iqHLxQ.rst b/Misc/NEWS.d/next/Library/2021-07-21-23-16-30.bpo-44704.iqHLxQ.rst
new file mode 100644
index 0000000..5866618
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-07-21-23-16-30.bpo-44704.iqHLxQ.rst
@@ -0,0 +1 @@
+The implementation of ``collections.abc.Set._hash()`` now matches that of ``frozenset.__hash__()``. \ No newline at end of file