aboutsummaryrefslogtreecommitdiff
path: root/tests/test_local_bindings.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_local_bindings.cpp')
-rw-r--r--tests/test_local_bindings.cpp47
1 files changed, 26 insertions, 21 deletions
diff --git a/tests/test_local_bindings.cpp b/tests/test_local_bindings.cpp
index c61e3888..13736774 100644
--- a/tests/test_local_bindings.cpp
+++ b/tests/test_local_bindings.cpp
@@ -8,11 +8,14 @@
BSD-style license that can be found in the LICENSE file.
*/
-#include "pybind11_tests.h"
-#include "local_bindings.h"
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
+
+#include "local_bindings.h"
+#include "pybind11_tests.h"
+
#include <numeric>
+#include <utility>
TEST_SUBMODULE(local_bindings, m) {
// test_load_external
@@ -21,9 +24,9 @@ TEST_SUBMODULE(local_bindings, m) {
// test_local_bindings
// Register a class with py::module_local:
- bind_local<LocalType, -1>(m, "LocalType", py::module_local())
- .def("get3", [](LocalType &t) { return t.i + 3; })
- ;
+ bind_local<LocalType, -1>(m, "LocalType", py::module_local()).def("get3", [](LocalType &t) {
+ return t.i + 3;
+ });
m.def("local_value", [](LocalType &l) { return l.i; });
@@ -32,20 +35,21 @@ TEST_SUBMODULE(local_bindings, m) {
// one, in pybind11_cross_module_tests.cpp, is designed to fail):
bind_local<NonLocalType, 0>(m, "NonLocalType")
.def(py::init<int>())
- .def("get", [](LocalType &i) { return i.i; })
- ;
+ .def("get", [](LocalType &i) { return i.i; });
// test_duplicate_local
- // py::module_local declarations should be visible across compilation units that get linked together;
- // this tries to register a duplicate local. It depends on a definition in test_class.cpp and
- // should raise a runtime error from the duplicate definition attempt. If test_class isn't
- // available it *also* throws a runtime error (with "test_class not enabled" as value).
+ // py::module_local declarations should be visible across compilation units that get linked
+ // together; this tries to register a duplicate local. It depends on a definition in
+ // test_class.cpp and should raise a runtime error from the duplicate definition attempt. If
+ // test_class isn't available it *also* throws a runtime error (with "test_class not enabled"
+ // as value).
m.def("register_local_external", [m]() {
auto main = py::module_::import("pybind11_tests");
if (py::hasattr(main, "class_")) {
bind_local<LocalExternal, 7>(m, "LocalExternal", py::module_local());
+ } else {
+ throw std::runtime_error("test_class not enabled");
}
- else throw std::runtime_error("test_class not enabled");
});
// test_stl_bind_local
@@ -75,23 +79,24 @@ TEST_SUBMODULE(local_bindings, m) {
m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); });
// test_internal_locals_differ
- m.def("local_cpp_types_addr", []() { return (uintptr_t) &py::detail::registered_local_types_cpp(); });
+ m.def("local_cpp_types_addr",
+ []() { return (uintptr_t) &py::detail::get_local_internals().registered_types_cpp; });
// test_stl_caster_vs_stl_bind
- m.def("load_vector_via_caster", [](std::vector<int> v) {
- return std::accumulate(v.begin(), v.end(), 0);
- });
+ m.def("load_vector_via_caster",
+ [](std::vector<int> v) { return std::accumulate(v.begin(), v.end(), 0); });
// test_cross_module_calls
m.def("return_self", [](LocalVec *v) { return v; });
m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); });
- class Cat : public pets::Pet { public: Cat(std::string name) : Pet(name) {}; };
- py::class_<pets::Pet>(m, "Pet", py::module_local())
- .def("get_name", &pets::Pet::name);
+ class Cat : public pets::Pet {
+ public:
+ explicit Cat(std::string name) : Pet(std::move(name)) {}
+ };
+ py::class_<pets::Pet>(m, "Pet", py::module_local()).def("get_name", &pets::Pet::name);
// Binding for local extending class:
- py::class_<Cat, pets::Pet>(m, "Cat")
- .def(py::init<std::string>());
+ py::class_<Cat, pets::Pet>(m, "Cat").def(py::init<std::string>());
m.def("pet_name", [](pets::Pet &p) { return p.name(); });
py::class_<MixGL>(m, "MixGL").def(py::init<int>());