18 struct OwnsPythonObjects {
19 py::object
value = py::none();
24 py::class_<OwnsPythonObjects> cls(
25 m,
"OwnsPythonObjects", py::custom_type_setup([](PyHeapTypeObject *heap_type) {
26 auto *
type = &heap_type->ht_type;
27 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
28 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());
33 type->tp_clear = [](PyObject *self_base) {
34 auto &
self = py::cast<OwnsPythonObjects &>(py::handle(self_base));
35 self.value = py::none();
39 cls.def(py::init<>());