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 diff --git a/Objects/typeobject.c b/Objects/typeobject.c index b9d549610693c1..2090db49e5ed2c 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -10767,6 +10767,8 @@ slot_tp_finalize(PyObject *self) } } + PyType_Modified(Py_TYPE(self)); + _PyThreadState_PopCStackRef(tstate, &cref); /* Restore the saved exception. */