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" |
| 13 | #include <pybind11/stl_bind.h> |
| 14 | |
| 15 | TEST_SUBMODULE(local_bindings, m) { |
| 16 | |
| 17 | // test_local_bindings |
| 18 | // Register a class with py::module_local: |
| 19 | bind_local<LocalType, -1>(m, "LocalType", py::module_local()) |
| 20 | .def("get3", [](LocalType &t) { return t.i + 3; }) |
| 21 | ; |
| 22 | |
| 23 | m.def("local_value", [](LocalType &l) { return l.i; }); |
| 24 | |
| 25 | // test_nonlocal_failure |
| 26 | // The main pybind11 test module is loaded first, so this registration will succeed (the second |
| 27 | // one, in pybind11_cross_module_tests.cpp, is designed to fail): |
| 28 | bind_local<NonLocalType, 0>(m, "NonLocalType") |
| 29 | .def(py::init<int>()) |
| 30 | .def("get", [](LocalType &i) { return i.i; }) |
| 31 | ; |
| 32 | |
| 33 | // test_duplicate_local |
| 34 | // py::module_local declarations should be visible across compilation units that get linked together; |
| 35 | // this tries to register a duplicate local. It depends on a definition in test_class.cpp and |
| 36 | // should raise a runtime error from the duplicate definition attempt. If test_class isn't |
| 37 | // available it *also* throws a runtime error (with "test_class not enabled" as value). |
| 38 | m.def("register_local_external", [m]() { |
| 39 | auto main = py::module::import("pybind11_tests"); |
| 40 | if (py::hasattr(main, "class_")) { |
| 41 | bind_local<LocalExternal, 7>(m, "LocalExternal", py::module_local()); |
| 42 | } |
| 43 | else throw std::runtime_error("test_class not enabled"); |
| 44 | }); |
| 45 | |
| 46 | // test_stl_bind_local |
| 47 | // stl_bind.h binders defaults to py::module_local if the types are local or converting: |
| 48 | py::bind_vector<std::vector<LocalType>>(m, "LocalVec"); |
| 49 | py::bind_map<std::unordered_map<std::string, LocalType>>(m, "LocalMap"); |
| 50 | // and global if the type (or one of the types, for the map) is global: |
| 51 | py::bind_vector<std::vector<NonLocalType>>(m, "NonLocalVec"); |
| 52 | py::bind_map<std::unordered_map<std::string, NonLocalType>>(m, "NonLocalMap"); |
| 53 | |
| 54 | // test_stl_bind_global |
| 55 | // They can, however, be overridden to global using `py::module_local(false)`: |
| 56 | bind_local<NonLocal2, 10>(m, "NonLocal2"); |
| 57 | py::bind_vector<std::vector<NonLocal2>>(m, "LocalVec2", py::module_local()); |
| 58 | py::bind_map<std::unordered_map<std::string, uint8_t>>(m, "NonLocalMap2", py::module_local(false)); |
| 59 | |
| 60 | // test_internal_locals_differ |
| 61 | m.def("local_cpp_types_addr", []() { return (uintptr_t) &py::detail::registered_local_types_cpp(); }); |
| 62 | } |