File indexing completed on 2025-01-18 10:17:51
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <pybind11/stl_bind.h>
0011
0012 #include "local_bindings.h"
0013 #include "pybind11_tests.h"
0014 #include "test_exceptions.h"
0015
0016 #include <numeric>
0017 #include <utility>
0018
0019 PYBIND11_MODULE(pybind11_cross_module_tests, m) {
0020 m.doc() = "pybind11 cross-module test module";
0021
0022
0023
0024
0025
0026
0027
0028 bind_local<ExternalType1>(m, "ExternalType1", py::module_local());
0029 bind_local<ExternalType2>(m, "ExternalType2", py::module_local());
0030
0031
0032 py::register_local_exception<LocalSimpleException>(m, "LocalSimpleException");
0033 m.def("raise_runtime_error", []() {
0034 PyErr_SetString(PyExc_RuntimeError, "My runtime error");
0035 throw py::error_already_set();
0036 });
0037 m.def("raise_value_error", []() {
0038 PyErr_SetString(PyExc_ValueError, "My value error");
0039 throw py::error_already_set();
0040 });
0041 m.def("throw_pybind_value_error", []() { throw py::value_error("pybind11 value error"); });
0042 m.def("throw_pybind_type_error", []() { throw py::type_error("pybind11 type error"); });
0043 m.def("throw_stop_iteration", []() { throw py::stop_iteration(); });
0044 m.def("throw_local_error", []() { throw LocalException("just local"); });
0045 m.def("throw_local_simple_error", []() { throw LocalSimpleException("external mod"); });
0046 py::register_exception_translator([](std::exception_ptr p) {
0047 try {
0048 if (p) {
0049 std::rethrow_exception(p);
0050 }
0051 } catch (const shared_exception &e) {
0052 PyErr_SetString(PyExc_KeyError, e.what());
0053 }
0054 });
0055
0056
0057 py::register_local_exception_translator([](std::exception_ptr p) {
0058 try {
0059 if (p) {
0060 std::rethrow_exception(p);
0061 }
0062 } catch (const LocalException &e) {
0063 PyErr_SetString(PyExc_KeyError, e.what());
0064 }
0065 });
0066
0067
0068
0069 bind_local<LocalType, 1>(m, "LocalType", py::module_local()).def("get2", [](LocalType &t) {
0070 return t.i + 2;
0071 });
0072
0073
0074 m.def("local_value", [](LocalType &l) { return l.i; });
0075
0076
0077
0078
0079 m.def("register_nonlocal", [m]() { bind_local<NonLocalType, 0>(m, "NonLocalType"); });
0080
0081
0082
0083 py::bind_vector<LocalVec>(m, "LocalVec");
0084 py::bind_map<LocalMap>(m, "LocalMap");
0085
0086
0087
0088
0089 m.def("register_nonlocal_vec", [m]() { py::bind_vector<NonLocalVec>(m, "NonLocalVec"); });
0090 m.def("register_nonlocal_map", [m]() { py::bind_map<NonLocalMap>(m, "NonLocalMap"); });
0091
0092
0093
0094 py::bind_vector<NonLocalVec2>(m, "NonLocalVec2", py::module_local());
0095
0096 m.def("register_nonlocal_map2",
0097 [m]() { py::bind_map<NonLocalMap2>(m, "NonLocalMap2", py::module_local(false)); });
0098
0099
0100
0101
0102 m.def("register_mixed_global_local",
0103 [m]() { bind_local<MixedGlobalLocal, 200>(m, "MixedGlobalLocal", py::module_local()); });
0104 m.def("register_mixed_local_global", [m]() {
0105 bind_local<MixedLocalGlobal, 2000>(m, "MixedLocalGlobal", py::module_local(false));
0106 });
0107 m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); });
0108 m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); });
0109
0110
0111 m.def("local_cpp_types_addr",
0112 []() { return (uintptr_t) &py::detail::get_local_internals().registered_types_cpp; });
0113
0114
0115 py::bind_vector<std::vector<int>>(m, "VectorInt");
0116
0117 m.def("load_vector_via_binding",
0118 [](std::vector<int> &v) { return std::accumulate(v.begin(), v.end(), 0); });
0119
0120
0121 m.def("return_self", [](LocalVec *v) { return v; });
0122 m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); });
0123
0124 class Dog : public pets::Pet {
0125 public:
0126 explicit Dog(std::string name) : Pet(std::move(name)) {}
0127 };
0128 py::class_<pets::Pet>(m, "Pet", py::module_local()).def("name", &pets::Pet::name);
0129
0130 py::class_<Dog, pets::Pet>(m, "Dog").def(py::init<std::string>());
0131 m.def("pet_name", [](pets::Pet &p) { return p.name(); });
0132
0133 py::class_<MixGL>(m, "MixGL", py::module_local()).def(py::init<int>());
0134 m.def("get_gl_value", [](MixGL &o) { return o.i + 100; });
0135
0136 py::class_<MixGL2>(m, "MixGL2", py::module_local()).def(py::init<int>());
0137
0138
0139
0140
0141
0142 py::bind_vector<std::vector<bool>>(m, "VectorBool");
0143
0144
0145
0146
0147 m.def("missing_header_arg", [](const std::vector<float> &) {});
0148 m.def("missing_header_return", []() { return std::vector<float>(); });
0149 }