Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:51

0001 /*
0002     tests/pybind11_cross_module_tests.cpp -- contains tests that require multiple modules
0003 
0004     Copyright (c) 2017 Jason Rhinelander <jason@imaginary.ca>
0005 
0006     All rights reserved. Use of this source code is governed by a
0007     BSD-style license that can be found in the LICENSE file.
0008 */
0009 
0010 #include <pybind11/stl_bind.h>
0011 
0012 #include "local_bindings.h"
0013 #include "pybind11_tests.h"
0014 #include "test_exceptions.h"
0015 
0016 #include <numeric>
0017 #include <utility>
0018 
0019 PYBIND11_MODULE(pybind11_cross_module_tests, m) {
0020     m.doc() = "pybind11 cross-module test module";
0021 
0022     // test_local_bindings.py tests:
0023     //
0024     // Definitions here are tested by importing both this module and the
0025     // relevant pybind11_tests submodule from a test_whatever.py
0026 
0027     // test_load_external
0028     bind_local<ExternalType1>(m, "ExternalType1", py::module_local());
0029     bind_local<ExternalType2>(m, "ExternalType2", py::module_local());
0030 
0031     // test_exceptions.py
0032     py::register_local_exception<LocalSimpleException>(m, "LocalSimpleException");
0033     m.def("raise_runtime_error", []() {
0034         PyErr_SetString(PyExc_RuntimeError, "My runtime error");
0035         throw py::error_already_set();
0036     });
0037     m.def("raise_value_error", []() {
0038         PyErr_SetString(PyExc_ValueError, "My value error");
0039         throw py::error_already_set();
0040     });
0041     m.def("throw_pybind_value_error", []() { throw py::value_error("pybind11 value error"); });
0042     m.def("throw_pybind_type_error", []() { throw py::type_error("pybind11 type error"); });
0043     m.def("throw_stop_iteration", []() { throw py::stop_iteration(); });
0044     m.def("throw_local_error", []() { throw LocalException("just local"); });
0045     m.def("throw_local_simple_error", []() { throw LocalSimpleException("external mod"); });
0046     py::register_exception_translator([](std::exception_ptr p) {
0047         try {
0048             if (p) {
0049                 std::rethrow_exception(p);
0050             }
0051         } catch (const shared_exception &e) {
0052             PyErr_SetString(PyExc_KeyError, e.what());
0053         }
0054     });
0055 
0056     // translate the local exception into a key error but only in this module
0057     py::register_local_exception_translator([](std::exception_ptr p) {
0058         try {
0059             if (p) {
0060                 std::rethrow_exception(p);
0061             }
0062         } catch (const LocalException &e) {
0063             PyErr_SetString(PyExc_KeyError, e.what());
0064         }
0065     });
0066 
0067     // test_local_bindings.py
0068     // Local to both:
0069     bind_local<LocalType, 1>(m, "LocalType", py::module_local()).def("get2", [](LocalType &t) {
0070         return t.i + 2;
0071     });
0072 
0073     // Can only be called with our python type:
0074     m.def("local_value", [](LocalType &l) { return l.i; });
0075 
0076     // test_nonlocal_failure
0077     // This registration will fail (global registration when LocalFail is already registered
0078     // globally in the main test module):
0079     m.def("register_nonlocal", [m]() { bind_local<NonLocalType, 0>(m, "NonLocalType"); });
0080 
0081     // test_stl_bind_local
0082     // stl_bind.h binders defaults to py::module_local if the types are local or converting:
0083     py::bind_vector<LocalVec>(m, "LocalVec");
0084     py::bind_map<LocalMap>(m, "LocalMap");
0085 
0086     // test_stl_bind_global
0087     // and global if the type (or one of the types, for the map) is global (so these will fail,
0088     // assuming pybind11_tests is already loaded):
0089     m.def("register_nonlocal_vec", [m]() { py::bind_vector<NonLocalVec>(m, "NonLocalVec"); });
0090     m.def("register_nonlocal_map", [m]() { py::bind_map<NonLocalMap>(m, "NonLocalMap"); });
0091     // The default can, however, be overridden to global using `py::module_local()` or
0092     // `py::module_local(false)`.
0093     // Explicitly made local:
0094     py::bind_vector<NonLocalVec2>(m, "NonLocalVec2", py::module_local());
0095     // Explicitly made global (and so will fail to bind):
0096     m.def("register_nonlocal_map2",
0097           [m]() { py::bind_map<NonLocalMap2>(m, "NonLocalMap2", py::module_local(false)); });
0098 
0099     // test_mixed_local_global
0100     // We try this both with the global type registered first and vice versa (the order shouldn't
0101     // matter).
0102     m.def("register_mixed_global_local",
0103           [m]() { bind_local<MixedGlobalLocal, 200>(m, "MixedGlobalLocal", py::module_local()); });
0104     m.def("register_mixed_local_global", [m]() {
0105         bind_local<MixedLocalGlobal, 2000>(m, "MixedLocalGlobal", py::module_local(false));
0106     });
0107     m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); });
0108     m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); });
0109 
0110     // test_internal_locals_differ
0111     m.def("local_cpp_types_addr",
0112           []() { return (uintptr_t) &py::detail::get_local_internals().registered_types_cpp; });
0113 
0114     // test_stl_caster_vs_stl_bind
0115     py::bind_vector<std::vector<int>>(m, "VectorInt");
0116 
0117     m.def("load_vector_via_binding",
0118           [](std::vector<int> &v) { return std::accumulate(v.begin(), v.end(), 0); });
0119 
0120     // test_cross_module_calls
0121     m.def("return_self", [](LocalVec *v) { return v; });
0122     m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); });
0123 
0124     class Dog : public pets::Pet {
0125     public:
0126         explicit Dog(std::string name) : Pet(std::move(name)) {}
0127     };
0128     py::class_<pets::Pet>(m, "Pet", py::module_local()).def("name", &pets::Pet::name);
0129     // Binding for local extending class:
0130     py::class_<Dog, pets::Pet>(m, "Dog").def(py::init<std::string>());
0131     m.def("pet_name", [](pets::Pet &p) { return p.name(); });
0132 
0133     py::class_<MixGL>(m, "MixGL", py::module_local()).def(py::init<int>());
0134     m.def("get_gl_value", [](MixGL &o) { return o.i + 100; });
0135 
0136     py::class_<MixGL2>(m, "MixGL2", py::module_local()).def(py::init<int>());
0137 
0138     // test_vector_bool
0139     // We can't test both stl.h and stl_bind.h conversions of `std::vector<bool>` within
0140     // the same module (it would be an ODR violation). Therefore `bind_vector` of `bool`
0141     // is defined here and tested in `test_stl_binders.py`.
0142     py::bind_vector<std::vector<bool>>(m, "VectorBool");
0143 
0144     // test_missing_header_message
0145     // The main module already includes stl.h, but we need to test the error message
0146     // which appears when this header is missing.
0147     m.def("missing_header_arg", [](const std::vector<float> &) {});
0148     m.def("missing_header_return", []() { return std::vector<float>(); });
0149 }