pybind11_cross_module_tests.cpp 5.2 KB

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