@@ -174,7 +174,7 @@ def test_simple(self):
174
174
gc .collect ()
175
175
self .assert_del_calls (ids )
176
176
self .assert_survivors ([])
177
- self .assertIs (wr (), None )
177
+ self .assertIsNone (wr ())
178
178
gc .collect ()
179
179
self .assert_del_calls (ids )
180
180
self .assert_survivors ([])
@@ -188,12 +188,12 @@ def test_simple_resurrect(self):
188
188
gc .collect ()
189
189
self .assert_del_calls (ids )
190
190
self .assert_survivors (ids )
191
- self .assertIsNot (wr (), None )
191
+ self .assertIsNotNone (wr ())
192
192
self .clear_survivors ()
193
193
gc .collect ()
194
194
self .assert_del_calls (ids )
195
195
self .assert_survivors ([])
196
- self .assertIs (wr (), None )
196
+ self .assertIsNone (wr ())
197
197
198
198
@support .cpython_only
199
199
def test_non_gc (self ):
@@ -265,7 +265,7 @@ def test_simple(self):
265
265
gc .collect ()
266
266
self .assert_del_calls (ids )
267
267
self .assert_survivors ([])
268
- self .assertIs (wr (), None )
268
+ self .assertIsNone (wr ())
269
269
gc .collect ()
270
270
self .assert_del_calls (ids )
271
271
self .assert_survivors ([])
@@ -276,19 +276,24 @@ def test_simple_resurrect(self):
276
276
s = SelfCycleResurrector ()
277
277
ids = [id (s )]
278
278
wr = weakref .ref (s )
279
+ wrc = weakref .ref (s , lambda x : None )
279
280
del s
280
281
gc .collect ()
281
282
self .assert_del_calls (ids )
282
283
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 ())
285
290
# When trying to destroy the object a second time, __del__
286
291
# isn't called anymore (and the object isn't resurrected).
287
292
self .clear_survivors ()
288
293
gc .collect ()
289
294
self .assert_del_calls (ids )
290
295
self .assert_survivors ([])
291
- self .assertIs (wr (), None )
296
+ self .assertIsNone (wr ())
292
297
293
298
def test_simple_suicide (self ):
294
299
# Test the GC is able to deal with an object that kills its last
@@ -301,11 +306,11 @@ def test_simple_suicide(self):
301
306
gc .collect ()
302
307
self .assert_del_calls (ids )
303
308
self .assert_survivors ([])
304
- self .assertIs (wr (), None )
309
+ self .assertIsNone (wr ())
305
310
gc .collect ()
306
311
self .assert_del_calls (ids )
307
312
self .assert_survivors ([])
308
- self .assertIs (wr (), None )
313
+ self .assertIsNone (wr ())
309
314
310
315
311
316
class ChainedBase :
@@ -378,18 +383,27 @@ def check_non_resurrecting_chain(self, classes):
378
383
379
384
def check_resurrecting_chain (self , classes ):
380
385
N = len (classes )
386
+ def dummy_callback (ref ):
387
+ pass
381
388
with SimpleBase .test ():
382
389
nodes = self .build_chain (classes )
383
390
N = len (nodes )
384
391
ids = [id (s ) for s in nodes ]
385
392
survivor_ids = [id (s ) for s in nodes if isinstance (s , SimpleResurrector )]
386
393
wrs = [weakref .ref (s ) for s in nodes ]
394
+ wrcs = [weakref .ref (s , dummy_callback ) for s in nodes ]
387
395
del nodes
388
396
gc .collect ()
389
397
self .assert_del_calls (ids )
390
398
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 ())
393
407
self .clear_survivors ()
394
408
gc .collect ()
395
409
self .assert_del_calls (ids )
@@ -491,7 +505,7 @@ def test_legacy(self):
491
505
self .assert_del_calls (ids )
492
506
self .assert_tp_del_calls (ids )
493
507
self .assert_survivors ([])
494
- self .assertIs (wr (), None )
508
+ self .assertIsNone (wr ())
495
509
gc .collect ()
496
510
self .assert_del_calls (ids )
497
511
self .assert_tp_del_calls (ids )
@@ -507,13 +521,13 @@ def test_legacy_resurrect(self):
507
521
self .assert_tp_del_calls (ids )
508
522
self .assert_survivors (ids )
509
523
# weakrefs are cleared before tp_del is called.
510
- self .assertIs (wr (), None )
524
+ self .assertIsNone (wr ())
511
525
self .clear_survivors ()
512
526
gc .collect ()
513
527
self .assert_del_calls (ids )
514
528
self .assert_tp_del_calls (ids * 2 )
515
529
self .assert_survivors (ids )
516
- self .assertIs (wr (), None )
530
+ self .assertIsNone (wr ())
517
531
518
532
def test_legacy_self_cycle (self ):
519
533
# Self-cycles with legacy finalizers end up in gc.garbage.
@@ -527,11 +541,11 @@ def test_legacy_self_cycle(self):
527
541
self .assert_tp_del_calls ([])
528
542
self .assert_survivors ([])
529
543
self .assert_garbage (ids )
530
- self .assertIsNot (wr (), None )
544
+ self .assertIsNotNone (wr ())
531
545
# Break the cycle to allow collection
532
546
gc .garbage [0 ].ref = None
533
547
self .assert_garbage ([])
534
- self .assertIs (wr (), None )
548
+ self .assertIsNone (wr ())
535
549
536
550
537
551
if __name__ == "__main__" :
0 commit comments