pybind11_cross_module_tests.cpp revision 12391:ceeca8b41e4b
1/*
2    tests/pybind11_cross_module_tests.cpp -- contains tests that require multiple modules
3
4    Copyright (c) 2017 Jason Rhinelander <jason@imaginary.ca>
5
6    All rights reserved. Use of this source code is governed by a
7    BSD-style license that can be found in the LICENSE file.
8*/
9
10#include "pybind11_tests.h"
11#include "local_bindings.h"
12#include <pybind11/stl_bind.h>
13#include <numeric>
14
15PYBIND11_MODULE(pybind11_cross_module_tests, m) {
16    m.doc() = "pybind11 cross-module test module";
17
18    // test_local_bindings.py tests:
19    //
20    // Definitions here are tested by importing both this module and the
21    // relevant pybind11_tests submodule from a test_whatever.py
22
23    // test_load_external
24    bind_local<ExternalType1>(m, "ExternalType1", py::module_local());
25    bind_local<ExternalType2>(m, "ExternalType2", py::module_local());
26
27    // test_exceptions.py
28    m.def("raise_runtime_error", []() { PyErr_SetString(PyExc_RuntimeError, "My runtime error"); throw py::error_already_set(); });
29    m.def("raise_value_error", []() { PyErr_SetString(PyExc_ValueError, "My value error"); throw py::error_already_set(); });
30    m.def("throw_pybind_value_error", []() { throw py::value_error("pybind11 value error"); });
31    m.def("throw_pybind_type_error", []() { throw py::type_error("pybind11 type error"); });
32    m.def("throw_stop_iteration", []() { throw py::stop_iteration(); });
33
34    // test_local_bindings.py
35    // Local to both:
36    bind_local<LocalType, 1>(m, "LocalType", py::module_local())
37        .def("get2", [](LocalType &t) { return t.i + 2; })
38        ;
39
40    // Can only be called with our python type:
41    m.def("local_value", [](LocalType &l) { return l.i; });
42
43    // test_nonlocal_failure
44    // This registration will fail (global registration when LocalFail is already registered
45    // globally in the main test module):
46    m.def("register_nonlocal", [m]() {
47        bind_local<NonLocalType, 0>(m, "NonLocalType");
48    });
49
50    // test_stl_bind_local
51    // stl_bind.h binders defaults to py::module_local if the types are local or converting:
52    py::bind_vector<LocalVec>(m, "LocalVec");
53    py::bind_map<LocalMap>(m, "LocalMap");
54
55    // test_stl_bind_global
56    // and global if the type (or one of the types, for the map) is global (so these will fail,
57    // assuming pybind11_tests is already loaded):
58    m.def("register_nonlocal_vec", [m]() {
59        py::bind_vector<NonLocalVec>(m, "NonLocalVec");
60    });
61    m.def("register_nonlocal_map", [m]() {
62        py::bind_map<NonLocalMap>(m, "NonLocalMap");
63    });
64    // The default can, however, be overridden to global using `py::module_local()` or
65    // `py::module_local(false)`.
66    // Explicitly made local:
67    py::bind_vector<NonLocalVec2>(m, "NonLocalVec2", py::module_local());
68    // Explicitly made global (and so will fail to bind):
69    m.def("register_nonlocal_map2", [m]() {
70        py::bind_map<NonLocalMap2>(m, "NonLocalMap2", py::module_local(false));
71    });
72
73    // test_mixed_local_global
74    // We try this both with the global type registered first and vice versa (the order shouldn't
75    // matter).
76    m.def("register_mixed_global_local", [m]() {
77        bind_local<MixedGlobalLocal, 200>(m, "MixedGlobalLocal", py::module_local());
78    });
79    m.def("register_mixed_local_global", [m]() {
80        bind_local<MixedLocalGlobal, 2000>(m, "MixedLocalGlobal", py::module_local(false));
81    });
82    m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); });
83    m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); });
84
85    // test_internal_locals_differ
86    m.def("local_cpp_types_addr", []() { return (uintptr_t) &py::detail::registered_local_types_cpp(); });
87
88    // test_stl_caster_vs_stl_bind
89    py::bind_vector<std::vector<int>>(m, "VectorInt");
90
91    m.def("load_vector_via_binding", [](std::vector<int> &v) {
92        return std::accumulate(v.begin(), v.end(), 0);
93    });
94
95    // test_cross_module_calls
96    m.def("return_self", [](LocalVec *v) { return v; });
97    m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); });
98
99    class Dog : public pets::Pet { public: Dog(std::string name) : Pet(name) {}; };
100    py::class_<pets::Pet>(m, "Pet", py::module_local())
101        .def("name", &pets::Pet::name);
102    // Binding for local extending class:
103    py::class_<Dog, pets::Pet>(m, "Dog")
104        .def(py::init<std::string>());
105    m.def("pet_name", [](pets::Pet &p) { return p.name(); });
106
107    py::class_<MixGL>(m, "MixGL", py::module_local()).def(py::init<int>());
108    m.def("get_gl_value", [](MixGL &o) { return o.i + 100; });
109
110    py::class_<MixGL2>(m, "MixGL2", py::module_local()).def(py::init<int>());
111
112    // test_vector_bool
113    // We can't test both stl.h and stl_bind.h conversions of `std::vector<bool>` within
114    // the same module (it would be an ODR violation). Therefore `bind_vector` of `bool`
115    // is defined here and tested in `test_stl_binders.py`.
116    py::bind_vector<std::vector<bool>>(m, "VectorBool");
117
118    // test_missing_header_message
119    // The main module already includes stl.h, but we need to test the error message
120    // which appears when this header is missing.
121    m.def("missing_header_arg", [](std::vector<float>) { });
122    m.def("missing_header_return", []() { return std::vector<float>(); });
123}
124