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 
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(_MSC_FULL_VER)
84  m.attr("compiler_info") = "MSVC " PYBIND11_TOSTRING(_MSC_FULL_VER);
85 #elif defined(__VERSION__)
86  m.attr("compiler_info") = __VERSION__;
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  m.attr("PYBIND11_SIMPLE_GIL_MANAGEMENT") =
93 #if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
94  true;
95 #else
96  false;
97 #endif
98 
100 
101 #if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
102  m.attr("detailed_error_messages_enabled") = true;
103 #else
104  m.attr("detailed_error_messages_enabled") = false;
105 #endif
106 
107  py::class_<UserType>(m, "UserType", "A `py::class_` type for testing")
108  .def(py::init<>())
109  .def(py::init<int>())
110  .def("get_value", &UserType::value, "Get value using a method")
111  .def("set_value", &UserType::set, "Set value using a method")
112  .def_property("value", &UserType::value, &UserType::set, "Get/set value using a property")
113  .def("__repr__", [](const UserType &u) { return "UserType({})"_s.format(u.value()); });
114 
115  py::class_<IncType, UserType>(m, "IncType")
116  .def(py::init<>())
117  .def(py::init<int>())
118  .def("__repr__", [](const IncType &u) { return "IncType({})"_s.format(u.value()); });
119 
120  for (const auto &initializer : initializers()) {
121  initializer(m);
122  }
123 }
ConstructorStats::alive
int alive()
Definition: constructor_stats.h:131
UserType::set
void set(int set)
Definition: pybind11_tests.h:32
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
get_internals
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
Definition: internals.h:467
ConstructorStats::default_constructions
int default_constructions
Definition: constructor_stats.h:82
constructor_stats.h
internals::registered_instances
std::unordered_multimap< const void *, instance * > registered_instances
Definition: internals.h:173
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:310
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:375
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
test_initializer::Initializer
void(*)(py::module_ &) Initializer
Definition: pybind11_tests.h:10
init
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Definition: pybind11.h:1912
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:31
pybind11_tests.h
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
PYBIND11_MODULE
PYBIND11_MODULE(pybind11_tests, m)
Definition: pybind11_tests.cpp:78


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