From 4d648b48aaabb1bf0cf76414d1ac629cadaf7534 Mon Sep 17 00:00:00 2001
From: Mikhail Borisov <43937008+fxeqxmulfx@users.noreply.github.com>
Date: Mon, 23 Jun 2025 00:27:39 +0500
Subject: [PATCH 01/12] Add tests
---
Lib/test/test_gc.py | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py
index b4cbfb6d774080..710a2b3eb2af73 100644
--- a/Lib/test/test_gc.py
+++ b/Lib/test/test_gc.py
@@ -1517,6 +1517,23 @@ def test_ast_fini(self):
""")
assert_python_ok("-c", code)
+ def test_reset_type_cache_after_finalization(self):
+ # https://github.com/python/cpython/issues/135552
+ code = textwrap.dedent("""
+ class BaseNode:
+ def __del__(self):
+ BaseNode.next = BaseNode.next.next
+
+
+ class Node(BaseNode):
+ pass
+
+
+ BaseNode.next = Node()
+ BaseNode.next.next = Node()
+ """)
+ assert_python_ok("-c", code)
+
def setUpModule():
global enabled, debug
From 77e9ca7b9a0b5c5aed790735d7653120d34516e0 Mon Sep 17 00:00:00 2001
From: Sergey Miryanov
Date: Mon, 23 Jun 2025 00:37:02 +0500
Subject: [PATCH 02/12] Organize tests
---
Lib/test/test_gc.py | 47 ++++++++++++++++++++++++++++++---------------
1 file changed, 31 insertions(+), 16 deletions(-)
diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py
index 710a2b3eb2af73..58c1882886002e 100644
--- a/Lib/test/test_gc.py
+++ b/Lib/test/test_gc.py
@@ -1125,6 +1125,37 @@ def test_something(self):
""")
assert_python_ok("-c", source)
+ def test_do_not_cleanup_type_subclasses_before_finalization(self):
+ # https://github.com/python/cpython/issues/135552
+ code = textwrap.dedent("""
+ class BaseNode:
+ def __del__(self):
+ BaseNode.next = BaseNode.next.next
+
+ class Node(BaseNode):
+ pass
+
+ BaseNode.next = Node()
+ BaseNode.next.next = Node()
+ """)
+ assert_python_ok("-c", code)
+
+ code_inside_function = textwrap.dedent("""
+ def test():
+ class BaseNode:
+ def __del__(self):
+ BaseNode.next = BaseNode.next.next
+
+ class Node(BaseNode):
+ pass
+
+ BaseNode.next = Node()
+ BaseNode.next.next = Node()
+
+ test()
+ """)
+ assert_python_ok("-c", code_inside_function)
+
class IncrementalGCTests(unittest.TestCase):
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
@@ -1517,22 +1548,6 @@ def test_ast_fini(self):
""")
assert_python_ok("-c", code)
- def test_reset_type_cache_after_finalization(self):
- # https://github.com/python/cpython/issues/135552
- code = textwrap.dedent("""
- class BaseNode:
- def __del__(self):
- BaseNode.next = BaseNode.next.next
-
-
- class Node(BaseNode):
- pass
-
-
- BaseNode.next = Node()
- BaseNode.next.next = Node()
- """)
- assert_python_ok("-c", code)
def setUpModule():
From b6825da9df07a27a19a8ef36a870c8f0b46d5f29 Mon Sep 17 00:00:00 2001
From: Sergey Miryanov
Date: Tue, 24 Jun 2025 15:07:38 +0500
Subject: [PATCH 03/12] Simplify tests
---
Lib/test/test_gc.py | 19 ++++++-------------
1 file changed, 6 insertions(+), 13 deletions(-)
diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py
index 58c1882886002e..a6d9ce6f91ff2d 100644
--- a/Lib/test/test_gc.py
+++ b/Lib/test/test_gc.py
@@ -1127,30 +1127,23 @@ def test_something(self):
def test_do_not_cleanup_type_subclasses_before_finalization(self):
# https://github.com/python/cpython/issues/135552
- code = textwrap.dedent("""
+ code = """
class BaseNode:
def __del__(self):
BaseNode.next = BaseNode.next.next
+ BaseNode.next.next
class Node(BaseNode):
pass
BaseNode.next = Node()
BaseNode.next.next = Node()
- """)
- assert_python_ok("-c", code)
+ """
+ assert_python_ok("-c", textwrap.dedent(code))
- code_inside_function = textwrap.dedent("""
+ code_inside_function = textwrap.dedent(F"""
def test():
- class BaseNode:
- def __del__(self):
- BaseNode.next = BaseNode.next.next
-
- class Node(BaseNode):
- pass
-
- BaseNode.next = Node()
- BaseNode.next.next = Node()
+ {textwrap.indent(code, ' ')}
test()
""")
From f62d43e6af2dc76a6c96f950ec3acce03e8dbf41 Mon Sep 17 00:00:00 2001
From: Sergey Miryanov
Date: Fri, 4 Jul 2025 23:51:37 +0500
Subject: [PATCH 04/12] Remove extra line
---
Lib/test/test_gc.py | 1 -
1 file changed, 1 deletion(-)
diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py
index a6d9ce6f91ff2d..552d9f937633dd 100644
--- a/Lib/test/test_gc.py
+++ b/Lib/test/test_gc.py
@@ -1542,7 +1542,6 @@ def test_ast_fini(self):
assert_python_ok("-c", code)
-
def setUpModule():
global enabled, debug
enabled = gc.isenabled()
From 398c4f58f5dd07746a5969d63676e509efa5669d Mon Sep 17 00:00:00 2001
From: Sergey Miryanov
Date: Sat, 5 Jul 2025 00:05:33 +0500
Subject: [PATCH 05/12] Add extra comments
---
Lib/test/test_gc.py | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py
index 552d9f937633dd..4cd80d136cfe39 100644
--- a/Lib/test/test_gc.py
+++ b/Lib/test/test_gc.py
@@ -1127,11 +1127,15 @@ def test_something(self):
def test_do_not_cleanup_type_subclasses_before_finalization(self):
# https://github.com/python/cpython/issues/135552
+ # If we cleanup weakrefs for tp_subclasses before calling
+ # the finalizer (__del__) then the line `fail = BaseNode.next.next`
+ # should fail because we are trying to access a subclass
+ # atribute. But subclass type cache was not properly invalidated.
code = """
class BaseNode:
def __del__(self):
BaseNode.next = BaseNode.next.next
- BaseNode.next.next
+ fail = BaseNode.next.next
class Node(BaseNode):
pass
@@ -1139,6 +1143,8 @@ class Node(BaseNode):
BaseNode.next = Node()
BaseNode.next.next = Node()
"""
+ # this test checks garbage collection while interp
+ # finalization
assert_python_ok("-c", textwrap.dedent(code))
code_inside_function = textwrap.dedent(F"""
@@ -1147,6 +1153,7 @@ def test():
test()
""")
+ # this test checks regular garbage collection
assert_python_ok("-c", code_inside_function)
From 7dd966925f469f715b1503bf44954ab002ac47d0 Mon Sep 17 00:00:00 2001
From: Sergey Miryanov
Date: Sat, 5 Jul 2025 01:13:11 +0500
Subject: [PATCH 06/12] Add tests for checking that weakrefs clears before and
after finalization
---
Lib/test/test_gc.py | 45 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+)
diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py
index 4cd80d136cfe39..8850584358f1b8 100644
--- a/Lib/test/test_gc.py
+++ b/Lib/test/test_gc.py
@@ -1156,6 +1156,51 @@ def test():
# this test checks regular garbage collection
assert_python_ok("-c", code_inside_function)
+ def test_type_weakref_with_callback_should_be_none(self):
+ # This test checks that weakrefs for types with callbacks
+ # are cleared before the finalizer is called
+ code = """
+ import weakref
+ def test():
+ class Class:
+ def __init__(self):
+ self._self = self
+ self._z = weakref.ref(Class, lambda x: None)
+
+ def __del__(self):
+ assert self._z() is None, "Type weakref is not None"
+
+ Class()
+
+ test()
+ """
+ _, _, stderr = assert_python_ok("-c", code)
+ assert b"Type weakref is not None" not in stderr
+
+ def test_type_weakref_without_callback_should_be_not_none(self):
+ # This test checks that weakrefs for types without callbacks
+ # are cleared after the finalizer is called
+ code = """
+ import weakref
+ def test():
+ class Class:
+ def __init__(self):
+ self._self = self
+ self._x = weakref.ref(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"
+
+ 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
+
class IncrementalGCTests(unittest.TestCase):
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
From 1da73b37fe813c77bae73adf9b29f4a898cf3cb8 Mon Sep 17 00:00:00 2001
From: Sergey Miryanov
Date: Sat, 5 Jul 2025 21:10:36 +0500
Subject: [PATCH 07/12] Make tests a little bit reliable
---
Lib/test/test_gc.py | 19 +++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)
diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py
index 8850584358f1b8..f6f658de2cb638 100644
--- a/Lib/test/test_gc.py
+++ b/Lib/test/test_gc.py
@@ -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
@@ -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):
From 05105397ead70678bf2484f11772e845c038b407 Mon Sep 17 00:00:00 2001
From: Sergey Miryanov
Date: Sat, 5 Jul 2025 21:57:21 +0500
Subject: [PATCH 08/12] Simplify tests
---
Lib/test/test_gc.py | 48 ++++++++++++++++-----------------------------
1 file changed, 17 insertions(+), 31 deletions(-)
diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py
index f6f658de2cb638..3c2c110656cd4d 100644
--- a/Lib/test/test_gc.py
+++ b/Lib/test/test_gc.py
@@ -1156,53 +1156,39 @@ def test():
# this test checks regular garbage collection
assert_python_ok("-c", code_inside_function)
- def test_type_weakref_with_callback_should_be_none(self):
- # This test checks that weakrefs for types with callbacks
- # are cleared before the finalizer is called
+ 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._z = weakref.ref(Class, lambda x: None)
+ self._1 = weakref.ref(Class, lambda x: None)
+ self._2 = weakref.ref(Class)
+ self._3 = weakref.ref(self)
def __del__(self):
- if self._z() is None:
- print("Type weakref is None as expected")
-
- Class()
-
- test()
- """
- _, 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
- # are cleared after the finalizer is called
- code = """
- import weakref
- def test():
- class Class:
- def __init__(self):
- self._self = self
- self._x = weakref.ref(self)
- self._z = weakref.ref(Class)
-
- def __del__(self):
- if self._x() is None:
- print("Instance weakref is None as expected")
- if self._z() is Class:
+ 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)
- assert b"Instance weakref is None as expected" in stdout
+ assert b"Type weakref with callback is None as expected" in stdout
assert b"Type weakref is Class as expected" in stdout
+ assert b"Instance weakref is None as expected" in stdout
class IncrementalGCTests(unittest.TestCase):
From 50410d6b90ec8fd4a957678ada48431b648b1a22 Mon Sep 17 00:00:00 2001
From: Sergey Miryanov
Date: Sun, 6 Jul 2025 10:50:29 +0500
Subject: [PATCH 09/12] Use unittest asserts
---
Lib/test/test_gc.py | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py
index 3c2c110656cd4d..37a4f4d69e434d 100644
--- a/Lib/test/test_gc.py
+++ b/Lib/test/test_gc.py
@@ -1186,9 +1186,9 @@ def __del__(self):
test()
"""
_, stdout, _ = assert_python_ok("-c", code)
- assert b"Type weakref with callback is None as expected" in stdout
- assert b"Type weakref is Class as expected" in stdout
- assert b"Instance weakref is None as expected" in stdout
+ 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):
From aa18e7431ec27e9049f2c7badc024469979d8591 Mon Sep 17 00:00:00 2001
From: Sergey Miryanov
Date: Tue, 8 Jul 2025 12:07:26 +0500
Subject: [PATCH 10/12] Move test_clearing_weakrefs_in_gc to test_weakref
---
Lib/test/test_gc.py | 34 ----------------------------------
Lib/test/test_weakref.py | 32 ++++++++++++++++++++++++++++++++
2 files changed, 32 insertions(+), 34 deletions(-)
diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py
index 37a4f4d69e434d..4cd80d136cfe39 100644
--- a/Lib/test/test_gc.py
+++ b/Lib/test/test_gc.py
@@ -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")
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py
index 4c7c900eb56ae1..8754e3ee5c99c0 100644
--- a/Lib/test/test_weakref.py
+++ b/Lib/test/test_weakref.py
@@ -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):
From 7788e5537014c96d729c73d7982cf208c16deef5 Mon Sep 17 00:00:00 2001
From: Sergey Miryanov
Date: Tue, 8 Jul 2025 12:28:26 +0500
Subject: [PATCH 11/12] Apply suggestions from code review
Co-authored-by: Kumar Aditya
---
Lib/test/test_gc.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py
index 4cd80d136cfe39..53f4ed788bdfc9 100644
--- a/Lib/test/test_gc.py
+++ b/Lib/test/test_gc.py
@@ -1126,11 +1126,11 @@ def test_something(self):
assert_python_ok("-c", source)
def test_do_not_cleanup_type_subclasses_before_finalization(self):
- # https://github.com/python/cpython/issues/135552
+ # See https://github.com/python/cpython/issues/135552
# If we cleanup weakrefs for tp_subclasses before calling
# the finalizer (__del__) then the line `fail = BaseNode.next.next`
# should fail because we are trying to access a subclass
- # atribute. But subclass type cache was not properly invalidated.
+ # attribute. But subclass type cache was not properly invalidated.
code = """
class BaseNode:
def __del__(self):
From 0339d544908326dfb5a9c0c3bb138f40b654c65f Mon Sep 17 00:00:00 2001
From: Neil Schemenauer
Date: Thu, 7 Aug 2025 16:53:30 -0700
Subject: [PATCH 12/12] Revise test_clearing_weakrefs_in_gc test
Types are not treated specially, it only matters if the weakref has a callback or not.
---
Lib/test/test_weakref.py | 16 +++++-----------
1 file changed, 5 insertions(+), 11 deletions(-)
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py
index 8754e3ee5c99c0..47f6b46061ac30 100644
--- a/Lib/test/test_weakref.py
+++ b/Lib/test/test_weakref.py
@@ -1045,12 +1045,9 @@ 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:
@@ -1058,17 +1055,14 @@ 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()