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

Method check_len_race

Lib/test/test_weakref.py:1351–1373  ·  view source on GitHub ↗
(self, dict_type, cons)

Source from the content-addressed store, hash-verified

1349 self.check_len_cycles(weakref.WeakValueDictionary, lambda k: (1, k))
1350
1351 def check_len_race(self, dict_type, cons):
1352 # Extended sanity checks for len() in the face of cyclic collection
1353 self.addCleanup(gc.set_threshold, *gc.get_threshold())
1354 for th in range(1, 100):
1355 N = 20
1356 gc.collect(0)
1357 gc.set_threshold(th, th, th)
1358 items = [RefCycle() for i in range(N)]
1359 dct = dict_type(cons(o) for o in items)
1360 del items
1361 # All items will be collected at next garbage collection pass
1362 it = dct.items()
1363 try:
1364 next(it)
1365 except StopIteration:
1366 pass
1367 n1 = len(dct)
1368 del it
1369 n2 = len(dct)
1370 self.assertGreaterEqual(n1, 0)
1371 self.assertLessEqual(n1, N)
1372 self.assertGreaterEqual(n2, 0)
1373 self.assertLessEqual(n2, n1)
1374
1375 def test_weak_keyed_len_race(self):
1376 self.check_len_race(weakref.WeakKeyDictionary, lambda k: (k, 1))

Callers 2

Calls 6

addCleanupMethod · 0.80
assertGreaterEqualMethod · 0.80
assertLessEqualMethod · 0.80
RefCycleClass · 0.70
collectMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected