test_local_bindings.cpp
Go to the documentation of this file.
1 /*
2  tests/test_local_bindings.cpp -- tests the py::module_local class feature which makes a class
3  binding local to the module in which it is defined.
4 
5  Copyright (c) 2017 Jason Rhinelander <jason@imaginary.ca>
6 
7  All rights reserved. Use of this source code is governed by a
8  BSD-style license that can be found in the LICENSE file.
9 */
10 
11 #include <pybind11/stl.h>
12 #include <pybind11/stl_bind.h>
13 
14 #include "local_bindings.h"
15 #include "pybind11_tests.h"
16 
17 #include <numeric>
18 #include <utility>
19 
20 TEST_SUBMODULE(local_bindings, m) {
21  // test_load_external
22  m.def("load_external1", [](ExternalType1 &e) { return e.i; });
23  m.def("load_external2", [](ExternalType2 &e) { return e.i; });
24 
25  // test_local_bindings
26  // Register a class with py::module_local:
27  bind_local<LocalType, -1>(m, "LocalType", py::module_local()).def("get3", [](LocalType &t) {
28  return t.i + 3;
29  });
30 
31  m.def("local_value", [](LocalType &l) { return l.i; });
32 
33  // test_nonlocal_failure
34  // The main pybind11 test module is loaded first, so this registration will succeed (the second
35  // one, in pybind11_cross_module_tests.cpp, is designed to fail):
36  bind_local<NonLocalType, 0>(m, "NonLocalType")
37  .def(py::init<int>())
38  .def("get", [](LocalType &i) { return i.i; });
39 
40  // test_duplicate_local
41  // py::module_local declarations should be visible across compilation units that get linked
42  // together; this tries to register a duplicate local. It depends on a definition in
43  // test_class.cpp and should raise a runtime error from the duplicate definition attempt. If
44  // test_class isn't available it *also* throws a runtime error (with "test_class not enabled"
45  // as value).
46  m.def("register_local_external", [m]() {
47  auto main = py::module_::import("pybind11_tests");
48  if (py::hasattr(main, "class_")) {
49  bind_local<LocalExternal, 7>(m, "LocalExternal", py::module_local());
50  } else {
51  throw std::runtime_error("test_class not enabled");
52  }
53  });
54 
55  // test_stl_bind_local
56  // stl_bind.h binders defaults to py::module_local if the types are local or converting:
57  py::bind_vector<LocalVec>(m, "LocalVec");
58  py::bind_map<LocalMap>(m, "LocalMap");
59  // and global if the type (or one of the types, for the map) is global:
60  py::bind_vector<NonLocalVec>(m, "NonLocalVec");
61  py::bind_map<NonLocalMap>(m, "NonLocalMap");
62 
63  // test_stl_bind_global
64  // They can, however, be overridden to global using `py::module_local(false)`:
65  bind_local<NonLocal2, 10>(m, "NonLocal2");
66  py::bind_vector<LocalVec2>(m, "LocalVec2", py::module_local());
67  py::bind_map<NonLocalMap2>(m, "NonLocalMap2", py::module_local(false));
68 
69  // test_mixed_local_global
70  // We try this both with the global type registered first and vice versa (the order shouldn't
71  // matter).
72  m.def("register_mixed_global", [m]() {
73  bind_local<MixedGlobalLocal, 100>(m, "MixedGlobalLocal", py::module_local(false));
74  });
75  m.def("register_mixed_local", [m]() {
76  bind_local<MixedLocalGlobal, 1000>(m, "MixedLocalGlobal", py::module_local());
77  });
78  m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); });
79  m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); });
80 
81  // test_internal_locals_differ
82  m.def("local_cpp_types_addr",
84 
85  // test_stl_caster_vs_stl_bind
86  m.def("load_vector_via_caster",
87  [](std::vector<int> v) { return std::accumulate(v.begin(), v.end(), 0); });
88 
89  // test_cross_module_calls
90  m.def("return_self", [](LocalVec *v) { return v; });
91  m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); });
92 
93  class Cat : public pets::Pet {
94  public:
95  explicit Cat(std::string name) : Pet(std::move(name)) {}
96  };
97  py::class_<pets::Pet>(m, "Pet", py::module_local()).def("get_name", &pets::Pet::name);
98  // Binding for local extending class:
99  py::class_<Cat, pets::Pet>(m, "Cat").def(py::init<std::string>());
100  m.def("pet_name", [](pets::Pet &p) { return p.name(); });
101 
102  py::class_<MixGL>(m, "MixGL").def(py::init<int>());
103  m.def("get_gl_value", [](MixGL &o) { return o.i + 10; });
104 
105  py::class_<MixGL2>(m, "MixGL2").def(py::init<int>());
106 }
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.)
stl.h
hasattr
bool hasattr(handle obj, handle name)
Definition: pytypes.h:853
LocalType
LocalBase< 0 > LocalType
Registered with py::module_local in both main and secondary modules:
Definition: local_bindings.h:15
TEST_SUBMODULE
TEST_SUBMODULE(local_bindings, m)
Definition: test_local_bindings.cpp:20
main
int main(int argc, char **argv)
Definition: cmake/example_cmake_find_gtsam/main.cpp:63
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)
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
pets::Pet
Definition: local_bindings.h:77
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
Cat
Definition: test_tagbased_polymorphic.cpp:60
LocalVec
std::vector< LocalType > LocalVec
Definition: local_bindings.h:31
bind_local
py::class_< T > bind_local(Args &&...args)
Definition: local_bindings.h:69
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
Cat::Cat
Cat(const std::string &_name, Kind _kind=Kind::Cat)
Definition: test_tagbased_polymorphic.cpp:61
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
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
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:05:28