Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:54

0001 /*
0002     tests/test_local_bindings.cpp -- tests the py::module_local class feature which makes a class
0003                                      binding local to the module in which it is defined.
0004 
0005     Copyright (c) 2017 Jason Rhinelander <jason@imaginary.ca>
0006 
0007     All rights reserved. Use of this source code is governed by a
0008     BSD-style license that can be found in the LICENSE file.
0009 */
0010 
0011 #include <pybind11/stl.h>
0012 #include <pybind11/stl_bind.h>
0013 
0014 #include "local_bindings.h"
0015 #include "pybind11_tests.h"
0016 
0017 #include <numeric>
0018 #include <utility>
0019 
0020 TEST_SUBMODULE(local_bindings, m) {
0021     // test_load_external
0022     m.def("load_external1", [](ExternalType1 &e) { return e.i; });
0023     m.def("load_external2", [](ExternalType2 &e) { return e.i; });
0024 
0025     // test_local_bindings
0026     // Register a class with py::module_local:
0027     bind_local<LocalType, -1>(m, "LocalType", py::module_local()).def("get3", [](LocalType &t) {
0028         return t.i + 3;
0029     });
0030 
0031     m.def("local_value", [](LocalType &l) { return l.i; });
0032 
0033     // test_nonlocal_failure
0034     // The main pybind11 test module is loaded first, so this registration will succeed (the second
0035     // one, in pybind11_cross_module_tests.cpp, is designed to fail):
0036     bind_local<NonLocalType, 0>(m, "NonLocalType")
0037         .def(py::init<int>())
0038         .def("get", [](LocalType &i) { return i.i; });
0039 
0040     // test_duplicate_local
0041     // py::module_local declarations should be visible across compilation units that get linked
0042     // together; this tries to register a duplicate local.  It depends on a definition in
0043     // test_class.cpp and should raise a runtime error from the duplicate definition attempt.  If
0044     // test_class isn't available it *also* throws a runtime error (with "test_class not enabled"
0045     // as value).
0046     m.def("register_local_external", [m]() {
0047         auto main = py::module_::import("pybind11_tests");
0048         if (py::hasattr(main, "class_")) {
0049             bind_local<LocalExternal, 7>(m, "LocalExternal", py::module_local());
0050         } else {
0051             throw std::runtime_error("test_class not enabled");
0052         }
0053     });
0054 
0055     // test_stl_bind_local
0056     // stl_bind.h binders defaults to py::module_local if the types are local or converting:
0057     py::bind_vector<LocalVec>(m, "LocalVec");
0058     py::bind_map<LocalMap>(m, "LocalMap");
0059     // and global if the type (or one of the types, for the map) is global:
0060     py::bind_vector<NonLocalVec>(m, "NonLocalVec");
0061     py::bind_map<NonLocalMap>(m, "NonLocalMap");
0062 
0063     // test_stl_bind_global
0064     // They can, however, be overridden to global using `py::module_local(false)`:
0065     bind_local<NonLocal2, 10>(m, "NonLocal2");
0066     py::bind_vector<LocalVec2>(m, "LocalVec2", py::module_local());
0067     py::bind_map<NonLocalMap2>(m, "NonLocalMap2", py::module_local(false));
0068 
0069     // test_mixed_local_global
0070     // We try this both with the global type registered first and vice versa (the order shouldn't
0071     // matter).
0072     m.def("register_mixed_global", [m]() {
0073         bind_local<MixedGlobalLocal, 100>(m, "MixedGlobalLocal", py::module_local(false));
0074     });
0075     m.def("register_mixed_local", [m]() {
0076         bind_local<MixedLocalGlobal, 1000>(m, "MixedLocalGlobal", py::module_local());
0077     });
0078     m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); });
0079     m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); });
0080 
0081     // test_internal_locals_differ
0082     m.def("local_cpp_types_addr",
0083           []() { return (uintptr_t) &py::detail::get_local_internals().registered_types_cpp; });
0084 
0085     // test_stl_caster_vs_stl_bind
0086     m.def("load_vector_via_caster",
0087           [](std::vector<int> v) { return std::accumulate(v.begin(), v.end(), 0); });
0088 
0089     // test_cross_module_calls
0090     m.def("return_self", [](LocalVec *v) { return v; });
0091     m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); });
0092 
0093     class Cat : public pets::Pet {
0094     public:
0095         explicit Cat(std::string name) : Pet(std::move(name)) {}
0096     };
0097     py::class_<pets::Pet>(m, "Pet", py::module_local()).def("get_name", &pets::Pet::name);
0098     // Binding for local extending class:
0099     py::class_<Cat, pets::Pet>(m, "Cat").def(py::init<std::string>());
0100     m.def("pet_name", [](pets::Pet &p) { return p.name(); });
0101 
0102     py::class_<MixGL>(m, "MixGL").def(py::init<int>());
0103     m.def("get_gl_value", [](MixGL &o) { return o.i + 10; });
0104 
0105     py::class_<MixGL2>(m, "MixGL2").def(py::init<int>());
0106 }