Scarab  2.8.1
Project 8 C++ Utility Library
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 #include "constructor_stats.h"
12 
13 #include <functional>
14 #include <list>
15 
16 /*
17 For testing purposes, we define a static global variable here in a function that each individual
18 test .cpp calls with its initialization lambda. It's convenient here because we can just not
19 compile some test files to disable/ignore some of the test code.
20 
21 It is NOT recommended as a way to use pybind11 in practice, however: the initialization order will
22 be essentially random, which is okay for our test scripts (there are no dependencies between the
23 individual pybind11 test .cpp files), but most likely not what you want when using pybind11
24 productively.
25 
26 Instead, see the "How can I reduce the build time?" question in the "Frequently asked questions"
27 section of the documentation for good practice on splitting binding code over multiple files.
28 */
29 std::list<std::function<void(py::module &)>> &initializers() {
30  static std::list<std::function<void(py::module &)>> inits;
31  return inits;
32 }
33 
35  initializers().push_back(init);
36 }
37 
38 test_initializer::test_initializer(const char *submodule_name, Initializer init) {
39  initializers().push_back([=](py::module &parent) {
40  auto m = parent.def_submodule(submodule_name);
41  init(m);
42  });
43 }
44 
46  py::class_<ConstructorStats>(m, "ConstructorStats")
47  .def("alive", &ConstructorStats::alive)
48  .def("values", &ConstructorStats::values)
49  .def_readwrite("default_constructions", &ConstructorStats::default_constructions)
54  .def_static("get", (ConstructorStats &(*)(py::object)) &ConstructorStats::get, py::return_value_policy::reference_internal)
55 
56  // Not exactly ConstructorStats, but related: expose the internal pybind number of registered instances
57  // to allow instance cleanup checks (invokes a GC first)
58  .def_static("detail_reg_inst", []() {
60  return py::detail::get_internals().registered_instances.size();
61  })
62  ;
63 }
64 
65 PYBIND11_MODULE(pybind11_tests, m) {
66  m.doc() = "pybind11 test module";
67 
69 
70 #if !defined(NDEBUG)
71  m.attr("debug_enabled") = true;
72 #else
73  m.attr("debug_enabled") = false;
74 #endif
75 
76  py::class_<UserType>(m, "UserType", "A `py::class_` type for testing")
77  .def(py::init<>())
78  .def(py::init<int>())
79  .def("get_value", &UserType::value, "Get value using a method")
80  .def("set_value", &UserType::set, "Set value using a method")
81  .def_property("value", &UserType::value, &UserType::set, "Get/set value using a property")
82  .def("__repr__", [](const UserType& u) { return "UserType({})"_s.format(u.value()); });
83 
84  py::class_<IncType, UserType>(m, "IncType")
85  .def(py::init<>())
86  .def(py::init<int>())
87  .def("__repr__", [](const IncType& u) { return "IncType({})"_s.format(u.value()); });
88 
89  for (const auto &initializer : initializers())
90  initializer(m);
91 
92  if (!py::hasattr(m, "have_eigen")) m.attr("have_eigen") = false;
93 }
std::list< std::function< void(py::module &)> > & initializers()
static ConstructorStats & get()
int value() const
PYBIND11_MODULE(pybind11_tests, m)
Wrapper for Python extension modules.
Definition: pybind11.h:789
obj_attr_accessor attr(handle key) const
Definition: pytypes.h:1410
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Definition: pybind11.h:1371
test_initializer(Initializer init)
void(*)(py::module &) Initializer
class_ & def_static(const char *name_, Func &&f, const Extra &... extra)
Definition: pybind11.h:1118
void bind_ConstructorStats(py::module &m)
void set(int set)
class_ & def_property(const char *name, const Getter &fget, const Setter &fset, const Extra &...extra)
Uses return_value_policy::reference_internal by default.
Definition: pybind11.h:1244
bool typename Extra class_ & def(const char *name_, Func &&f, const Extra &... extra)
Definition: pybind11.h:1110
bool hasattr(handle obj, handle name)
Definition: pytypes.h:387
class_ & def_readwrite(const char *name, D C::*pm, const Extra &... extra)
Definition: pybind11.h:1186