|
| 1 | +#include "pybind11_tests.h" |
| 2 | + |
| 3 | +#include <pybind11/smart_holder.h> |
| 4 | + |
| 5 | +#include <memory> |
| 6 | + |
| 7 | +namespace pybind11_tests { |
| 8 | +namespace class_sh_void_ptr_capsule { |
| 9 | + |
| 10 | +// Without the helper we will run into a type_caster::load recursion. |
| 11 | +// This is because whenever the type_caster::load is called, it checks |
| 12 | +// whether the object defines an `as_` method that returns the void pointer |
| 13 | +// capsule. If yes, it calls the method. But in the following testcases, those |
| 14 | +// `as_` methods are defined with pybind11, which implicitly takes the object |
| 15 | +// itself as the first parameter. Therefore calling those methods causes loading |
| 16 | +// the object again, which causes infinite recursion. |
| 17 | +// This test is unusual in the sense that the void pointer capsules are meant to |
| 18 | +// be provided by objects wrapped with systems other than pybind11 |
| 19 | +// (i.e. having to avoid the recursion is an artificial problem, not the norm). |
| 20 | +// Conveniently, the helper also serves to keep track of `capsule_generated`. |
| 21 | +struct HelperBase { |
| 22 | + HelperBase() = default; |
| 23 | + virtual ~HelperBase() = default; |
| 24 | + |
| 25 | + bool capsule_generated = false; |
| 26 | + virtual int get() const { return 100; } |
| 27 | +}; |
| 28 | + |
| 29 | +struct Valid: public HelperBase { |
| 30 | + int get() const override { return 101; } |
| 31 | + |
| 32 | + PyObject* as_pybind11_tests_class_sh_void_ptr_capsule_Valid() { |
| 33 | + void* vptr = dynamic_cast<void*>(this); |
| 34 | + capsule_generated = true; |
| 35 | + // We assume vptr out lives the capsule, so we use nullptr for the |
| 36 | + // destructor. |
| 37 | + return PyCapsule_New( |
| 38 | + vptr, "::pybind11_tests::class_sh_void_ptr_capsule::Valid", |
| 39 | + nullptr); |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +struct NoConversion: public HelperBase { |
| 44 | + int get() const override { return 102; } |
| 45 | +}; |
| 46 | + |
| 47 | +struct NoCapsuleReturned: public HelperBase { |
| 48 | + int get() const { return 103; } |
| 49 | + |
| 50 | + PyObject* as_pybind11_tests_class_sh_void_ptr_capsule_NoCapsuleReturned() { |
| 51 | + capsule_generated = true; |
| 52 | + Py_XINCREF(Py_None); |
| 53 | + return Py_None; |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +struct AsAnotherObject: public HelperBase { |
| 58 | + int get() const override { return 104; } |
| 59 | + |
| 60 | + PyObject* as_pybind11_tests_class_sh_void_ptr_capsule_Valid() { |
| 61 | + void* vptr = dynamic_cast<void*>(this); |
| 62 | + capsule_generated = true; |
| 63 | + // We assume vptr out lives the capsule, so we use nullptr for the |
| 64 | + // destructor. |
| 65 | + return PyCapsule_New( |
| 66 | + vptr, "::pybind11_tests::class_sh_void_ptr_capsule::Valid", |
| 67 | + nullptr); |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +int get_from_valid_capsule(const Valid* c) { |
| 72 | + return c->get(); |
| 73 | +} |
| 74 | + |
| 75 | +int get_from_shared_ptr_valid_capsule(std::shared_ptr<Valid> c) { |
| 76 | + return c->get(); |
| 77 | +} |
| 78 | + |
| 79 | +int get_from_unique_ptr_valid_capsule(std::unique_ptr<Valid> c) { |
| 80 | + return c->get(); |
| 81 | +} |
| 82 | + |
| 83 | +int get_from_no_conversion_capsule(const NoConversion* c) { |
| 84 | + return c->get(); |
| 85 | +} |
| 86 | + |
| 87 | +int get_from_no_capsule_returned(const NoCapsuleReturned* c) { |
| 88 | + return c->get(); |
| 89 | +} |
| 90 | + |
| 91 | +} // namespace class_sh_void_ptr_capsule |
| 92 | +} // namespace pybind11_tests |
| 93 | + |
| 94 | +PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::HelperBase) |
| 95 | +PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::Valid) |
| 96 | +PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::NoConversion) |
| 97 | +PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::NoCapsuleReturned) |
| 98 | +PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::AsAnotherObject) |
| 99 | + |
| 100 | +TEST_SUBMODULE(class_sh_void_ptr_capsule, m) { |
| 101 | + using namespace pybind11_tests::class_sh_void_ptr_capsule; |
| 102 | + |
| 103 | + py::classh<HelperBase>(m, "HelperBase") |
| 104 | + .def(py::init<>()) |
| 105 | + .def("get", &HelperBase::get) |
| 106 | + .def_readonly("capsule_generated", &HelperBase::capsule_generated); |
| 107 | + |
| 108 | + py::classh<Valid, HelperBase>(m, "Valid") |
| 109 | + .def(py::init<>()) |
| 110 | + .def("as_pybind11_tests_class_sh_void_ptr_capsule_Valid", |
| 111 | + [](HelperBase* self) { |
| 112 | + Valid *obj = dynamic_cast<Valid *>(self); |
| 113 | + assert(obj != nullptr); |
| 114 | + PyObject* capsule = obj->as_pybind11_tests_class_sh_void_ptr_capsule_Valid(); |
| 115 | + return pybind11::reinterpret_steal<py::capsule>(capsule); |
| 116 | + }); |
| 117 | + |
| 118 | + py::classh<NoConversion, HelperBase>(m, "NoConversion") |
| 119 | + .def(py::init<>()); |
| 120 | + |
| 121 | + py::classh<NoCapsuleReturned, HelperBase>(m, "NoCapsuleReturned") |
| 122 | + .def(py::init<>()) |
| 123 | + .def("as_pybind11_tests_class_sh_void_ptr_capsule_NoCapsuleReturned", |
| 124 | + [](HelperBase* self) { |
| 125 | + NoCapsuleReturned *obj = dynamic_cast<NoCapsuleReturned *>(self); |
| 126 | + assert(obj != nullptr); |
| 127 | + PyObject* capsule = obj->as_pybind11_tests_class_sh_void_ptr_capsule_NoCapsuleReturned(); |
| 128 | + return pybind11::reinterpret_steal<py::capsule>(capsule); |
| 129 | + }); |
| 130 | + |
| 131 | + py::classh<AsAnotherObject, HelperBase>(m, "AsAnotherObject") |
| 132 | + .def(py::init<>()) |
| 133 | + .def("as_pybind11_tests_class_sh_void_ptr_capsule_Valid", |
| 134 | + [](HelperBase* self) { |
| 135 | + AsAnotherObject *obj = dynamic_cast<AsAnotherObject *>(self); |
| 136 | + assert(obj != nullptr); |
| 137 | + PyObject* capsule = obj->as_pybind11_tests_class_sh_void_ptr_capsule_Valid(); |
| 138 | + return pybind11::reinterpret_steal<py::capsule>(capsule); |
| 139 | + }); |
| 140 | + |
| 141 | + m.def("get_from_valid_capsule", &get_from_valid_capsule); |
| 142 | + m.def("get_from_shared_ptr_valid_capsule", &get_from_shared_ptr_valid_capsule); |
| 143 | + m.def("get_from_unique_ptr_valid_capsule", &get_from_unique_ptr_valid_capsule); |
| 144 | + m.def("get_from_no_conversion_capsule", &get_from_no_conversion_capsule); |
| 145 | + m.def("get_from_no_capsule_returned", &get_from_no_capsule_returned); |
| 146 | +} |
0 commit comments