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
Show file tree
Hide file tree
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
Next Next commit
Move test_clearing_weakrefs_in_gc to test_weakref
  • Loading branch information
sergey-miryanov committed Jul 8, 2025
commit aa18e7431ec27e9049f2c7badc024469979d8591
34 changes: 0 additions & 34 deletions Lib/test/test_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1156,40 +1156,6 @@ def test():
# this test checks regular garbage collection
assert_python_ok("-c", code_inside_function)

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
code = """
import weakref
def test():
class Class:
def __init__(self):
self._self = self
self._1 = weakref.ref(Class, lambda x: None)
self._2 = weakref.ref(Class)
self._3 = weakref.ref(self)

def __del__(self):
if self._1() is None:
print("Type weakref with callback is None as expected")
if self._2() is Class:
print("Type weakref is Class as expected")
if self._3() is None:
print("Instance weakref is None as expected")

Class()

test()
"""
_, stdout, _ = assert_python_ok("-c", code)
self.assertRegex(stdout, b"Type weakref with callback is None as expected")
self.assertRegex(stdout, b"Type weakref is Class as expected")
self.assertRegex(stdout, b"Instance weakref is None as expected")


class IncrementalGCTests(unittest.TestCase):
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
Expand Down
32 changes: 32 additions & 0 deletions Lib/test/test_weakref.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,38 @@ def callback(obj):
stderr = res.err.decode("ascii", "backslashreplace")
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
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")
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")

Class()

test()
gc.collect()
self.assertEqual(errors, [])


class SubclassableWeakrefTestCase(TestBase):

Expand Down