Skip to content

Commit 015be88

Browse files
committed
Update tests in test_custom_type_setup.cpp
1 parent ad587eb commit 015be88

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

tests/test_custom_type_setup.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff 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
}));

0 commit comments

Comments
 (0)