Skip to content
Merged
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