μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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/*
18For testing purposes, we define a static global variable here in a function that each individual
19test .cpp calls with its initialization lambda. It's convenient here because we can just not
20compile some test files to disable/ignore some of the test code.
21
22It is NOT recommended as a way to use pybind11 in practice, however: the initialization order will
23be essentially random, which is okay for our test scripts (there are no dependencies between the
24individual pybind11 test .cpp files), but most likely not what you want when using pybind11
25productively.
26
27Instead, see the "How can I reduce the build time?" question in the "Frequently asked questions"
28section of the documentation for good practice on splitting binding code over multiple files.
29*/
30std::list<std::function<void(py::module_ &)>> &initializers() {
31 static std::list<std::function<void(py::module_ &)>> inits;
32 return inits;
33}
34
36
38 initializers().emplace_back([=](py::module_ &parent) {
39 auto m = parent.def_submodule(submodule_name);
40 init(m);
41 });
42}
43
44void 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", []() {
61 return py::detail::get_internals().registered_instances.size();
62 });
63}
64
65PYBIND11_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}
static ConstructorStats & get()
Like UserType, but increments value on copy for quick reference vs. copy tests.
A user-defined type which is exported and can be used by any test.
void set(int set)
int value() const
void(*)(py::module_ &) Initializer
test_initializer(Initializer init)
#define PYBIND11_MODULE(name, variable)
\rst This macro creates the entry point that will be invoked when the Python interpreter imports an e...
Definition: common.h:440
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Definition: pybind11.h:1900
void bind_ConstructorStats(py::module_ &m)
std::list< std::function< void(py::module_ &)> > & initializers()