2017-07-29 02:03:44 +00:00
|
|
|
/*
|
|
|
|
tests/test_local_bindings.cpp -- tests the py::module_local class feature which makes a class
|
|
|
|
binding local to the module in which it is defined.
|
|
|
|
|
|
|
|
Copyright (c) 2017 Jason Rhinelander <jason@imaginary.ca>
|
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pybind11_tests.h"
|
|
|
|
#include "local_bindings.h"
|
2021-07-09 21:09:56 +00:00
|
|
|
|
2017-08-13 01:03:06 +00:00
|
|
|
#include <pybind11/stl.h>
|
2017-07-29 02:03:44 +00:00
|
|
|
#include <pybind11/stl_bind.h>
|
2021-07-09 21:09:56 +00:00
|
|
|
|
2017-08-13 01:03:06 +00:00
|
|
|
#include <numeric>
|
2021-07-09 21:09:56 +00:00
|
|
|
#include <utility>
|
2017-07-29 02:03:44 +00:00
|
|
|
|
2017-08-13 01:03:06 +00:00
|
|
|
TEST_SUBMODULE(local_bindings, m) {
|
2017-09-02 23:31:47 +00:00
|
|
|
// test_load_external
|
|
|
|
m.def("load_external1", [](ExternalType1 &e) { return e.i; });
|
|
|
|
m.def("load_external2", [](ExternalType2 &e) { return e.i; });
|
|
|
|
|
2017-07-29 02:03:44 +00:00
|
|
|
// 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; })
|
|
|
|
;
|
|
|
|
|
|
|
|
m.def("local_value", [](LocalType &l) { return l.i; });
|
|
|
|
|
|
|
|
// test_nonlocal_failure
|
|
|
|
// The main pybind11 test module is loaded first, so this registration will succeed (the second
|
|
|
|
// 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; })
|
|
|
|
;
|
|
|
|
|
|
|
|
// 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).
|
|
|
|
m.def("register_local_external", [m]() {
|
2020-10-03 17:38:03 +00:00
|
|
|
auto main = py::module_::import("pybind11_tests");
|
2017-07-29 02:03:44 +00:00
|
|
|
if (py::hasattr(main, "class_")) {
|
|
|
|
bind_local<LocalExternal, 7>(m, "LocalExternal", py::module_local());
|
|
|
|
}
|
|
|
|
else throw std::runtime_error("test_class not enabled");
|
|
|
|
});
|
|
|
|
|
|
|
|
// test_stl_bind_local
|
|
|
|
// stl_bind.h binders defaults to py::module_local if the types are local or converting:
|
2017-08-13 01:03:06 +00:00
|
|
|
py::bind_vector<LocalVec>(m, "LocalVec");
|
|
|
|
py::bind_map<LocalMap>(m, "LocalMap");
|
2017-07-29 02:03:44 +00:00
|
|
|
// and global if the type (or one of the types, for the map) is global:
|
2017-08-13 01:03:06 +00:00
|
|
|
py::bind_vector<NonLocalVec>(m, "NonLocalVec");
|
|
|
|
py::bind_map<NonLocalMap>(m, "NonLocalMap");
|
2017-07-29 02:03:44 +00:00
|
|
|
|
|
|
|
// test_stl_bind_global
|
|
|
|
// They can, however, be overridden to global using `py::module_local(false)`:
|
|
|
|
bind_local<NonLocal2, 10>(m, "NonLocal2");
|
2017-08-13 01:03:06 +00:00
|
|
|
py::bind_vector<LocalVec2>(m, "LocalVec2", py::module_local());
|
|
|
|
py::bind_map<NonLocalMap2>(m, "NonLocalMap2", py::module_local(false));
|
2017-07-29 02:03:44 +00:00
|
|
|
|
2017-08-04 17:05:12 +00:00
|
|
|
// test_mixed_local_global
|
|
|
|
// We try this both with the global type registered first and vice versa (the order shouldn't
|
|
|
|
// matter).
|
|
|
|
m.def("register_mixed_global", [m]() {
|
|
|
|
bind_local<MixedGlobalLocal, 100>(m, "MixedGlobalLocal", py::module_local(false));
|
|
|
|
});
|
|
|
|
m.def("register_mixed_local", [m]() {
|
|
|
|
bind_local<MixedLocalGlobal, 1000>(m, "MixedLocalGlobal", py::module_local());
|
|
|
|
});
|
|
|
|
m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); });
|
|
|
|
m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); });
|
|
|
|
|
2017-07-29 02:03:44 +00:00
|
|
|
// test_internal_locals_differ
|
2021-07-21 12:22:18 +00:00
|
|
|
m.def("local_cpp_types_addr", []() { return (uintptr_t) &py::detail::get_local_internals().registered_types_cpp; });
|
2017-08-13 01:03:06 +00:00
|
|
|
|
|
|
|
// test_stl_caster_vs_stl_bind
|
|
|
|
m.def("load_vector_via_caster", [](std::vector<int> v) {
|
|
|
|
return std::accumulate(v.begin(), v.end(), 0);
|
|
|
|
});
|
2017-08-17 15:38:05 +00:00
|
|
|
|
|
|
|
// test_cross_module_calls
|
|
|
|
m.def("return_self", [](LocalVec *v) { return v; });
|
|
|
|
m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); });
|
|
|
|
|
2021-07-09 21:09:56 +00:00
|
|
|
class Cat : public pets::Pet {
|
|
|
|
public:
|
CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250)
* Adding google-explicit-constructor to .clang-tidy
* clang-tidy explicit attr.h (all automatic)
* clang-tidy explicit cast.h (all automatic)
* clang-tidy detail/init.h (1 NOLINT)
* clang-tidy detail/type_caster_base.h (2 NOLINT)
* clang-tidy pybind11.h (7 NOLINT)
* clang-tidy detail/common.h (3 NOLINT)
* clang-tidy detail/descr.h (2 NOLINT)
* clang-tidy pytypes.h (23 NOLINT, only 1 explicit)
* clang-tidy eigen.h (7 NOLINT, 0 explicit)
* Adding 2 explicit in functional.h
* Adding 4 explicit in iostream.h
* clang-tidy numpy.h (1 NOLINT, 1 explicit)
* clang-tidy embed.h (0 NOLINT, 1 explicit)
* clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit)
* clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit)
* clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit)
* clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit)
* clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit)
* clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit)
* clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/object.h (0 NOLINT, 2 explicit)
* clang-tidy batch of fully automatic fixes.
* Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error.
2021-09-09 01:53:38 +00:00
|
|
|
explicit Cat(std::string name) : Pet(std::move(name)) {}
|
2021-07-09 21:09:56 +00:00
|
|
|
};
|
2017-08-17 15:38:05 +00:00
|
|
|
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>());
|
|
|
|
m.def("pet_name", [](pets::Pet &p) { return p.name(); });
|
|
|
|
|
|
|
|
py::class_<MixGL>(m, "MixGL").def(py::init<int>());
|
|
|
|
m.def("get_gl_value", [](MixGL &o) { return o.i + 10; });
|
|
|
|
|
|
|
|
py::class_<MixGL2>(m, "MixGL2").def(py::init<int>());
|
2017-07-29 02:03:44 +00:00
|
|
|
}
|