mirror of
https://github.com/pybind/pybind11.git
synced 2026-04-20 06:49:25 +00:00
feat: add semi-public API: pybind11::detail::is_holder_constructed (#5669)
* Add semi-public API: `pybind11::detail::is_holder_constructed` * Update docs for `pybind11::custom_type_setup` * Update tests in `test_custom_type_setup.cpp` * Apply suggestions from code review
This commit is contained in:
@@ -1384,13 +1384,21 @@ You can do that using ``py::custom_type_setup``:
|
||||
auto *type = &heap_type->ht_type;
|
||||
type->tp_flags |= Py_TPFLAGS_HAVE_GC;
|
||||
type->tp_traverse = [](PyObject *self_base, visitproc visit, void *arg) {
|
||||
auto &self = py::cast<OwnsPythonObjects&>(py::handle(self_base));
|
||||
Py_VISIT(self.value.ptr());
|
||||
// https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_traverse
|
||||
#if PY_VERSION_HEX >= 0x03090000
|
||||
Py_VISIT(Py_TYPE(self_base));
|
||||
#endif
|
||||
if (py::detail::is_holder_constructed(self_base)) {
|
||||
auto &self = py::cast<OwnsPythonObjects&>(py::handle(self_base));
|
||||
Py_VISIT(self.value.ptr());
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
type->tp_clear = [](PyObject *self_base) {
|
||||
auto &self = py::cast<OwnsPythonObjects&>(py::handle(self_base));
|
||||
self.value = py::none();
|
||||
if (py::detail::is_holder_constructed(self_base)) {
|
||||
auto &self = py::cast<OwnsPythonObjects&>(py::handle(self_base));
|
||||
self.value = py::none();
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user