From ccef77fd40aae3c1756ec8c77bf1ab9497a914e0 Mon Sep 17 00:00:00 2001 From: sweeneyde Date: Wed, 21 Jul 2021 19:03:09 -0400 Subject: [PATCH 1/3] Fix Set._hash --- Lib/_collections_abc.py | 1 + Lib/test/test_collections.py | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/Lib/_collections_abc.py b/Lib/_collections_abc.py index d92b848e9436fb4..bff58ad4a7f6a72 100644 --- a/Lib/_collections_abc.py +++ b/Lib/_collections_abc.py @@ -696,6 +696,7 @@ def _hash(self): 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 f98048b34a7a1d7..6634de3b2282604 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -1800,6 +1800,18 @@ def __repr__(self): self.assertTrue(f1 != l3) 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]: From 1e3409d6ab10c9af42af78b33e58f00881cfc6d3 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 21 Jul 2021 23:16:32 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2021-07-21-23-16-30.bpo-44704.iqHLxQ.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2021-07-21-23-16-30.bpo-44704.iqHLxQ.rst 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 000000000000000..586661876dedbaf --- /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 From cdcff2978f25a8fa45ddd6c530e272d1aafcf3b4 Mon Sep 17 00:00:00 2001 From: sweeneyde Date: Wed, 21 Jul 2021 19:23:59 -0400 Subject: [PATCH 3/3] Fix trailing whitespace --- Lib/test/test_collections.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 6634de3b2282604..1f659d7d604e31d 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -1800,7 +1800,7 @@ def __repr__(self): self.assertTrue(f1 != l3) 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},