Skip to content

Commit 46280a7

Browse files
Test PR.
1 parent 4dc33d6 commit 46280a7

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

tests/test_class_sh_virtual_py_cpp_mix.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,34 @@ int get_from_cpp_plainc_ptr(const Base *b) { return b->get() + 4000; }
2929

3030
int get_from_cpp_unique_ptr(std::unique_ptr<Base> b) { return b->get() + 5000; }
3131

32+
33+
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
34+
35+
struct VirtualItem
36+
{
37+
VirtualItem() = default;
38+
virtual ~VirtualItem() = default;
39+
virtual int get() const = 0;
40+
};
41+
42+
struct VirtualItemTrampoline : public VirtualItem, py::trampoline_self_life_support
43+
{
44+
int get() const override { PYBIND11_OVERRIDE_PURE(int, VirtualItem, get, ); }
45+
};
46+
47+
struct VirtualItemContainer
48+
{
49+
std::shared_ptr<VirtualItem> held;
50+
// ...
51+
};
52+
53+
void cpp_space_test(std::shared_ptr<VirtualItemContainer> container_vec)
54+
{
55+
auto i = container_vec->held->get(); // segfaults
56+
}
57+
58+
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
59+
3260
struct BaseVirtualOverrider : Base, py::trampoline_self_life_support {
3361
using Base::Base;
3462

@@ -55,4 +83,19 @@ TEST_SUBMODULE(class_sh_virtual_py_cpp_mix, m) {
5583

5684
m.def("get_from_cpp_plainc_ptr", get_from_cpp_plainc_ptr, py::arg("b"));
5785
m.def("get_from_cpp_unique_ptr", get_from_cpp_unique_ptr, py::arg("b"));
86+
87+
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
88+
89+
py::classh<VirtualItem, VirtualItemTrampoline>(m, "VirtualItem")
90+
.def(py::init<>())
91+
.def("get", &VirtualItem::get)
92+
;
93+
94+
py::classh<VirtualItemContainer>(m, "VirtualItemContainer")
95+
.def(py::init<std::shared_ptr<VirtualItem>>())
96+
;
97+
98+
m.def("cpp_space_test", cpp_space_test);
99+
100+
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
58101
}

tests/test_class_sh_virtual_py_cpp_mix.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,14 @@ def test_get_from_cpp_plainc_ptr(ctor, expected):
6464
def test_get_from_cpp_unique_ptr(ctor, expected):
6565
obj = ctor()
6666
assert m.get_from_cpp_unique_ptr(obj) == expected
67+
68+
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
69+
70+
class VirtualItemDerived(m.VirtualItem):
71+
def getInt(self):
72+
return 42
73+
74+
def test_cpp_space_test():
75+
m.cpp_space_test(m.VirtualItemContainer(VirtualItemDerived()))
76+
77+
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

tests/test_with_catch/test_interpreter.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,48 @@ PYBIND11_EMBEDDED_MODULE(throw_error_already_set, ) {
9191
d["missing"].cast<py::object>();
9292
}
9393

94+
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
95+
96+
97+
struct Item
98+
{
99+
Item() = default;
100+
virtual ~Item() = default;
101+
virtual int getInt() const = 0;
102+
};
103+
104+
struct ItemTrampoline : public Item, py::trampoline_self_life_support
105+
{
106+
int getInt() const override { PYBIND11_OVERRIDE_PURE(int, Item, getInt, ); }
107+
};
108+
109+
PYBIND11_EMBEDDED_MODULE(embedded_smart_holder, m) {
110+
111+
py::classh<Item, ItemTrampoline>(m, "Item")
112+
.def(py::init<>())
113+
.def("getInt", &Item::getInt)
114+
;
115+
}
116+
117+
TEST_CASE("Embedded smart holder test") {
118+
119+
auto module = py::module_::import("embedded_smart_holder");
120+
121+
auto o = module.attr("Item")(); // create py
122+
123+
auto i = o.cast<std::shared_ptr<Item>>(); // cast cpp
124+
125+
if (i->getInt() != 42) // test cpp
126+
throw std::runtime_error("Not 42");
127+
128+
o = py::object(); // release py
129+
130+
if (i->getInt() != 42) // test cpp
131+
throw std::runtime_error("Not 42 after release");
132+
}
133+
134+
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
135+
94136
TEST_CASE("PYTHONPATH is used to update sys.path") {
95137
// The setup for this TEST_CASE is in catch.cpp!
96138
auto sys_path = py::str(py::module_::import("sys").attr("path")).cast<std::string>();

0 commit comments

Comments
 (0)