From 7c2efccfe0b0bb683fbbda0e7881bd03ca8dcdd6 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 18 Mar 2024 08:57:27 +0100 Subject: [PATCH] gh-116946: Emit RuntimeError if a heap does not implement traverse --- Objects/typeobject.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 24f31492985164..c462ebd0f6339e 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -4709,6 +4709,15 @@ _PyType_FromMetaclass_impl( } } + if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) + || type->tp_traverse == NULL) + { + if (PyErr_WarnFormat(PyExc_RuntimeError, 1, + "heap type %N should implement tp_traverse and visit their type", + type)) { + goto finally; + } + } assert(_PyType_CheckConsistency(type)); finally: