Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 1 | /* |
| 2 | tests/test_local_bindings.cpp -- tests the py::module_local class feature which makes a class |
| 3 | binding local to the module in which it is defined. |
| 4 | |
| 5 | Copyright (c) 2017 Jason Rhinelander <jason@imaginary.ca> |
| 6 | |
| 7 | All rights reserved. Use of this source code is governed by a |
| 8 | BSD-style license that can be found in the LICENSE file. |
| 9 | */ |
| 10 | |
| 11 | #include "pybind11_tests.h" |
| 12 | #include "local_bindings.h" |
Dean Moldovan | 8d3cedb | 2017-08-13 03:03:06 +0200 | [diff] [blame] | 13 | #include <pybind11/stl.h> |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 14 | #include <pybind11/stl_bind.h> |
Dean Moldovan | 8d3cedb | 2017-08-13 03:03:06 +0200 | [diff] [blame] | 15 | #include <numeric> |
| 16 | |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 17 | TEST_SUBMODULE(local_bindings, m) { |
Dean Moldovan | 7b1de1e | 2017-09-03 01:31:47 +0200 | [diff] [blame^] | 18 | // test_load_external |
| 19 | m.def("load_external1", [](ExternalType1 &e) { return e.i; }); |
| 20 | m.def("load_external2", [](ExternalType2 &e) { return e.i; }); |
| 21 | |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 22 | // test_local_bindings |
| 23 | // Register a class with py::module_local: |
| 24 | bind_local<LocalType, -1>(m, "LocalType", py::module_local()) |
| 25 | .def("get3", [](LocalType &t) { return t.i + 3; }) |
| 26 | ; |
| 27 | |
| 28 | m.def("local_value", [](LocalType &l) { return l.i; }); |
| 29 | |
| 30 | // test_nonlocal_failure |
| 31 | // The main pybind11 test module is loaded first, so this registration will succeed (the second |
| 32 | // one, in pybind11_cross_module_tests.cpp, is designed to fail): |
| 33 | bind_local<NonLocalType, 0>(m, "NonLocalType") |
| 34 | .def(py::init<int>()) |
| 35 | .def("get", [](LocalType &i) { return i.i; }) |
| 36 | ; |
| 37 | |
| 38 | // test_duplicate_local |
| 39 | // py::module_local declarations should be visible across compilation units that get linked together; |
| 40 | // this tries to register a duplicate local. It depends on a definition in test_class.cpp and |
| 41 | // should raise a runtime error from the duplicate definition attempt. If test_class isn't |
| 42 | // available it *also* throws a runtime error (with "test_class not enabled" as value). |
| 43 | m.def("register_local_external", [m]() { |
| 44 | auto main = py::module::import("pybind11_tests"); |
| 45 | if (py::hasattr(main, "class_")) { |
| 46 | bind_local<LocalExternal, 7>(m, "LocalExternal", py::module_local()); |
| 47 | } |
| 48 | else throw std::runtime_error("test_class not enabled"); |
| 49 | }); |
| 50 | |
| 51 | // test_stl_bind_local |
| 52 | // stl_bind.h binders defaults to py::module_local if the types are local or converting: |
Dean Moldovan | 8d3cedb | 2017-08-13 03:03:06 +0200 | [diff] [blame] | 53 | py::bind_vector<LocalVec>(m, "LocalVec"); |
| 54 | py::bind_map<LocalMap>(m, "LocalMap"); |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 55 | // and global if the type (or one of the types, for the map) is global: |
Dean Moldovan | 8d3cedb | 2017-08-13 03:03:06 +0200 | [diff] [blame] | 56 | py::bind_vector<NonLocalVec>(m, "NonLocalVec"); |
| 57 | py::bind_map<NonLocalMap>(m, "NonLocalMap"); |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 58 | |
| 59 | // test_stl_bind_global |
| 60 | // They can, however, be overridden to global using `py::module_local(false)`: |
| 61 | bind_local<NonLocal2, 10>(m, "NonLocal2"); |
Dean Moldovan | 8d3cedb | 2017-08-13 03:03:06 +0200 | [diff] [blame] | 62 | py::bind_vector<LocalVec2>(m, "LocalVec2", py::module_local()); |
| 63 | py::bind_map<NonLocalMap2>(m, "NonLocalMap2", py::module_local(false)); |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 64 | |
Jason Rhinelander | 4b15923 | 2017-08-04 13:05:12 -0400 | [diff] [blame] | 65 | // test_mixed_local_global |
| 66 | // We try this both with the global type registered first and vice versa (the order shouldn't |
| 67 | // matter). |
| 68 | m.def("register_mixed_global", [m]() { |
| 69 | bind_local<MixedGlobalLocal, 100>(m, "MixedGlobalLocal", py::module_local(false)); |
| 70 | }); |
| 71 | m.def("register_mixed_local", [m]() { |
| 72 | bind_local<MixedLocalGlobal, 1000>(m, "MixedLocalGlobal", py::module_local()); |
| 73 | }); |
| 74 | m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); }); |
| 75 | m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); }); |
| 76 | |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 77 | // test_internal_locals_differ |
| 78 | m.def("local_cpp_types_addr", []() { return (uintptr_t) &py::detail::registered_local_types_cpp(); }); |
Dean Moldovan | 8d3cedb | 2017-08-13 03:03:06 +0200 | [diff] [blame] | 79 | |
| 80 | // test_stl_caster_vs_stl_bind |
| 81 | m.def("load_vector_via_caster", [](std::vector<int> v) { |
| 82 | return std::accumulate(v.begin(), v.end(), 0); |
| 83 | }); |
Jason Rhinelander | 5e14aa6 | 2017-08-17 11:38:05 -0400 | [diff] [blame] | 84 | |
| 85 | // test_cross_module_calls |
| 86 | m.def("return_self", [](LocalVec *v) { return v; }); |
| 87 | m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); }); |
| 88 | |
| 89 | class Cat : public pets::Pet { public: Cat(std::string name) : Pet(name) {}; }; |
| 90 | py::class_<pets::Pet>(m, "Pet", py::module_local()) |
| 91 | .def("get_name", &pets::Pet::name); |
| 92 | // Binding for local extending class: |
| 93 | py::class_<Cat, pets::Pet>(m, "Cat") |
| 94 | .def(py::init<std::string>()); |
| 95 | m.def("pet_name", [](pets::Pet &p) { return p.name(); }); |
| 96 | |
| 97 | py::class_<MixGL>(m, "MixGL").def(py::init<int>()); |
| 98 | m.def("get_gl_value", [](MixGL &o) { return o.i + 10; }); |
| 99 | |
| 100 | py::class_<MixGL2>(m, "MixGL2").def(py::init<int>()); |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 101 | } |