blob: fdb67a1a695ce53e09bdf4f351927e7a5a08a2ef [file] [log] [blame]
Jason Rhinelander7437c692017-07-28 22:03:44 -04001/*
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 Moldovan8d3cedb2017-08-13 03:03:06 +020013#include <pybind11/stl.h>
Jason Rhinelander7437c692017-07-28 22:03:44 -040014#include <pybind11/stl_bind.h>
Dean Moldovan8d3cedb2017-08-13 03:03:06 +020015#include <numeric>
16
17PYBIND11_MAKE_OPAQUE(LocalVec);
18PYBIND11_MAKE_OPAQUE(LocalVec2);
19PYBIND11_MAKE_OPAQUE(LocalMap);
20PYBIND11_MAKE_OPAQUE(NonLocalVec);
21PYBIND11_MAKE_OPAQUE(NonLocalMap);
22PYBIND11_MAKE_OPAQUE(NonLocalMap2);
Jason Rhinelander7437c692017-07-28 22:03:44 -040023
24TEST_SUBMODULE(local_bindings, m) {
Jason Rhinelander7437c692017-07-28 22:03:44 -040025 // test_local_bindings
26 // Register a class with py::module_local:
27 bind_local<LocalType, -1>(m, "LocalType", py::module_local())
28 .def("get3", [](LocalType &t) { return t.i + 3; })
29 ;
30
31 m.def("local_value", [](LocalType &l) { return l.i; });
32
33 // test_nonlocal_failure
34 // The main pybind11 test module is loaded first, so this registration will succeed (the second
35 // one, in pybind11_cross_module_tests.cpp, is designed to fail):
36 bind_local<NonLocalType, 0>(m, "NonLocalType")
37 .def(py::init<int>())
38 .def("get", [](LocalType &i) { return i.i; })
39 ;
40
41 // test_duplicate_local
42 // py::module_local declarations should be visible across compilation units that get linked together;
43 // this tries to register a duplicate local. It depends on a definition in test_class.cpp and
44 // should raise a runtime error from the duplicate definition attempt. If test_class isn't
45 // available it *also* throws a runtime error (with "test_class not enabled" as value).
46 m.def("register_local_external", [m]() {
47 auto main = py::module::import("pybind11_tests");
48 if (py::hasattr(main, "class_")) {
49 bind_local<LocalExternal, 7>(m, "LocalExternal", py::module_local());
50 }
51 else throw std::runtime_error("test_class not enabled");
52 });
53
54 // test_stl_bind_local
55 // stl_bind.h binders defaults to py::module_local if the types are local or converting:
Dean Moldovan8d3cedb2017-08-13 03:03:06 +020056 py::bind_vector<LocalVec>(m, "LocalVec");
57 py::bind_map<LocalMap>(m, "LocalMap");
Jason Rhinelander7437c692017-07-28 22:03:44 -040058 // and global if the type (or one of the types, for the map) is global:
Dean Moldovan8d3cedb2017-08-13 03:03:06 +020059 py::bind_vector<NonLocalVec>(m, "NonLocalVec");
60 py::bind_map<NonLocalMap>(m, "NonLocalMap");
Jason Rhinelander7437c692017-07-28 22:03:44 -040061
62 // test_stl_bind_global
63 // They can, however, be overridden to global using `py::module_local(false)`:
64 bind_local<NonLocal2, 10>(m, "NonLocal2");
Dean Moldovan8d3cedb2017-08-13 03:03:06 +020065 py::bind_vector<LocalVec2>(m, "LocalVec2", py::module_local());
66 py::bind_map<NonLocalMap2>(m, "NonLocalMap2", py::module_local(false));
Jason Rhinelander7437c692017-07-28 22:03:44 -040067
Jason Rhinelander4b159232017-08-04 13:05:12 -040068 // test_mixed_local_global
69 // We try this both with the global type registered first and vice versa (the order shouldn't
70 // matter).
71 m.def("register_mixed_global", [m]() {
72 bind_local<MixedGlobalLocal, 100>(m, "MixedGlobalLocal", py::module_local(false));
73 });
74 m.def("register_mixed_local", [m]() {
75 bind_local<MixedLocalGlobal, 1000>(m, "MixedLocalGlobal", py::module_local());
76 });
77 m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); });
78 m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); });
79
Jason Rhinelander7437c692017-07-28 22:03:44 -040080 // test_internal_locals_differ
81 m.def("local_cpp_types_addr", []() { return (uintptr_t) &py::detail::registered_local_types_cpp(); });
Dean Moldovan8d3cedb2017-08-13 03:03:06 +020082
83 // test_stl_caster_vs_stl_bind
84 m.def("load_vector_via_caster", [](std::vector<int> v) {
85 return std::accumulate(v.begin(), v.end(), 0);
86 });
Jason Rhinelander7437c692017-07-28 22:03:44 -040087}