diff options
author | Jason Lowe-Power <jason@lowepower.com> | 2017-11-17 17:02:05 -0800 |
---|---|---|
committer | Jason Lowe-Power <jason@lowepower.com> | 2017-12-14 00:27:59 +0000 |
commit | f07d5069d86e31ecf195664850f79fb00c445bd3 (patch) | |
tree | f54ac06896fa828f873d199a0e9b25bd94911c79 /ext/pybind11/tests/pybind11_tests.cpp | |
parent | 3f64b374c49491f18dc2ca538ed8c8597e4aac83 (diff) | |
download | gem5-f07d5069d86e31ecf195664850f79fb00c445bd3.tar.xz |
ext: Upgrade PyBind11 to version 2.2.1
This upgrade is necessary for pybind to build with GCC 7.2.
We still need to add the patch for stl.h. MSC_FULL_VER change is no longer
needed.
See https://gem5-review.googlesource.com/c/public/gem5/+/2230
Change-Id: I806729217d022070583994c2dfcaa74476aef30f
Signed-off-by: Jason Lowe-Power <jason@lowepower.com>
Reviewed-on: https://gem5-review.googlesource.com/5801
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
Maintainer: Andreas Sandberg <andreas.sandberg@arm.com>
Diffstat (limited to 'ext/pybind11/tests/pybind11_tests.cpp')
-rw-r--r-- | ext/pybind11/tests/pybind11_tests.cpp | 49 |
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(); } |