20 m.doc() =
"pybind11 cross-module test module";
28 bind_local<ExternalType1>(
m,
"ExternalType1", py::module_local());
29 bind_local<ExternalType2>(
m,
"ExternalType2", py::module_local());
32 py::register_local_exception<LocalSimpleException>(
m,
"LocalSimpleException");
33 m.def(
"raise_runtime_error", []() {
34 PyErr_SetString(PyExc_RuntimeError,
"My runtime error");
35 throw py::error_already_set();
37 m.def(
"raise_value_error", []() {
38 PyErr_SetString(PyExc_ValueError,
"My value error");
39 throw py::error_already_set();
41 m.def(
"throw_pybind_value_error", []() {
throw py::value_error(
"pybind11 value error"); });
42 m.def(
"throw_pybind_type_error", []() {
throw py::type_error(
"pybind11 type error"); });
43 m.def(
"throw_stop_iteration", []() {
throw py::stop_iteration(); });
44 m.def(
"throw_local_error", []() {
throw LocalException(
"just local"); });
49 std::rethrow_exception(p);
52 PyErr_SetString(PyExc_KeyError, e.what());
60 std::rethrow_exception(p);
63 PyErr_SetString(PyExc_KeyError, e.
what());
69 bind_local<LocalType, 1>(
m,
"LocalType", py::module_local()).def(
"get2", [](
LocalType &
t) {
74 m.def(
"local_value", [](
LocalType &
l) {
return l.
i; });
79 m.def(
"register_nonlocal", [m]() { bind_local<NonLocalType, 0>(
m,
"NonLocalType"); });
83 py::bind_vector<LocalVec>(
m,
"LocalVec");
84 py::bind_map<LocalMap>(
m,
"LocalMap");
89 m.def(
"register_nonlocal_vec", [m]() { py::bind_vector<NonLocalVec>(
m,
"NonLocalVec"); });
90 m.def(
"register_nonlocal_map", [m]() { py::bind_map<NonLocalMap>(
m,
"NonLocalMap"); });
94 py::bind_vector<NonLocalVec2>(
m,
"NonLocalVec2", py::module_local());
96 m.def(
"register_nonlocal_map2",
97 [m]() { py::bind_map<NonLocalMap2>(
m,
"NonLocalMap2", py::module_local(
false)); });
102 m.def(
"register_mixed_global_local",
103 [m]() { bind_local<MixedGlobalLocal, 200>(
m,
"MixedGlobalLocal", py::module_local()); });
104 m.def(
"register_mixed_local_global", [m]() {
105 bind_local<MixedLocalGlobal, 2000>(
m,
"MixedLocalGlobal", py::module_local(
false));
111 m.def(
"local_cpp_types_addr",
115 py::bind_vector<std::vector<int>>(
m,
"VectorInt");
117 m.def(
"load_vector_via_binding",
118 [](std::vector<int> &
v) {
return std::accumulate(v.begin(), v.end(), 0); });
121 m.def(
"return_self", [](
LocalVec *v) {
return v; });
126 explicit Dog(std::string
name) : Pet(std::move(name)) {}
128 py::class_<pets::Pet>(
m,
"Pet", py::module_local()).def(
"name", &
pets::Pet::name);
130 py::class_<Dog, pets::Pet>(
m,
"Dog").def(py::init<std::string>());
133 py::class_<MixGL>(
m,
"MixGL", py::module_local()).def(py::init<int>());
134 m.def(
"get_gl_value", [](
MixGL &o) {
return o.
i + 100; });
136 py::class_<MixGL2>(
m,
"MixGL2", py::module_local()).def(py::init<int>());
142 py::bind_vector<std::vector<bool>>(
m,
"VectorBool");
147 m.def(
"missing_header_arg", [](
const std::vector<float> &) {});
148 m.def(
"missing_header_return", []() {
return std::vector<float>(); });
std::vector< LocalType > LocalVec
const std::string & name() const
void register_exception_translator(ExceptionTranslator &&translator)
static const Line3 l(Rot3(), 1, 1)
const char * what() const noexcept override
Array< int, Dynamic, 1 > v
local_internals & get_local_internals()
Works like get_internals, but for things which are locally registered.
PYBIND11_MODULE(pybind11_cross_module_tests, m)
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Dog(const std::string &_name, Kind _kind=Kind::Dog)
_W64 unsigned int uintptr_t
LocalBase< 5 > MixedGlobalLocal
Mixed: global first, then local.
Annotation for function names.
Simple class used to test py::local:
void register_local_exception_translator(ExceptionTranslator &&translator)
LocalBase< 4 > MixedLocalGlobal
Mixed: registered local first, then global.