8000 gh-135552: Add tests that check if weakref for tp_subclasses cleared after finalization by sergey-miryanov · Pull Request #136304 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-135552: Add tests that check if weakref for tp_subclasses cleared after finalization #136304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Make tests a little bit reliable
  • Loading branch information
sergey-miryanov committed Jul 5, 2025
commit 1da73b37fe813c77bae73adf9b29f4a898cf3cb8
19 changes: 11 additions & 8 deletions Lib/test/test_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,14 +1168,15 @@ def __init__(self):
self._z = weakref.ref(Class, lambda x: None)

def __del__(self):
assert self._z() is None, "Type weakref is not None"
if self._z() is None:
print("Type weakref is None as expected")

Class()

test()
"""
_, _, stderr = assert_python_ok("-c", code)
assert b"Type weakref is not None" not in stderr
_, stdout, _ = assert_python_ok("-c", code)
assert b"Type weakref is None as expected" in stdout

def test_type_weakref_without_callback_should_be_not_none(self):
# This test checks that weakrefs for types without callbacks
Expand All @@ -1190,16 +1191,18 @@ def __init__(self):
self._z = weakref.ref(Class)

def __del__(self):
assert self._x() is None, "Instance weakref is not None"
assert self._z() is Class, "Type weakref is not Class"
if self._x() is None:
print("Instance weakref is None as expected")
if self._z() is Class:
print("Type weakref is Class as expected")

Class()

test()
"""
_, _, stderr = assert_python_ok("-c", code)
assert b"Instance weakref is not None" not in stderr
assert b"Type weakref is not Class" not in stderr
_, stdout, _ = assert_python_ok("-c", code)
assert b"Instance weakref is None as expected" in stdout
assert b"Type weakref is Class as expected" in stdout


class IncrementalGCTests(unittest.TestCase):
Expand Down
Loading
0