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

Method test_order

Lib/test/test_weakref.py:2183–2220  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2181 self.assertRaises(TypeError, weakref.finalize)
2182
2183 def test_order(self):
2184 a = self.A()
2185 res = []
2186
2187 f1 = weakref.finalize(a, res.append, 'f1')
2188 f2 = weakref.finalize(a, res.append, 'f2')
2189 f3 = weakref.finalize(a, res.append, 'f3')
2190 f4 = weakref.finalize(a, res.append, 'f4')
2191 f5 = weakref.finalize(a, res.append, 'f5')
2192
2193 # make sure finalizers can keep themselves alive
2194 del f1, f4
2195
2196 self.assertTrue(f2.alive)
2197 self.assertTrue(f3.alive)
2198 self.assertTrue(f5.alive)
2199
2200 self.assertTrue(f5.detach())
2201 self.assertFalse(f5.alive)
2202
2203 f5() # nothing because previously unregistered
2204 res.append('A')
2205 f3() # => res.append('f3')
2206 self.assertFalse(f3.alive)
2207 res.append('B')
2208 f3() # nothing because previously called
2209 res.append('C')
2210 del a
2211 self._collect_if_necessary()
2212 # => res.append('f4')
2213 # => res.append('f2')
2214 # => res.append('f1')
2215 self.assertFalse(f2.alive)
2216 res.append('D')
2217 f2() # nothing because previously called by gc
2218
2219 expected = ['A', 'f3', 'B', 'C', 'f4', 'f2', 'f1', 'D']
2220 self.assertEqual(res, expected)
2221
2222 def test_all_freed(self):
2223 # we want a weakrefable subclass of weakref.finalize

Callers

nothing calls this directly

Calls 9

_collect_if_necessaryMethod · 0.95
f2Function · 0.85
AMethod · 0.80
assertTrueMethod · 0.80
assertFalseMethod · 0.80
finalizeMethod · 0.45
detachMethod · 0.45
appendMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected