From f9f00495a3f0ef6037c215c42bd2d919590ff11f Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Sun, 10 Jul 2022 00:43:53 -0400 Subject: [PATCH] Properly visit self in >=3.9 traverse (#4051) * Properly visit self in >=3.9 traverse * Add comment about 3.9 behavior --- include/pybind11/detail/class.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index c09f70ef0..42720f844 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -531,6 +531,10 @@ extern "C" inline int pybind11_set_dict(PyObject *self, PyObject *new_dict, void extern "C" inline int pybind11_traverse(PyObject *self, visitproc visit, void *arg) { PyObject *&dict = *_PyObject_GetDictPtr(self); Py_VISIT(dict); +// https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_traverse +#if PY_VERSION_HEX >= 0x03090000 + Py_VISIT(Py_TYPE(self)); +#endif return 0; }