MCPcopy Create free account
hub / github.com/pybind/pybind11 / wrap

Function wrap

tests/test_pickling.cpp:32–56  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

30struct SimpleCppDerived : SimpleBase {};
31
32void wrap(py::module m) {
33 py::class_<SimpleBase, SimpleBaseTrampoline>(m, "SimpleBase")
34 .def(py::init<>())
35 .def_readwrite("num", &SimpleBase::num)
36 .def(py::pickle(
37 [](const py::object &self) {
38 py::dict d = py::getattr(self, "__dict__", py::dict());
39 return py::make_tuple(self.attr("num"), d);
40 },
41 [](const py::tuple &t) {
42 if (t.size() != 2) {
43 throw std::runtime_error("Invalid state!");
44 }
45 auto cpp_state = std::unique_ptr<SimpleBase>(new SimpleBaseTrampoline);
46 cpp_state->num = t[0].cast<int>();
47 auto py_state = t[1].cast<py::dict>();
48 return std::make_pair(std::move(cpp_state), py_state);
49 }));
50
51 m.def("make_SimpleCppDerivedAsBase",
52 []() { return std::unique_ptr<SimpleBase>(new SimpleCppDerived); });
53 m.def("check_dynamic_cast_SimpleCppDerived", [](const SimpleBase *base_ptr) {
54 return dynamic_cast<const SimpleCppDerived *>(base_ptr) != nullptr;
55 });
56}
57
58} // namespace exercise_trampoline
59

Callers 1

TEST_SUBMODULEFunction · 0.70

Calls 7

pickleFunction · 0.85
getattrFunction · 0.85
dictClass · 0.85
make_tupleFunction · 0.85
moveFunction · 0.85
attrMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected