@@ -174,7 +174,7 @@ def test_simple(self):
174174 gc .collect ()
175175 self .assert_del_calls (ids )
176176 self .assert_survivors ([])
177- self .assertIs (wr (), None )
177+ self .assertIsNone (wr ())
178178 gc .collect ()
179179 self .assert_del_calls (ids )
180180 self .assert_survivors ([])
@@ -188,12 +188,12 @@ def test_simple_resurrect(self):
188188 gc .collect ()
189189 self .assert_del_calls (ids )
190190 self .assert_survivors (ids )
191- self .assertIsNot (wr (), None )
191+ self .assertIsNotNone (wr ())
192192 self .clear_survivors ()
193193 gc .collect ()
194194 self .assert_del_calls (ids )
195195 self .assert_survivors ([])
196- self .assertIs (wr (), None )
196+ self .assertIsNone (wr ())
197197
198198 @support .cpython_only
199199 def test_non_gc (self ):
@@ -265,7 +265,7 @@ def test_simple(self):
265265 gc .collect ()
266266 self .assert_del_calls (ids )
267267 self .assert_survivors ([])
268- self .assertIs (wr (), None )
268+ self .assertIsNone (wr ())
269269 gc .collect ()
270270 self .assert_del_calls (ids )
271271 self .assert_survivors ([])
@@ -276,19 +276,24 @@ def test_simple_resurrect(self):
276276 s = SelfCycleResurrector ()
277277 ids = [id (s )]
278278 wr = weakref .ref (s )
279+ wrc = weakref .ref (s , lambda x : None )
279280 del s
280281 gc .collect ()
281282 self .assert_del_calls (ids )
282283 self .assert_survivors (ids )
283- # XXX is this desirable?
284- self .assertIs (wr (), None )
284+ # This used to be None because weakrefs were cleared before
285+ # calling finalizers. Now they are cleared after.
286+ self .assertIsNotNone (wr ())
287+ # A weakref with a callback is still cleared before calling
288+ # finalizers.
289+ self .assertIsNone (wrc ())
285290 # When trying to destroy the object a second time, __del__
286291 # isn't called anymore (and the object isn't resurrected).
287292 self .clear_survivors ()
288293 gc .collect ()
289294 self .assert_del_calls (ids )
290295 self .assert_survivors ([])
291- self .assertIs (wr (), None )
296+ self .assertIsNone (wr ())
292297
293298 def test_simple_suicide (self ):
294299 # Test the GC is able to deal with an object that kills its last
@@ -301,11 +306,11 @@ def test_simple_suicide(self):
301306 gc .collect ()
302307 self .assert_del_calls (ids )
303308 self .assert_survivors ([])
304- self .assertIs (wr (), None )
309+ self .assertIsNone (wr ())
305310 gc .collect ()
306311 self .assert_del_calls (ids )
307312 self .assert_survivors ([])
308- self .assertIs (wr (), None )
313+ self .assertIsNone (wr ())
309314
310315
311316class ChainedBase :
@@ -378,18 +383,27 @@ def check_non_resurrecting_chain(self, classes):
378383
379384 def check_resurrecting_chain (self , classes ):
380385 N = len (classes )
386+ def dummy_callback (ref ):
387+ pass
381388 with SimpleBase .test ():
382389 nodes = self .build_chain (classes )
383390 N = len (nodes )
384391 ids = [id (s ) for s in nodes ]
385392 survivor_ids = [id (s ) for s in nodes if isinstance (s , SimpleResurrector )]
386393 wrs = [weakref .ref (s ) for s in nodes ]
394+ wrcs = [weakref .ref (s , dummy_callback ) for s in nodes ]
387395 del nodes
388396 gc .collect ()
389397 self .assert_del_calls (ids )
390398 self .assert_survivors (survivor_ids )
391- # XXX desirable?
392- self .assertEqual ([wr () for wr in wrs ], [None ] * N )
399+ for wr in wrs :
400+ # These values used to be None because weakrefs were cleared
401+ # before calling finalizers. Now they are cleared after.
402+ self .assertIsNotNone (wr ())
403+ for wr in wrcs :
404+ # Weakrefs with callbacks are still cleared before calling
405+ # finalizers.
406+ self .assertIsNone (wr ())
393407 self .clear_survivors ()
394408 gc .collect ()
395409 self .assert_del_calls (ids )
@@ -491,7 +505,7 @@ def test_legacy(self):
491505 self .assert_del_calls (ids )
492506 self .assert_tp_del_calls (ids )
493507 self .assert_survivors ([])
494- self .assertIs (wr (), None )
508+ self .assertIsNone (wr ())
495509 gc .collect ()
496510 self .assert_del_calls (ids )
497511 self .assert_tp_del_calls (ids )
@@ -507,13 +521,13 @@ def test_legacy_resurrect(self):
507521 self .assert_tp_del_calls (ids )
508522 self .assert_survivors (ids )
509523 # weakrefs are cleared before tp_del is called.
510- self .assertIs (wr (), None )
524+ self .assertIsNone (wr ())
511525 self .clear_survivors ()
512526 gc .collect ()
513527 self .assert_del_calls (ids )
514528 self .assert_tp_del_calls (ids * 2 )
515529 self .assert_survivors (ids )
516- self .assertIs (wr (), None )
530+ self .assertIsNone (wr ())
517531
518532 def test_legacy_self_cycle (self ):
519533 # Self-cycles with legacy finalizers end up in gc.garbage.
@@ -527,11 +541,11 @@ def test_legacy_self_cycle(self):
527541 self .assert_tp_del_calls ([])
528542 self .assert_survivors ([])
529543 self .assert_garbage (ids )
530- self .assertIsNot (wr (), None )
544+ self .assertIsNotNone (wr ())
531545 # Break the cycle to allow collection
532546 gc .garbage [0 ].ref = None
533547 self .assert_garbage ([])
534- self .assertIs (wr (), None )
548+ self .assertIsNone (wr ())
535549
536550
537551if __name__ == "__main__" :
0 commit comments