2017-07-29 00:57:57 +00:00
|
|
|
/*
|
|
|
|
tests/pybind11_cross_module_tests.cpp -- contains tests that require multiple modules
|
|
|
|
|
|
|
|
Copyright (c) 2017 Jason Rhinelander <jason@imaginary.ca>
|
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
#include <pybind11/stl_bind.h>
|
|
|
|
|
2017-07-29 02:03:44 +00:00
|
|
|
#include "local_bindings.h"
|
2022-02-10 20:17:07 +00:00
|
|
|
#include "pybind11_tests.h"
|
2021-05-27 15:00:18 +00:00
|
|
|
#include "test_exceptions.h"
|
2021-07-09 21:09:56 +00:00
|
|
|
|
2017-08-13 01:03:06 +00:00
|
|
|
#include <numeric>
|
2021-07-09 21:09:56 +00:00
|
|
|
#include <utility>
|
2017-07-29 00:57:57 +00:00
|
|
|
|
|
|
|
PYBIND11_MODULE(pybind11_cross_module_tests, m) {
|
|
|
|
m.doc() = "pybind11 cross-module test module";
|
|
|
|
|
|
|
|
// test_local_bindings.py tests:
|
|
|
|
//
|
|
|
|
// Definitions here are tested by importing both this module and the
|
|
|
|
// relevant pybind11_tests submodule from a test_whatever.py
|
|
|
|
|
2017-09-02 23:31:47 +00:00
|
|
|
// test_load_external
|
|
|
|
bind_local<ExternalType1>(m, "ExternalType1", py::module_local());
|
|
|
|
bind_local<ExternalType2>(m, "ExternalType2", py::module_local());
|
|
|
|
|
2017-07-29 01:38:23 +00:00
|
|
|
// test_exceptions.py
|
2021-07-21 12:22:18 +00:00
|
|
|
py::register_local_exception<LocalSimpleException>(m, "LocalSimpleException");
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def("raise_runtime_error", []() {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "My runtime error");
|
|
|
|
throw py::error_already_set();
|
|
|
|
});
|
|
|
|
m.def("raise_value_error", []() {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "My value error");
|
|
|
|
throw py::error_already_set();
|
|
|
|
});
|
2017-07-29 01:38:23 +00:00
|
|
|
m.def("throw_pybind_value_error", []() { throw py::value_error("pybind11 value error"); });
|
|
|
|
m.def("throw_pybind_type_error", []() { throw py::type_error("pybind11 type error"); });
|
|
|
|
m.def("throw_stop_iteration", []() { throw py::stop_iteration(); });
|
2021-07-21 12:22:18 +00:00
|
|
|
m.def("throw_local_error", []() { throw LocalException("just local"); });
|
|
|
|
m.def("throw_local_simple_error", []() { throw LocalSimpleException("external mod"); });
|
2021-05-27 15:00:18 +00:00
|
|
|
py::register_exception_translator([](std::exception_ptr p) {
|
2022-02-10 20:17:07 +00:00
|
|
|
try {
|
|
|
|
if (p) {
|
|
|
|
std::rethrow_exception(p);
|
|
|
|
}
|
|
|
|
} catch (const shared_exception &e) {
|
|
|
|
PyErr_SetString(PyExc_KeyError, e.what());
|
|
|
|
}
|
2021-05-27 15:00:18 +00:00
|
|
|
});
|
2017-07-29 01:38:23 +00:00
|
|
|
|
2021-07-21 12:22:18 +00:00
|
|
|
// translate the local exception into a key error but only in this module
|
|
|
|
py::register_local_exception_translator([](std::exception_ptr p) {
|
2022-02-10 20:17:07 +00:00
|
|
|
try {
|
|
|
|
if (p) {
|
|
|
|
std::rethrow_exception(p);
|
|
|
|
}
|
|
|
|
} catch (const LocalException &e) {
|
|
|
|
PyErr_SetString(PyExc_KeyError, e.what());
|
|
|
|
}
|
2021-07-21 12:22:18 +00:00
|
|
|
});
|
|
|
|
|
2017-07-29 02:03:44 +00:00
|
|
|
// test_local_bindings.py
|
|
|
|
// Local to both:
|
2022-02-10 20:17:07 +00:00
|
|
|
bind_local<LocalType, 1>(m, "LocalType", py::module_local()).def("get2", [](LocalType &t) {
|
|
|
|
return t.i + 2;
|
|
|
|
});
|
2017-07-29 02:03:44 +00:00
|
|
|
|
|
|
|
// Can only be called with our python type:
|
|
|
|
m.def("local_value", [](LocalType &l) { return l.i; });
|
|
|
|
|
|
|
|
// test_nonlocal_failure
|
|
|
|
// This registration will fail (global registration when LocalFail is already registered
|
|
|
|
// globally in the main test module):
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def("register_nonlocal", [m]() { bind_local<NonLocalType, 0>(m, "NonLocalType"); });
|
2017-07-29 02:03:44 +00:00
|
|
|
|
|
|
|
// test_stl_bind_local
|
|
|
|
// stl_bind.h binders defaults to py::module_local if the types are local or converting:
|
2017-08-13 01:03:06 +00:00
|
|
|
py::bind_vector<LocalVec>(m, "LocalVec");
|
|
|
|
py::bind_map<LocalMap>(m, "LocalMap");
|
|
|
|
|
|
|
|
// test_stl_bind_global
|
2017-07-29 02:03:44 +00:00
|
|
|
// and global if the type (or one of the types, for the map) is global (so these will fail,
|
|
|
|
// assuming pybind11_tests is already loaded):
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def("register_nonlocal_vec", [m]() { py::bind_vector<NonLocalVec>(m, "NonLocalVec"); });
|
|
|
|
m.def("register_nonlocal_map", [m]() { py::bind_map<NonLocalMap>(m, "NonLocalMap"); });
|
2017-07-29 02:03:44 +00:00
|
|
|
// The default can, however, be overridden to global using `py::module_local()` or
|
|
|
|
// `py::module_local(false)`.
|
|
|
|
// Explicitly made local:
|
2017-08-13 01:03:06 +00:00
|
|
|
py::bind_vector<NonLocalVec2>(m, "NonLocalVec2", py::module_local());
|
2017-07-29 02:03:44 +00:00
|
|
|
// Explicitly made global (and so will fail to bind):
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def("register_nonlocal_map2",
|
|
|
|
[m]() { py::bind_map<NonLocalMap2>(m, "NonLocalMap2", py::module_local(false)); });
|
2017-07-29 02:03:44 +00:00
|
|
|
|
2017-08-04 17:05:12 +00:00
|
|
|
// test_mixed_local_global
|
|
|
|
// We try this both with the global type registered first and vice versa (the order shouldn't
|
|
|
|
// matter).
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def("register_mixed_global_local",
|
|
|
|
[m]() { bind_local<MixedGlobalLocal, 200>(m, "MixedGlobalLocal", py::module_local()); });
|
2017-08-04 17:05:12 +00:00
|
|
|
m.def("register_mixed_local_global", [m]() {
|
|
|
|
bind_local<MixedLocalGlobal, 2000>(m, "MixedLocalGlobal", py::module_local(false));
|
|
|
|
});
|
|
|
|
m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); });
|
|
|
|
m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); });
|
|
|
|
|
2017-07-29 02:03:44 +00:00
|
|
|
// test_internal_locals_differ
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def("local_cpp_types_addr",
|
|
|
|
[]() { return (uintptr_t) &py::detail::get_local_internals().registered_types_cpp; });
|
2017-08-13 01:03:06 +00:00
|
|
|
|
|
|
|
// test_stl_caster_vs_stl_bind
|
|
|
|
py::bind_vector<std::vector<int>>(m, "VectorInt");
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def("load_vector_via_binding",
|
|
|
|
[](std::vector<int> &v) { return std::accumulate(v.begin(), v.end(), 0); });
|
2017-08-17 15:38:05 +00:00
|
|
|
|
|
|
|
// test_cross_module_calls
|
|
|
|
m.def("return_self", [](LocalVec *v) { return v; });
|
|
|
|
m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); });
|
|
|
|
|
2021-07-09 21:09:56 +00:00
|
|
|
class Dog : public pets::Pet {
|
|
|
|
public:
|
CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250)
* Adding google-explicit-constructor to .clang-tidy
* clang-tidy explicit attr.h (all automatic)
* clang-tidy explicit cast.h (all automatic)
* clang-tidy detail/init.h (1 NOLINT)
* clang-tidy detail/type_caster_base.h (2 NOLINT)
* clang-tidy pybind11.h (7 NOLINT)
* clang-tidy detail/common.h (3 NOLINT)
* clang-tidy detail/descr.h (2 NOLINT)
* clang-tidy pytypes.h (23 NOLINT, only 1 explicit)
* clang-tidy eigen.h (7 NOLINT, 0 explicit)
* Adding 2 explicit in functional.h
* Adding 4 explicit in iostream.h
* clang-tidy numpy.h (1 NOLINT, 1 explicit)
* clang-tidy embed.h (0 NOLINT, 1 explicit)
* clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit)
* clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit)
* clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit)
* clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit)
* clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit)
* clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit)
* clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/object.h (0 NOLINT, 2 explicit)
* clang-tidy batch of fully automatic fixes.
* Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error.
2021-09-09 01:53:38 +00:00
|
|
|
explicit Dog(std::string name) : Pet(std::move(name)) {}
|
2021-07-09 21:09:56 +00:00
|
|
|
};
|
2022-02-10 20:17:07 +00:00
|
|
|
py::class_<pets::Pet>(m, "Pet", py::module_local()).def("name", &pets::Pet::name);
|
2017-08-17 15:38:05 +00:00
|
|
|
// Binding for local extending class:
|
2022-02-10 20:17:07 +00:00
|
|
|
py::class_<Dog, pets::Pet>(m, "Dog").def(py::init<std::string>());
|
2017-08-17 15:38:05 +00:00
|
|
|
m.def("pet_name", [](pets::Pet &p) { return p.name(); });
|
|
|
|
|
|
|
|
py::class_<MixGL>(m, "MixGL", py::module_local()).def(py::init<int>());
|
|
|
|
m.def("get_gl_value", [](MixGL &o) { return o.i + 100; });
|
|
|
|
|
|
|
|
py::class_<MixGL2>(m, "MixGL2", py::module_local()).def(py::init<int>());
|
2017-09-01 19:42:20 +00:00
|
|
|
|
|
|
|
// test_vector_bool
|
|
|
|
// We can't test both stl.h and stl_bind.h conversions of `std::vector<bool>` within
|
|
|
|
// the same module (it would be an ODR violation). Therefore `bind_vector` of `bool`
|
|
|
|
// is defined here and tested in `test_stl_binders.py`.
|
|
|
|
py::bind_vector<std::vector<bool>>(m, "VectorBool");
|
2017-09-09 18:21:34 +00:00
|
|
|
|
|
|
|
// test_missing_header_message
|
|
|
|
// The main module already includes stl.h, but we need to test the error message
|
|
|
|
// which appears when this header is missing.
|
2021-07-12 20:10:28 +00:00
|
|
|
m.def("missing_header_arg", [](const std::vector<float> &) {});
|
2017-09-09 18:21:34 +00:00
|
|
|
m.def("missing_header_return", []() { return std::vector<float>(); });
|
2017-07-29 00:57:57 +00:00
|
|
|
}
|