pybind11_tests.cpp
Go to the documentation of this file.
1 /*
2  tests/pybind11_tests.cpp -- pybind example plugin
3 
4  Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
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_tests.h"
11 
12 #include "constructor_stats.h"
13 
14 #include <functional>
15 #include <list>
16 
17 /*
18 For testing purposes, we define a static global variable here in a function that each individual
19 test .cpp calls with its initialization lambda. It's convenient here because we can just not
20 compile some test files to disable/ignore some of the test code.
21 
22 It is NOT recommended as a way to use pybind11 in practice, however: the initialization order will
23 be essentially random, which is okay for our test scripts (there are no dependencies between the
24 individual pybind11 test .cpp files), but most likely not what you want when using pybind11
25 productively.
26 
27 Instead, see the "How can I reduce the build time?" question in the "Frequently asked questions"
28 section of the documentation for good practice on splitting binding code over multiple files.
29 */
30 std::list<std::function<void(py::module_ &)>> &initializers() {
31  static std::list<std::function<void(py::module_ &)>> inits;
32  return inits;
33 }
34 
36 
37 test_initializer::test_initializer(const char *submodule_name, Initializer init) {
38  initializers().emplace_back([=](py::module_ &parent) {
39  auto m = parent.def_submodule(submodule_name);
40  init(m);
41  });
42 }
43 
44 void bind_ConstructorStats(py::module_ &m) {
45  py::class_<ConstructorStats>(m, "ConstructorStats")
46  .def("alive", &ConstructorStats::alive)
47  .def("values", &ConstructorStats::values)
48  .def_readwrite("default_constructions", &ConstructorStats::default_constructions)
49  .def_readwrite("copy_assignments", &ConstructorStats::copy_assignments)
50  .def_readwrite("move_assignments", &ConstructorStats::move_assignments)
51  .def_readwrite("copy_constructions", &ConstructorStats::copy_constructions)
52  .def_readwrite("move_constructions", &ConstructorStats::move_constructions)
53  .def_static("get",
54  (ConstructorStats & (*) (py::object)) & ConstructorStats::get,
55  py::return_value_policy::reference_internal)
56 
57  // Not exactly ConstructorStats, but related: expose the internal pybind number of
58  // registered instances to allow instance cleanup checks (invokes a GC first)
59  .def_static("detail_reg_inst", []() {
62  });
63 }
64 
65 const char *cpp_std() {
66  return
67 #if defined(PYBIND11_CPP20)
68  "C++20";
69 #elif defined(PYBIND11_CPP17)
70  "C++17";
71 #elif defined(PYBIND11_CPP14)
72  "C++14";
73 #else
74  "C++11";
75 #endif
76 }
77 
78 PYBIND11_MODULE(pybind11_tests, m, py::mod_gil_not_used()) {
79  m.doc() = "pybind11 test module";
80 
81  // Intentionally kept minimal to not create a maintenance chore
82  // ("just enough" to be conclusive).
83 #if defined(__VERSION__)
84  m.attr("compiler_info") = __VERSION__;
85 #elif defined(_MSC_FULL_VER)
86  m.attr("compiler_info") = "MSVC " PYBIND11_TOSTRING(_MSC_FULL_VER);
87 #else
88  m.attr("compiler_info") = py::none();
89 #endif
90  m.attr("cpp_std") = cpp_std();
91  m.attr("PYBIND11_INTERNALS_ID") = PYBIND11_INTERNALS_ID;
92  // Free threaded Python uses UINT32_MAX for immortal objects.
93  m.attr("PYBIND11_REFCNT_IMMORTAL") = UINT32_MAX;
94  m.attr("PYBIND11_SIMPLE_GIL_MANAGEMENT") =
95 #if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
96  true;
97 #else
98  false;
99 #endif
100  m.attr("PYBIND11_NUMPY_1_ONLY") =
101 #if defined(PYBIND11_NUMPY_1_ONLY)
102  true;
103 #else
104  false;
105 #endif
106 
108 
109 #if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
110  m.attr("detailed_error_messages_enabled") = true;
111 #else
112  m.attr("detailed_error_messages_enabled") = false;
113 #endif
114 
115  py::class_<UserType>(m, "UserType", "A `py::class_` type for testing")
116  .def(py::init<>())
117  .def(py::init<int>())
118  .def("get_value", &UserType::value, "Get value using a method")
119  .def("set_value", &UserType::set, "Set value using a method")
120  .def_property("value", &UserType::value, &UserType::set, "Get/set value using a property")
121  .def("__repr__", [](const UserType &u) { return "UserType({})"_s.format(u.value()); });
122 
123  py::class_<IncType, UserType>(m, "IncType")
124  .def(py::init<>())
125  .def(py::init<int>())
126  .def("__repr__", [](const IncType &u) { return "IncType({})"_s.format(u.value()); });
127 
128  for (const auto &initializer : initializers()) {
129  initializer(m);
130  }
131 }
ConstructorStats::alive
int alive()
Definition: constructor_stats.h:131
PYBIND11_MODULE
PYBIND11_MODULE(pybind11_tests, m, py::mod_gil_not_used())
Definition: pybind11_tests.cpp:78
UserType::set
void set(int set)
Definition: pybind11_tests.h:34
initializers
std::list< std::function< void(py::module_ &)> > & initializers()
Definition: pybind11_tests.cpp:30
ConstructorStats::values
py::list values()
Definition: constructor_stats.h:153
cpp_std
const char * cpp_std()
Definition: pybind11_tests.cpp:65
ConstructorStats::default_constructions
int default_constructions
Definition: constructor_stats.h:82
constructor_stats.h
num_registered_instances
size_t num_registered_instances()
Definition: internals.h:664
ConstructorStats::get
static ConstructorStats & get()
Definition: constructor_stats.h:170
ConstructorStats::copy_assignments
int copy_assignments
Definition: constructor_stats.h:85
PYBIND11_INTERNALS_ID
#define PYBIND11_INTERNALS_ID
Definition: internals.h:309
test_initializer::test_initializer
test_initializer(Initializer init)
Definition: pybind11_tests.cpp:35
PYBIND11_TOSTRING
#define PYBIND11_TOSTRING(x)
Definition: wrap/pybind11/include/pybind11/detail/common.h:386
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
test_initializer::Initializer
void(*)(py::module_ &) Initializer
Definition: pybind11_tests.h:12
init
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Definition: pybind11.h:2006
pybind11_tests
Definition: test_methods_and_attributes.cpp:180
ConstructorStats::copy_constructions
int copy_constructions
Definition: constructor_stats.h:83
ConstructorStats::move_assignments
int move_assignments
Definition: constructor_stats.h:86
UserType::value
int value() const
Definition: pybind11_tests.h:33
pybind11_tests.h
UINT32_MAX
#define UINT32_MAX
Definition: ms_stdint.h:147
bind_ConstructorStats
void bind_ConstructorStats(py::module_ &m)
Definition: pybind11_tests.cpp:44
ConstructorStats::move_constructions
int move_constructions
Definition: constructor_stats.h:84
ConstructorStats::gc
static void gc()
Definition: constructor_stats.h:113
init
Definition: TutorialInplaceLU.cpp:2


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:34:49