summaryrefslogtreecommitdiff
path: root/ext/pybind11/tests/pybind11_tests.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ext/pybind11/tests/pybind11_tests.cpp')
-rw-r--r--ext/pybind11/tests/pybind11_tests.cpp49
1 files changed, 42 insertions, 7 deletions
diff --git a/ext/pybind11/tests/pybind11_tests.cpp b/ext/pybind11/tests/pybind11_tests.cpp
index 1d805d75b..bc7d2c3e7 100644
--- a/ext/pybind11/tests/pybind11_tests.cpp
+++ b/ext/pybind11/tests/pybind11_tests.cpp
@@ -10,6 +10,9 @@
#include "pybind11_tests.h"
#include "constructor_stats.h"
+#include <functional>
+#include <list>
+
/*
For testing purposes, we define a static global variable here in a function that each individual
test .cpp calls with its initialization lambda. It's convenient here because we can just not
@@ -28,8 +31,15 @@ std::list<std::function<void(py::module &)>> &initializers() {
return inits;
}
-test_initializer::test_initializer(std::function<void(py::module &)> initializer) {
- initializers().push_back(std::move(initializer));
+test_initializer::test_initializer(Initializer init) {
+ initializers().push_back(init);
+}
+
+test_initializer::test_initializer(const char *submodule_name, Initializer init) {
+ initializers().push_back([=](py::module &parent) {
+ auto m = parent.def_submodule(submodule_name);
+ init(m);
+ });
}
void bind_ConstructorStats(py::module &m) {
@@ -41,18 +51,43 @@ void bind_ConstructorStats(py::module &m) {
.def_readwrite("move_assignments", &ConstructorStats::move_assignments)
.def_readwrite("copy_constructions", &ConstructorStats::copy_constructions)
.def_readwrite("move_constructions", &ConstructorStats::move_constructions)
- .def_static("get", (ConstructorStats &(*)(py::object)) &ConstructorStats::get, py::return_value_policy::reference_internal);
+ .def_static("get", (ConstructorStats &(*)(py::object)) &ConstructorStats::get, py::return_value_policy::reference_internal)
+
+ // Not exactly ConstructorStats, but related: expose the internal pybind number of registered instances
+ // to allow instance cleanup checks (invokes a GC first)
+ .def_static("detail_reg_inst", []() {
+ ConstructorStats::gc();
+ return py::detail::get_internals().registered_instances.size();
+ })
+ ;
}
-PYBIND11_PLUGIN(pybind11_tests) {
- py::module m("pybind11_tests", "pybind testing plugin");
+PYBIND11_MODULE(pybind11_tests, m) {
+ m.doc() = "pybind11 test module";
bind_ConstructorStats(m);
+#if !defined(NDEBUG)
+ m.attr("debug_enabled") = true;
+#else
+ m.attr("debug_enabled") = false;
+#endif
+
+ py::class_<UserType>(m, "UserType", "A `py::class_` type for testing")
+ .def(py::init<>())
+ .def(py::init<int>())
+ .def("get_value", &UserType::value, "Get value using a method")
+ .def("set_value", &UserType::set, "Set value using a method")
+ .def_property("value", &UserType::value, &UserType::set, "Get/set value using a property")
+ .def("__repr__", [](const UserType& u) { return "UserType({})"_s.format(u.value()); });
+
+ py::class_<IncType, UserType>(m, "IncType")
+ .def(py::init<>())
+ .def(py::init<int>())
+ .def("__repr__", [](const IncType& u) { return "IncType({})"_s.format(u.value()); });
+
for (const auto &initializer : initializers())
initializer(m);
if (!py::hasattr(m, "have_eigen")) m.attr("have_eigen") = false;
-
- return m.ptr();
}