MCPcopy Index your code
hub / github.com/python/cpython / test_setdefault_atomic

Method test_setdefault_atomic

Lib/test/test_dict.py:501–519  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

499 self.assertRaises(Exc, d.setdefault, x, [])
500
501 def test_setdefault_atomic(self):
502 # Issue #13521: setdefault() calls __hash__ and __eq__ only once.
503 class Hashed(object):
504 def __init__(self):
505 self.hash_count = 0
506 self.eq_count = 0
507 def __hash__(self):
508 self.hash_count += 1
509 return 42
510 def __eq__(self, other):
511 self.eq_count += 1
512 return id(self) == id(other)
513 hashed1 = Hashed()
514 y = {hashed1: 5}
515 hashed2 = Hashed()
516 y.setdefault(hashed2, [])
517 self.assertEqual(hashed1.hash_count, 1)
518 self.assertEqual(hashed2.hash_count, 1)
519 self.assertEqual(hashed1.eq_count + hashed2.eq_count, 1)
520
521 def test_setitem_atomic_at_resize(self):
522 class Hashed(object):

Callers

nothing calls this directly

Calls 3

HashedClass · 0.85
setdefaultMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected