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) {
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 
94 
95 #if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
96  m.attr("detailed_error_messages_enabled") = true;
97 #else
98  m.attr("detailed_error_messages_enabled") = false;
99 #endif
100 
101  py::class_<UserType>(m, "UserType", "A `py::class_` type for testing")
102  .def(py::init<>())
103  .def(py::init<int>())
104  .def("get_value", &UserType::value, "Get value using a method")
105  .def("set_value", &UserType::set, "Set value using a method")
106  .def_property("value", &UserType::value, &UserType::set, "Get/set value using a property")
107  .def("__repr__", [](const UserType &u) { return "UserType({})"_s.format(u.value()); });
108 
109  py::class_<IncType, UserType>(m, "IncType")
110  .def(py::init<>())
111  .def(py::init<int>())
112  .def("__repr__", [](const IncType &u) { return "IncType({})"_s.format(u.value()); });
113 
114  for (const auto &initializer : initializers()) {
115  initializer(m);
116  }
117 }
Matrix3f m
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
Definition: internals.h:405
std::unordered_multimap< const void *, instance * > registered_instances
Definition: internals.h:155
PYBIND11_MODULE(pybind11_tests, m)
void set(int set)
test_initializer(Initializer init)
void(*)(py::module_ &) Initializer
static ConstructorStats & get()
std::list< std::function< void(py::module_ &)> > & initializers()
void bind_ConstructorStats(py::module_ &m)
int value() const
#define PYBIND11_INTERNALS_ID
Definition: internals.h:280
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Definition: pybind11.h:1882
const char * cpp_std()


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:35:26