|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <pybind11/stl_bind.h> |
|
|
|
#include "local_bindings.h" |
|
#include "pybind11_tests.h" |
|
#include "test_exceptions.h" |
|
|
|
#include <numeric> |
|
#include <utility> |
|
|
|
PYBIND11_MODULE(pybind11_cross_module_tests, m) { |
|
m.doc() = "pybind11 cross-module test module"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bind_local<ExternalType1>(m, "ExternalType1", py::module_local()); |
|
bind_local<ExternalType2>(m, "ExternalType2", py::module_local()); |
|
|
|
|
|
py::register_local_exception<LocalSimpleException>(m, "LocalSimpleException"); |
|
m.def("raise_runtime_error", []() { |
|
py::set_error(PyExc_RuntimeError, "My runtime error"); |
|
throw py::error_already_set(); |
|
}); |
|
m.def("raise_value_error", []() { |
|
py::set_error(PyExc_ValueError, "My value error"); |
|
throw py::error_already_set(); |
|
}); |
|
m.def("throw_pybind_value_error", []() { throw py::value_error("pybind11 value error"); }); |
|
m.def("throw_pybind_type_error", []() { throw py::type_error("pybind11 type error"); }); |
|
m.def("throw_stop_iteration", []() { throw py::stop_iteration(); }); |
|
m.def("throw_local_error", []() { throw LocalException("just local"); }); |
|
m.def("throw_local_simple_error", []() { throw LocalSimpleException("external mod"); }); |
|
py::register_exception_translator([](std::exception_ptr p) { |
|
try { |
|
if (p) { |
|
std::rethrow_exception(p); |
|
} |
|
} catch (const shared_exception &e) { |
|
py::set_error(PyExc_KeyError, e.what()); |
|
} |
|
}); |
|
|
|
|
|
py::register_local_exception_translator([](std::exception_ptr p) { |
|
try { |
|
if (p) { |
|
std::rethrow_exception(p); |
|
} |
|
} catch (const LocalException &e) { |
|
py::set_error(PyExc_KeyError, e.what()); |
|
} |
|
}); |
|
|
|
|
|
|
|
bind_local<LocalType, 1>(m, "LocalType", py::module_local()).def("get2", [](LocalType &t) { |
|
return t.i + 2; |
|
}); |
|
|
|
|
|
m.def("local_value", [](LocalType &l) { return l.i; }); |
|
|
|
|
|
|
|
|
|
m.def("register_nonlocal", [m]() { bind_local<NonLocalType, 0>(m, "NonLocalType"); }); |
|
|
|
|
|
|
|
py::bind_vector<LocalVec>(m, "LocalVec"); |
|
py::bind_map<LocalMap>(m, "LocalMap"); |
|
|
|
|
|
|
|
|
|
m.def("register_nonlocal_vec", [m]() { py::bind_vector<NonLocalVec>(m, "NonLocalVec"); }); |
|
m.def("register_nonlocal_map", [m]() { py::bind_map<NonLocalMap>(m, "NonLocalMap"); }); |
|
|
|
|
|
|
|
py::bind_vector<NonLocalVec2>(m, "NonLocalVec2", py::module_local()); |
|
|
|
m.def("register_nonlocal_map2", |
|
[m]() { py::bind_map<NonLocalMap2>(m, "NonLocalMap2", py::module_local(false)); }); |
|
|
|
|
|
|
|
|
|
m.def("register_mixed_global_local", |
|
[m]() { bind_local<MixedGlobalLocal, 200>(m, "MixedGlobalLocal", py::module_local()); }); |
|
m.def("register_mixed_local_global", [m]() { |
|
bind_local<MixedLocalGlobal, 2000>(m, "MixedLocalGlobal", py::module_local(false)); |
|
}); |
|
m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); }); |
|
m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); }); |
|
|
|
|
|
m.def("local_cpp_types_addr", |
|
[]() { return (uintptr_t) &py::detail::get_local_internals().registered_types_cpp; }); |
|
|
|
|
|
py::bind_vector<std::vector<int>>(m, "VectorInt"); |
|
|
|
m.def("load_vector_via_binding", |
|
[](std::vector<int> &v) { return std::accumulate(v.begin(), v.end(), 0); }); |
|
|
|
|
|
m.def("return_self", [](LocalVec *v) { return v; }); |
|
m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); }); |
|
|
|
class Dog : public pets::Pet { |
|
public: |
|
explicit Dog(std::string name) : Pet(std::move(name)) {} |
|
}; |
|
py::class_<pets::Pet>(m, "Pet", py::module_local()).def("name", &pets::Pet::name); |
|
|
|
py::class_<Dog, pets::Pet>(m, "Dog").def(py::init<std::string>()); |
|
m.def("pet_name", [](pets::Pet &p) { return p.name(); }); |
|
|
|
py::class_<MixGL>(m, "MixGL", py::module_local()).def(py::init<int>()); |
|
m.def("get_gl_value", [](MixGL &o) { return o.i + 100; }); |
|
|
|
py::class_<MixGL2>(m, "MixGL2", py::module_local()).def(py::init<int>()); |
|
|
|
|
|
|
|
|
|
|
|
py::bind_vector<std::vector<bool>>(m, "VectorBool"); |
|
|
|
|
|
|
|
|
|
m.def("missing_header_arg", [](const std::vector<float> &) {}); |
|
m.def("missing_header_return", []() { return std::vector<float>(); }); |
|
} |
|
|