Skip to content

Commit 69abfb5

Browse files
Test PR
1 parent 4dc33d6 commit 69abfb5

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

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)