pybind11_cross_module_tests.cpp
Go to the documentation of this file.
1 /*
2  tests/pybind11_cross_module_tests.cpp -- contains tests that require multiple modules
3 
4  Copyright (c) 2017 Jason Rhinelander <jason@imaginary.ca>
5 
6  All rights reserved. Use of this source code is governed by a
7  BSD-style license that can be found in the LICENSE file.
8 */
9 
10 #include <pybind11/stl_bind.h>
11 
12 #include "local_bindings.h"
13 #include "pybind11_tests.h"
14 #include "test_exceptions.h"
15 
16 #include <numeric>
17 #include <utility>
18 
19 PYBIND11_MODULE(pybind11_cross_module_tests, m) {
20  m.doc() = "pybind11 cross-module test module";
21 
22  // test_local_bindings.py tests:
23  //
24  // Definitions here are tested by importing both this module and the
25  // relevant pybind11_tests submodule from a test_whatever.py
26 
27  // test_load_external
28  bind_local<ExternalType1>(m, "ExternalType1", py::module_local());
29  bind_local<ExternalType2>(m, "ExternalType2", py::module_local());
30 
31  // test_exceptions.py
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();
36  });
37  m.def("raise_value_error", []() {
38  PyErr_SetString(PyExc_ValueError, "My value error");
39  throw py::error_already_set();
40  });
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"); });
45  m.def("throw_local_simple_error", []() { throw LocalSimpleException("external mod"); });
46  py::register_exception_translator([](std::exception_ptr p) {
47  try {
48  if (p) {
49  std::rethrow_exception(p);
50  }
51  } catch (const shared_exception &e) {
52  PyErr_SetString(PyExc_KeyError, e.what());
53  }
54  });
55 
56  // translate the local exception into a key error but only in this module
57  py::register_local_exception_translator([](std::exception_ptr p) {
58  try {
59  if (p) {
60  std::rethrow_exception(p);
61  }
62  } catch (const LocalException &e) {
63  PyErr_SetString(PyExc_KeyError, e.what());
64  }
65  });
66 
67  // test_local_bindings.py
68  // Local to both:
69  bind_local<LocalType, 1>(m, "LocalType", py::module_local()).def("get2", [](LocalType &t) {
70  return t.i + 2;
71  });
72 
73  // Can only be called with our python type:
74  m.def("local_value", [](LocalType &l) { return l.i; });
75 
76  // test_nonlocal_failure
77  // This registration will fail (global registration when LocalFail is already registered
78  // globally in the main test module):
79  m.def("register_nonlocal", [m]() { bind_local<NonLocalType, 0>(m, "NonLocalType"); });
80 
81  // test_stl_bind_local
82  // stl_bind.h binders defaults to py::module_local if the types are local or converting:
83  py::bind_vector<LocalVec>(m, "LocalVec");
84  py::bind_map<LocalMap>(m, "LocalMap");
85 
86  // test_stl_bind_global
87  // and global if the type (or one of the types, for the map) is global (so these will fail,
88  // assuming pybind11_tests is already loaded):
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"); });
91  // The default can, however, be overridden to global using `py::module_local()` or
92  // `py::module_local(false)`.
93  // Explicitly made local:
94  py::bind_vector<NonLocalVec2>(m, "NonLocalVec2", py::module_local());
95  // Explicitly made global (and so will fail to bind):
96  m.def("register_nonlocal_map2",
97  [m]() { py::bind_map<NonLocalMap2>(m, "NonLocalMap2", py::module_local(false)); });
98 
99  // test_mixed_local_global
100  // We try this both with the global type registered first and vice versa (the order shouldn't
101  // matter).
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));
106  });
107  m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); });
108  m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); });
109 
110  // test_internal_locals_differ
111  m.def("local_cpp_types_addr",
113 
114  // test_stl_caster_vs_stl_bind
115  py::bind_vector<std::vector<int>>(m, "VectorInt");
116 
117  m.def("load_vector_via_binding",
118  [](std::vector<int> &v) { return std::accumulate(v.begin(), v.end(), 0); });
119 
120  // test_cross_module_calls
121  m.def("return_self", [](LocalVec *v) { return v; });
122  m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); });
123 
124  class Dog : public pets::Pet {
125  public:
126  explicit Dog(std::string name) : Pet(std::move(name)) {}
127  };
128  py::class_<pets::Pet>(m, "Pet", py::module_local()).def("name", &pets::Pet::name);
129  // Binding for local extending class:
130  py::class_<Dog, pets::Pet>(m, "Dog").def(py::init<std::string>());
131  m.def("pet_name", [](pets::Pet &p) { return p.name(); });
132 
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; });
135 
136  py::class_<MixGL2>(m, "MixGL2", py::module_local()).def(py::init<int>());
137 
138  // test_vector_bool
139  // We can't test both stl.h and stl_bind.h conversions of `std::vector<bool>` within
140  // the same module (it would be an ODR violation). Therefore `bind_vector` of `bool`
141  // is defined here and tested in `test_stl_binders.py`.
142  py::bind_vector<std::vector<bool>>(m, "VectorBool");
143 
144  // test_missing_header_message
145  // The main module already includes stl.h, but we need to test the error message
146  // which appears when this header is missing.
147  m.def("missing_header_arg", [](const std::vector<float> &) {});
148  m.def("missing_header_return", []() { return std::vector<float>(); });
149 }
pets::Pet::name
const std::string & name() const
Definition: local_bindings.h:81
name
Annotation for function names.
Definition: attr.h:51
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
test_exceptions.h
register_exception_translator
void register_exception_translator(ExceptionTranslator &&translator)
Definition: pybind11.h:2512
MixedLocalGlobal
LocalBase< 4 > MixedLocalGlobal
Mixed: registered local first, then global.
Definition: local_bindings.h:23
local_internals::registered_types_cpp
type_map< type_info * > registered_types_cpp
Definition: internals.h:545
l
static const Line3 l(Rot3(), 1, 1)
shared_exception
Definition: test_exceptions.h:8
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
Dog
Definition: test_tagbased_polymorphic.cpp:41
pets::Pet
Definition: local_bindings.h:77
register_local_exception_translator
void register_local_exception_translator(ExceptionTranslator &&translator)
Definition: pybind11.h:2523
MixedGlobalLocal
LocalBase< 5 > MixedGlobalLocal
Mixed: global first, then local.
Definition: local_bindings.h:25
LocalBase
Simple class used to test py::local:
Definition: local_bindings.h:8
MixGL::i
int i
Definition: local_bindings.h:86
LocalVec
std::vector< LocalType > LocalVec
Definition: local_bindings.h:31
LocalException
Definition: local_bindings.h:40
Dog::Dog
Dog(const std::string &_name, Kind _kind=Kind::Dog)
Definition: test_tagbased_polymorphic.cpp:42
pybind11_tests.h
p
float * p
Definition: Tutorial_Map_using.cpp:9
get_local_internals
local_internals & get_local_internals()
Works like get_internals, but for things which are locally registered.
Definition: internals.h:584
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
PYBIND11_MODULE
PYBIND11_MODULE(pybind11_cross_module_tests, m)
Definition: pybind11_cross_module_tests.cpp:19
MixGL
Definition: local_bindings.h:85
align_3::t
Point2 t(10, 10)
stl_bind.h
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
LocalSimpleException
Definition: local_bindings.h:50
uintptr_t
_W64 unsigned int uintptr_t
Definition: ms_stdint.h:124
local_bindings.h


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:02:24