Visitar URL original
gh-135552: Add tests that check if weakref for tp_subclasses cleared after finalization by sergey-miryanov · Pull Request #136304 · python/cpython · GitHub
Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Revise test_clearing_weakrefs_in_gc test
Types are not treated specially, it only matters if the weakref has a callback or not.
  • Loading branch information
nascheme authored Aug 7, 2025
commit 0339d544908326dfb5a9c0c3bb138f40b654c65f
16 changes: 5 additions & 11 deletions Lib/test/test_weakref.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,30 +1045,24 @@ def callback(obj):
self.assertNotRegex(stderr, "_Py_Dealloc: Deallocator of type 'TestObj'")

def test_clearing_weakrefs_in_gc(self):
# This test checks that:
# 1. weakrefs for types with callbacks are cleared before the
# finalizer is called
# 2. weakrefs for types without callbacks are cleared after the
# finalizer is called
# 3. other weakrefs cleared before the finalizer is called
# This test checks that when finalizers are called:
# 1. weakrefs with callbacks have been cleared
# 2. weakrefs without callbacks have not been cleared
errors = []
def test():
class Class:
def __init__(self):
self._self = self
self.wr1 = weakref.ref(Class, lambda x: None)
self.wr2 = weakref.ref(Class)
self.wr3 = weakref.ref(self)

def __del__(self):
# we can't use assert* here, because gc will swallow
# exceptions
if self.wr1() is not None:
errors.append("Type weakref with callback is not None")
errors.append("weakref with callback as cleared")
if self.wr2() is not Class:
errors.append("Type weakref is not Class")
if self.wr3() is not None:
errors.append("Instance weakref is not None")
errors.append("weakref without callback was cleared")

Class()

Expand Down