|
7 | 7 | namespace pybind11_tests { |
8 | 8 | namespace class_sh_void_ptr_capsule { |
9 | 9 |
|
10 | | -// Conveniently, the helper serves to keep track of `capsule_generated`. |
11 | | -struct HelperBase { |
12 | | - HelperBase() = default; |
13 | | - HelperBase(const HelperBase &) = delete; |
14 | | - virtual ~HelperBase() = default; |
15 | | - |
16 | | - bool capsule_generated = false; |
17 | | - virtual int get() const { return 100; } |
18 | | -}; |
| 10 | +struct Valid {}; |
19 | 11 |
|
20 | | -struct Valid : public HelperBase { |
21 | | - int get() const override { return 101; } |
| 12 | +struct NoConversion {}; |
22 | 13 |
|
23 | | - PyObject *as_pybind11_tests_class_sh_void_ptr_capsule_Valid() { |
24 | | - void *vptr = dynamic_cast<void *>(this); |
25 | | - capsule_generated = true; |
26 | | - // We assume vptr out lives the capsule, so we use nullptr for the |
27 | | - // destructor. |
28 | | - return PyCapsule_New(vptr, "::pybind11_tests::class_sh_void_ptr_capsule::Valid", nullptr); |
29 | | - } |
30 | | -}; |
| 14 | +struct NoCapsuleReturned {}; |
31 | 15 |
|
32 | | -struct NoConversion : public HelperBase { |
33 | | - int get() const override { return 102; } |
34 | | -}; |
| 16 | +struct AsAnotherObject {}; |
35 | 17 |
|
36 | | -struct NoCapsuleReturned : public HelperBase { |
37 | | - int get() const override { return 103; } |
| 18 | +// Create a void pointer capsule for test. The encapsulated object does not |
| 19 | +// matter for this test case. |
| 20 | +PyObject *create_test_void_ptr_capsule() { |
| 21 | + static int just_to_have_something_to_point_to = 0; |
| 22 | + return PyCapsule_New(static_cast<void *>(&just_to_have_something_to_point_to), "int", nullptr); |
| 23 | +} |
38 | 24 |
|
39 | | - PyObject *as_pybind11_tests_class_sh_void_ptr_capsule_NoCapsuleReturned() { |
40 | | - capsule_generated = true; |
41 | | - Py_XINCREF(Py_None); |
42 | | - return Py_None; |
43 | | - } |
44 | | -}; |
| 25 | +int get_from_valid_capsule(const Valid *) { return 1; } |
45 | 26 |
|
46 | | -struct AsAnotherObject : public HelperBase { |
47 | | - int get() const override { return 104; } |
| 27 | +int get_from_shared_ptr_valid_capsule(const std::shared_ptr<Valid> &) { return 2; } |
48 | 28 |
|
49 | | - PyObject *as_pybind11_tests_class_sh_void_ptr_capsule_Valid() { |
50 | | - void *vptr = dynamic_cast<void *>(this); |
51 | | - capsule_generated = true; |
52 | | - // We assume vptr out lives the capsule, so we use nullptr for the |
53 | | - // destructor. |
54 | | - return PyCapsule_New(vptr, "::pybind11_tests::class_sh_void_ptr_capsule::Valid", nullptr); |
55 | | - } |
56 | | -}; |
| 29 | +int get_from_unique_ptr_valid_capsule(std::unique_ptr<Valid>) { return 3; } |
| 30 | + |
| 31 | +int get_from_no_conversion_capsule(const NoConversion *) { return 4; } |
| 32 | + |
| 33 | +int get_from_no_capsule_returned(const NoCapsuleReturned *) { return 5; } |
57 | 34 |
|
58 | 35 | // https://github.com/pybind/pybind11/issues/3788 |
59 | 36 | struct TypeWithGetattr { |
60 | 37 | TypeWithGetattr() = default; |
61 | 38 | int get_42() const { return 42; } |
62 | 39 | }; |
63 | 40 |
|
64 | | -int get_from_valid_capsule(const Valid *c) { return c->get(); } |
65 | | - |
66 | | -int get_from_shared_ptr_valid_capsule(const std::shared_ptr<Valid> &c) { return c->get(); } |
| 41 | +// https://github.com/pybind/pybind11/issues/3804 |
| 42 | +struct Base1 { |
| 43 | + int a1{}; |
| 44 | +}; |
| 45 | +struct Base2 { |
| 46 | + int a2{}; |
| 47 | +}; |
67 | 48 |
|
68 | | -int get_from_unique_ptr_valid_capsule(std::unique_ptr<Valid> c) { return c->get(); } |
| 49 | +struct Base12 : Base1, Base2 { |
| 50 | + int foo() const { return 0; } |
| 51 | +}; |
69 | 52 |
|
70 | | -int get_from_no_conversion_capsule(const NoConversion *c) { return c->get(); } |
| 53 | +struct Derived1 : Base12 { |
| 54 | + int bar() const { return 1; } |
| 55 | +}; |
71 | 56 |
|
72 | | -int get_from_no_capsule_returned(const NoCapsuleReturned *c) { return c->get(); } |
| 57 | +struct Derived2 : Base12 { |
| 58 | + int bar() const { return 2; } |
| 59 | +}; |
73 | 60 |
|
74 | 61 | } // namespace class_sh_void_ptr_capsule |
75 | 62 | } // namespace pybind11_tests |
76 | 63 |
|
77 | | -PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::HelperBase) |
78 | 64 | PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::Valid) |
79 | | -PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::NoConversion) |
80 | | -PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::NoCapsuleReturned) |
81 | | -PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::AsAnotherObject) |
82 | 65 | PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::TypeWithGetattr) |
| 66 | +PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::Base1) |
| 67 | +PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::Base2) |
| 68 | +PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::Base12) |
| 69 | +PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::Derived1) |
| 70 | +PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::Derived2) |
83 | 71 |
|
84 | 72 | TEST_SUBMODULE(class_sh_void_ptr_capsule, m) { |
85 | 73 | using namespace pybind11_tests::class_sh_void_ptr_capsule; |
86 | 74 |
|
87 | | - py::classh<HelperBase>(m, "HelperBase") |
88 | | - .def(py::init<>()) |
89 | | - .def("get", &HelperBase::get) |
90 | | - .def_readonly("capsule_generated", &HelperBase::capsule_generated); |
91 | | - |
92 | | - py::classh<Valid, HelperBase>(m, "Valid") |
93 | | - .def(py::init<>()) |
94 | | - .def("as_pybind11_tests_class_sh_void_ptr_capsule_Valid", [](Valid &self) { |
95 | | - PyObject *capsule = self.as_pybind11_tests_class_sh_void_ptr_capsule_Valid(); |
96 | | - return pybind11::reinterpret_steal<py::capsule>(capsule); |
97 | | - }); |
98 | | - |
99 | | - py::classh<NoConversion, HelperBase>(m, "NoConversion").def(py::init<>()); |
100 | | - |
101 | | - py::classh<NoCapsuleReturned, HelperBase>(m, "NoCapsuleReturned") |
102 | | - .def(py::init<>()) |
103 | | - .def("as_pybind11_tests_class_sh_void_ptr_capsule_NoCapsuleReturned", |
104 | | - [](NoCapsuleReturned &self) { |
105 | | - PyObject *capsule |
106 | | - = self.as_pybind11_tests_class_sh_void_ptr_capsule_NoCapsuleReturned(); |
107 | | - return pybind11::reinterpret_steal<py::capsule>(capsule); |
108 | | - }); |
109 | | - |
110 | | - py::classh<AsAnotherObject, HelperBase>(m, "AsAnotherObject") |
111 | | - .def(py::init<>()) |
112 | | - .def("as_pybind11_tests_class_sh_void_ptr_capsule_Valid", [](AsAnotherObject &self) { |
113 | | - PyObject *capsule = self.as_pybind11_tests_class_sh_void_ptr_capsule_Valid(); |
114 | | - return pybind11::reinterpret_steal<py::capsule>(capsule); |
115 | | - }); |
| 75 | + py::classh<Valid>(m, "Valid"); |
116 | 76 |
|
117 | 77 | m.def("get_from_valid_capsule", &get_from_valid_capsule); |
118 | 78 | m.def("get_from_shared_ptr_valid_capsule", &get_from_shared_ptr_valid_capsule); |
119 | 79 | m.def("get_from_unique_ptr_valid_capsule", &get_from_unique_ptr_valid_capsule); |
120 | 80 | m.def("get_from_no_conversion_capsule", &get_from_no_conversion_capsule); |
121 | 81 | m.def("get_from_no_capsule_returned", &get_from_no_capsule_returned); |
| 82 | + m.def("create_test_void_ptr_capsule", []() { |
| 83 | + PyObject *capsule = create_test_void_ptr_capsule(); |
| 84 | + return pybind11::reinterpret_steal<py::capsule>(capsule); |
| 85 | + }); |
122 | 86 |
|
123 | 87 | py::classh<TypeWithGetattr>(m, "TypeWithGetattr") |
124 | 88 | .def(py::init<>()) |
125 | 89 | .def("get_42", &TypeWithGetattr::get_42) |
126 | 90 | .def("__getattr__", |
127 | 91 | [](TypeWithGetattr &, const std::string &key) { return "GetAttr: " + key; }); |
| 92 | + |
| 93 | + py::classh<Base1>(m, "Base1"); |
| 94 | + py::classh<Base2>(m, "Base2"); |
| 95 | + |
| 96 | + py::classh<Base12, Base1, Base2>(m, "Base12") |
| 97 | + .def(py::init<>()) |
| 98 | + .def("foo", &Base12::foo) |
| 99 | + .def("__getattr__", |
| 100 | + [](Base12 &, const std::string &key) { return "Base GetAttr: " + key; }); |
| 101 | + |
| 102 | + py::classh<Derived1, Base12>(m, "Derived1").def(py::init<>()).def("bar", &Derived1::bar); |
| 103 | + |
| 104 | + py::classh<Derived2, Base12>(m, "Derived2").def(py::init<>()).def("bar", &Derived2::bar); |
128 | 105 | } |
0 commit comments