test_local_bindings.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  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. Copyright (c) 2017 Jason Rhinelander <[email protected]>
  5. All rights reserved. Use of this source code is governed by a
  6. BSD-style license that can be found in the LICENSE file.
  7. */
  8. #include "pybind11_tests.h"
  9. #include "local_bindings.h"
  10. #include <pybind11/stl.h>
  11. #include <pybind11/stl_bind.h>
  12. #include <numeric>
  13. TEST_SUBMODULE(local_bindings, m) {
  14. // test_load_external
  15. m.def("load_external1", [](ExternalType1 &e) { return e.i; });
  16. m.def("load_external2", [](ExternalType2 &e) { return e.i; });
  17. // test_local_bindings
  18. // Register a class with py::module_local:
  19. bind_local<LocalType, -1>(m, "LocalType", py::module_local())
  20. .def("get3", [](LocalType &t) { return t.i + 3; })
  21. ;
  22. m.def("local_value", [](LocalType &l) { return l.i; });
  23. // test_nonlocal_failure
  24. // The main pybind11 test module is loaded first, so this registration will succeed (the second
  25. // one, in pybind11_cross_module_tests.cpp, is designed to fail):
  26. bind_local<NonLocalType, 0>(m, "NonLocalType")
  27. .def(py::init<int>())
  28. .def("get", [](LocalType &i) { return i.i; })
  29. ;
  30. // test_duplicate_local
  31. // py::module_local declarations should be visible across compilation units that get linked together;
  32. // this tries to register a duplicate local. It depends on a definition in test_class.cpp and
  33. // should raise a runtime error from the duplicate definition attempt. If test_class isn't
  34. // available it *also* throws a runtime error (with "test_class not enabled" as value).
  35. m.def("register_local_external", [m]() {
  36. auto main = py::module::import("pybind11_tests");
  37. if (py::hasattr(main, "class_")) {
  38. bind_local<LocalExternal, 7>(m, "LocalExternal", py::module_local());
  39. }
  40. else throw std::runtime_error("test_class not enabled");
  41. });
  42. // test_stl_bind_local
  43. // stl_bind.h binders defaults to py::module_local if the types are local or converting:
  44. py::bind_vector<LocalVec>(m, "LocalVec");
  45. py::bind_map<LocalMap>(m, "LocalMap");
  46. // and global if the type (or one of the types, for the map) is global:
  47. py::bind_vector<NonLocalVec>(m, "NonLocalVec");
  48. py::bind_map<NonLocalMap>(m, "NonLocalMap");
  49. // test_stl_bind_global
  50. // They can, however, be overridden to global using `py::module_local(false)`:
  51. bind_local<NonLocal2, 10>(m, "NonLocal2");
  52. py::bind_vector<LocalVec2>(m, "LocalVec2", py::module_local());
  53. py::bind_map<NonLocalMap2>(m, "NonLocalMap2", py::module_local(false));
  54. // test_mixed_local_global
  55. // We try this both with the global type registered first and vice versa (the order shouldn't
  56. // matter).
  57. m.def("register_mixed_global", [m]() {
  58. bind_local<MixedGlobalLocal, 100>(m, "MixedGlobalLocal", py::module_local(false));
  59. });
  60. m.def("register_mixed_local", [m]() {
  61. bind_local<MixedLocalGlobal, 1000>(m, "MixedLocalGlobal", py::module_local());
  62. });
  63. m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); });
  64. m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); });
  65. // test_internal_locals_differ
  66. m.def("local_cpp_types_addr", []() { return (uintptr_t) &py::detail::registered_local_types_cpp(); });
  67. // test_stl_caster_vs_stl_bind
  68. m.def("load_vector_via_caster", [](std::vector<int> v) {
  69. return std::accumulate(v.begin(), v.end(), 0);
  70. });
  71. // test_cross_module_calls
  72. m.def("return_self", [](LocalVec *v) { return v; });
  73. m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); });
  74. class Cat : public pets::Pet { public: Cat(std::string name) : Pet(name) {}; };
  75. py::class_<pets::Pet>(m, "Pet", py::module_local())
  76. .def("get_name", &pets::Pet::name);
  77. // Binding for local extending class:
  78. py::class_<Cat, pets::Pet>(m, "Cat")
  79. .def(py::init<std::string>());
  80. m.def("pet_name", [](pets::Pet &p) { return p.name(); });
  81. py::class_<MixGL>(m, "MixGL").def(py::init<int>());
  82. m.def("get_gl_value", [](MixGL &o) { return o.i + 10; });
  83. py::class_<MixGL2>(m, "MixGL2").def(py::init<int>());
  84. }