diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-07-22 00:23:21 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-22 00:23:21 (GMT) |
commit | 4194f1465fa85371dcbead57a54bb06d1f0b97d9 (patch) | |
tree | 2b5d1054f052db9fa8da96c97c1697ce45d562b9 /Lib | |
parent | d17449f31d4af7a735e81b587bb329481764412f (diff) | |
download | cpython-4194f1465fa85371dcbead57a54bb06d1f0b97d9.zip cpython-4194f1465fa85371dcbead57a54bb06d1f0b97d9.tar.gz cpython-4194f1465fa85371dcbead57a54bb06d1f0b97d9.tar.bz2 |
bpo-44704: Make Set._hash consistent with frozenset.__hash__ (GH-27281) (GH-27282)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/_collections_abc.py | 1 | ||||
-rw-r--r-- | Lib/test/test_collections.py | 12 |
2 files changed, 13 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) |