test_modules.cpp
Go to the documentation of this file.
1 /*
2  tests/test_modules.cpp -- nested modules, importing modules, and
3  internal references
4 
5  Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
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 "constructor_stats.h"
12 #include "pybind11_tests.h"
13 
14 TEST_SUBMODULE(modules, m) {
15  // test_nested_modules
16  // This is intentionally "py::module" to verify it still can be used in place of "py::module_"
17  py::module m_sub = m.def_submodule("subsubmodule");
18  m_sub.def("submodule_func", []() { return "submodule_func()"; });
19 
20  // test_reference_internal
21  class A {
22  public:
23  explicit A(int v) : v(v) { print_created(this, v); }
24  ~A() { print_destroyed(this); }
25  A(const A &) { print_copy_created(this); }
26  A &operator=(const A &copy) {
27  print_copy_assigned(this);
28  v = copy.v;
29  return *this;
30  }
31  std::string toString() const { return "A[" + std::to_string(v) + "]"; }
32 
33  private:
34  int v;
35  };
36  py::class_<A>(m_sub, "A").def(py::init<int>()).def("__repr__", &A::toString);
37 
38  class B {
39  public:
40  B() { print_default_created(this); }
41  ~B() { print_destroyed(this); }
42  B(const B &) { print_copy_created(this); }
43  B &operator=(const B &copy) {
44  print_copy_assigned(this);
45  a1 = copy.a1;
46  a2 = copy.a2;
47  return *this;
48  }
49  A &get_a1() { return a1; }
50  A &get_a2() { return a2; }
51 
52  A a1{1};
53  A a2{2};
54  };
55  py::class_<B>(m_sub, "B")
56  .def(py::init<>())
57  .def("get_a1",
58  &B::get_a1,
59  "Return the internal A 1",
60  py::return_value_policy::reference_internal)
61  .def("get_a2",
62  &B::get_a2,
63  "Return the internal A 2",
64  py::return_value_policy::reference_internal)
65  .def_readwrite("a1", &B::a1) // def_readonly uses an internal
66  // reference return policy by default
67  .def_readwrite("a2", &B::a2);
68 
69  // This is intentionally "py::module" to verify it still can be used in place of "py::module_"
70  m.attr("OD") = py::module::import("collections").attr("OrderedDict");
71 
72  // test_duplicate_registration
73  // Registering two things with the same name
74  m.def("duplicate_registration", []() {
75  class Dupe1 {};
76  class Dupe2 {};
77  class Dupe3 {};
78  class DupeException {};
79 
80  // Go ahead and leak, until we have a non-leaking py::module_ constructor
81  auto dm
82  = py::module_::create_extension_module("dummy", nullptr, new py::module_::module_def);
83  auto failures = py::list();
84 
85  py::class_<Dupe1>(dm, "Dupe1");
86  py::class_<Dupe2>(dm, "Dupe2");
87  dm.def("dupe1_factory", []() { return Dupe1(); });
88  py::exception<DupeException>(dm, "DupeException");
89 
90  try {
91  py::class_<Dupe1>(dm, "Dupe1");
92  failures.append("Dupe1 class");
93  } catch (std::runtime_error &) {
94  }
95  try {
96  dm.def("Dupe1", []() { return Dupe1(); });
97  failures.append("Dupe1 function");
98  } catch (std::runtime_error &) {
99  }
100  try {
101  py::class_<Dupe3>(dm, "dupe1_factory");
102  failures.append("dupe1_factory");
103  } catch (std::runtime_error &) {
104  }
105  try {
106  py::exception<Dupe3>(dm, "Dupe2");
107  failures.append("Dupe2");
108  } catch (std::runtime_error &) {
109  }
110  try {
111  dm.def("DupeException", []() { return 30; });
112  failures.append("DupeException1");
113  } catch (std::runtime_error &) {
114  }
115  try {
116  py::class_<DupeException>(dm, "DupeException");
117  failures.append("DupeException2");
118  } catch (std::runtime_error &) {
119  }
120 
121  return failures;
122  });
123 
124  m.def("def_submodule", [](py::module_ m, const char *name) { return m.def_submodule(name); });
125 }
name
Annotation for function names.
Definition: attr.h:51
B
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:49
E1::B
@ B
constructor_stats.h
A
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
print_copy_created
void print_copy_created(T *inst, Values &&...values)
Definition: constructor_stats.h:282
print_default_created
void print_default_created(T *inst, Values &&...values)
Definition: constructor_stats.h:304
align_3::a1
Point2 a1
Definition: testPose2.cpp:769
module
module_ module
Definition: pybind11.h:1367
return_value_policy::copy
@ copy
print_copy_assigned
void print_copy_assigned(T *inst, Values &&...values)
Definition: constructor_stats.h:294
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
E1::A
@ A
pybind11_tests.h
align_3::a2
Point2 a2
Definition: testPose2.cpp:770
print_destroyed
void print_destroyed(T *inst, Values &&...values)
Definition: constructor_stats.h:314
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
TEST_SUBMODULE
TEST_SUBMODULE(modules, m)
Definition: test_modules.cpp:14
print_created
void print_created(T *inst, Values &&...values)
Definition: constructor_stats.h:309


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:39:27