File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -26,13 +26,21 @@ TEST_SUBMODULE(custom_type_setup, m) {
2626 auto *type = &heap_type->ht_type ;
2727 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2828 type->tp_traverse = [](PyObject *self_base, visitproc visit, void *arg) {
29- auto &self = py::cast<OwnsPythonObjects &>(py::handle (self_base));
30- Py_VISIT (self.value .ptr ());
29+ // https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_traverse
30+ #if PY_VERSION_HEX >= 0x03090000 // Python 3.9
31+ Py_VISIT (Py_TYPE (self_base));
32+ #endif
33+ if (py::detail::is_holder_constructed (self_base)) {
34+ auto &self = py::cast<OwnsPythonObjects &>(py::handle (self_base));
35+ Py_VISIT (self.value .ptr ());
36+ }
3137 return 0 ;
3238 };
3339 type->tp_clear = [](PyObject *self_base) {
34- auto &self = py::cast<OwnsPythonObjects &>(py::handle (self_base));
35- self.value = py::none ();
40+ if (py::detail::is_holder_constructed (self_base)) {
41+ auto &self = py::cast<OwnsPythonObjects &>(py::handle (self_base));
42+ self.value = py::none ();
43+ }
3644 return 0 ;
3745 };
3846 }));
You can’t perform that action at this time.
0 commit comments