blob: 359d6c61c91c14c4935ce839ab32c01487ea21de [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
Jason Rhinelander7437c692017-07-28 22:03:44 -040017TEST_SUBMODULE(local_bindings, m) {
Jason Rhinelander7437c692017-07-28 22:03:44 -040018 // test_local_bindings
19 // Register a class with py::module_local:
20 bind_local<LocalType, -1>(m, "LocalType", py::module_local())
21 .def("get3", [](LocalType &t) { return t.i + 3; })
22 ;
23
24 m.def("local_value", [](LocalType &l) { return l.i; });
25
26 // test_nonlocal_failure
27 // The main pybind11 test module is loaded first, so this registration will succeed (the second
28 // one, in pybind11_cross_module_tests.cpp, is designed to fail):
29 bind_local<NonLocalType, 0>(m, "NonLocalType")
30 .def(py::init<int>())
31 .def("get", [](LocalType &i) { return i.i; })
32 ;
33
34 // test_duplicate_local
35 // py::module_local declarations should be visible across compilation units that get linked together;
36 // this tries to register a duplicate local. It depends on a definition in test_class.cpp and
37 // should raise a runtime error from the duplicate definition attempt. If test_class isn't
38 // available it *also* throws a runtime error (with "test_class not enabled" as value).
39 m.def("register_local_external", [m]() {
40 auto main = py::module::import("pybind11_tests");
41 if (py::hasattr(main, "class_")) {
42 bind_local<LocalExternal, 7>(m, "LocalExternal", py::module_local());
43 }
44 else throw std::runtime_error("test_class not enabled");
45 });
46
47 // test_stl_bind_local
48 // stl_bind.h binders defaults to py::module_local if the types are local or converting:
Dean Moldovan8d3cedb2017-08-13 03:03:06 +020049 py::bind_vector<LocalVec>(m, "LocalVec");
50 py::bind_map<LocalMap>(m, "LocalMap");
Jason Rhinelander7437c692017-07-28 22:03:44 -040051 // and global if the type (or one of the types, for the map) is global:
Dean Moldovan8d3cedb2017-08-13 03:03:06 +020052 py::bind_vector<NonLocalVec>(m, "NonLocalVec");
53 py::bind_map<NonLocalMap>(m, "NonLocalMap");
Jason Rhinelander7437c692017-07-28 22:03:44 -040054
55 // test_stl_bind_global
56 // They can, however, be overridden to global using `py::module_local(false)`:
57 bind_local<NonLocal2, 10>(m, "NonLocal2");
Dean Moldovan8d3cedb2017-08-13 03:03:06 +020058 py::bind_vector<LocalVec2>(m, "LocalVec2", py::module_local());
59 py::bind_map<NonLocalMap2>(m, "NonLocalMap2", py::module_local(false));
Jason Rhinelander7437c692017-07-28 22:03:44 -040060
Jason Rhinelander4b159232017-08-04 13:05:12 -040061 // test_mixed_local_global
62 // We try this both with the global type registered first and vice versa (the order shouldn't
63 // matter).
64 m.def("register_mixed_global", [m]() {
65 bind_local<MixedGlobalLocal, 100>(m, "MixedGlobalLocal", py::module_local(false));
66 });
67 m.def("register_mixed_local", [m]() {
68 bind_local<MixedLocalGlobal, 1000>(m, "MixedLocalGlobal", py::module_local());
69 });
70 m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); });
71 m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); });
72
Jason Rhinelander7437c692017-07-28 22:03:44 -040073 // test_internal_locals_differ
74 m.def("local_cpp_types_addr", []() { return (uintptr_t) &py::detail::registered_local_types_cpp(); });
Dean Moldovan8d3cedb2017-08-13 03:03:06 +020075
76 // test_stl_caster_vs_stl_bind
77 m.def("load_vector_via_caster", [](std::vector<int> v) {
78 return std::accumulate(v.begin(), v.end(), 0);
79 });
Jason Rhinelander5e14aa62017-08-17 11:38:05 -040080
81 // test_cross_module_calls
82 m.def("return_self", [](LocalVec *v) { return v; });
83 m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); });
84
85 class Cat : public pets::Pet { public: Cat(std::string name) : Pet(name) {}; };
86 py::class_<pets::Pet>(m, "Pet", py::module_local())
87 .def("get_name", &pets::Pet::name);
88 // Binding for local extending class:
89 py::class_<Cat, pets::Pet>(m, "Cat")
90 .def(py::init<std::string>());
91 m.def("pet_name", [](pets::Pet &p) { return p.name(); });
92
93 py::class_<MixGL>(m, "MixGL").def(py::init<int>());
94 m.def("get_gl_value", [](MixGL &o) { return o.i + 10; });
95
96 py::class_<MixGL2>(m, "MixGL2").def(py::init<int>());
Jason Rhinelander7437c692017-07-28 22:03:44 -040097}