mirror of
https://github.com/python/cpython.git
synced 2025-11-01 06:01:29 +00:00
Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
This commit is contained in:
parent
f7ccc101d2
commit
d6fc72a5ae
4 changed files with 73 additions and 7 deletions
|
|
@ -26,6 +26,14 @@ class ReprWrapper:
|
|||
def __repr__(self):
|
||||
return repr(self.value)
|
||||
|
||||
class HashCountingInt(int):
|
||||
'int-like object that counts the number of times __hash__ is called'
|
||||
def __init__(self, *args):
|
||||
self.hash_count = 0
|
||||
def __hash__(self):
|
||||
self.hash_count += 1
|
||||
return int.__hash__(self)
|
||||
|
||||
class TestJointOps(unittest.TestCase):
|
||||
# Tests common to both set and frozenset
|
||||
|
||||
|
|
@ -270,6 +278,18 @@ def test_cyclical_print(self):
|
|||
fo.close()
|
||||
os.remove(test_support.TESTFN)
|
||||
|
||||
def test_do_not_rehash_dict_keys(self):
|
||||
n = 10
|
||||
d = dict.fromkeys(map(HashCountingInt, xrange(n)))
|
||||
self.assertEqual(sum(elem.hash_count for elem in d), n)
|
||||
s = self.thetype(d)
|
||||
self.assertEqual(sum(elem.hash_count for elem in d), n)
|
||||
s.difference(d)
|
||||
self.assertEqual(sum(elem.hash_count for elem in d), n)
|
||||
if hasattr(s, 'symmetric_difference_update'):
|
||||
s.symmetric_difference_update(d)
|
||||
self.assertEqual(sum(elem.hash_count for elem in d), n)
|
||||
|
||||
class TestSet(TestJointOps):
|
||||
thetype = set
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue