@@ -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+
94136TEST_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