Skip to content

Commit 073409b

Browse files
committed
Add additional tests for weakref clearing.
1 parent bb29ea1 commit 073409b

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

Lib/test/test_finalization.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,17 @@ 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)
283284
# This used to be None because weakrefs were cleared before
284285
# calling finalizers. Now they are cleared after.
285286
self.assertIsNot(wr(), None)
287+
# A weakref with a callback is still cleared before calling
288+
# finalizers.
289+
self.assertIsNone(wrc())
286290
# When trying to destroy the object a second time, __del__
287291
# isn't called anymore (and the object isn't resurrected).
288292
self.clear_survivors()
@@ -379,12 +383,15 @@ def check_non_resurrecting_chain(self, classes):
379383

380384
def check_resurrecting_chain(self, classes):
381385
N = len(classes)
386+
def dummy_callback(ref):
387+
pass
382388
with SimpleBase.test():
383389
nodes = self.build_chain(classes)
384390
N = len(nodes)
385391
ids = [id(s) for s in nodes]
386392
survivor_ids = [id(s) for s in nodes if isinstance(s, SimpleResurrector)]
387393
wrs = [weakref.ref(s) for s in nodes]
394+
wrcs = [weakref.ref(s, dummy_callback) for s in nodes]
388395
del nodes
389396
gc.collect()
390397
self.assert_del_calls(ids)
@@ -393,6 +400,10 @@ def check_resurrecting_chain(self, classes):
393400
# These values used to be None because weakrefs were cleared
394401
# before calling finalizers. Now they are cleared after.
395402
self.assertIsNotNone(wr())
403+
for wr in wrcs:
404+
# Weakrefs with callbacks are still cleared before calling
405+
# finalizers.
406+
self.assertIsNone(wr())
396407
self.clear_survivors()
397408
gc.collect()
398409
self.assert_del_calls(ids)

0 commit comments

Comments
 (0)