Skip to content

Commit 0510539

Browse files
Simplify tests
1 parent 1da73b3 commit 0510539

1 file changed

Lines changed: 17 additions & 31 deletions

File tree

Lib/test/test_gc.py

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,53 +1156,39 @@ def test():
11561156
# this test checks regular garbage collection
11571157
assert_python_ok("-c", code_inside_function)
11581158

1159-
def test_type_weakref_with_callback_should_be_none(self):
1160-
# This test checks that weakrefs for types with callbacks
1161-
# are cleared before the finalizer is called
1159+
def test_clearing_weakrefs_in_gc(self):
1160+
# This test checks that:
1161+
# 1. weakrefs for types with callbacks are cleared before the
1162+
# finalizer is called
1163+
# 2. weakrefs for types without callbacks are cleared after the
1164+
# finalizer is called
1165+
# 3. other weakrefs cleared before the finalizer is called
11621166
code = """
11631167
import weakref
11641168
def test():
11651169
class Class:
11661170
def __init__(self):
11671171
self._self = self
1168-
self._z = weakref.ref(Class, lambda x: None)
1172+
self._1 = weakref.ref(Class, lambda x: None)
1173+
self._2 = weakref.ref(Class)
1174+
self._3 = weakref.ref(self)
11691175
11701176
def __del__(self):
1171-
if self._z() is None:
1172-
print("Type weakref is None as expected")
1173-
1174-
Class()
1175-
1176-
test()
1177-
"""
1178-
_, stdout, _ = assert_python_ok("-c", code)
1179-
assert b"Type weakref is None as expected" in stdout
1180-
1181-
def test_type_weakref_without_callback_should_be_not_none(self):
1182-
# This test checks that weakrefs for types without callbacks
1183-
# are cleared after the finalizer is called
1184-
code = """
1185-
import weakref
1186-
def test():
1187-
class Class:
1188-
def __init__(self):
1189-
self._self = self
1190-
self._x = weakref.ref(self)
1191-
self._z = weakref.ref(Class)
1192-
1193-
def __del__(self):
1194-
if self._x() is None:
1195-
print("Instance weakref is None as expected")
1196-
if self._z() is Class:
1177+
if self._1() is None:
1178+
print("Type weakref with callback is None as expected")
1179+
if self._2() is Class:
11971180
print("Type weakref is Class as expected")
1181+
if self._3() is None:
1182+
print("Instance weakref is None as expected")
11981183
11991184
Class()
12001185
12011186
test()
12021187
"""
12031188
_, stdout, _ = assert_python_ok("-c", code)
1204-
assert b"Instance weakref is None as expected" in stdout
1189+
assert b"Type weakref with callback is None as expected" in stdout
12051190
assert b"Type weakref is Class as expected" in stdout
1191+
assert b"Instance weakref is None as expected" in stdout
12061192

12071193

12081194
class IncrementalGCTests(unittest.TestCase):

0 commit comments

Comments
 (0)