You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.2 KiB

3 months ago
  1. /*
  2. tests/test_modules.cpp -- nested modules, importing modules, and
  3. internal references
  4. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  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 "constructor_stats.h"
  10. std::string submodule_func() {
  11. return "submodule_func()";
  12. }
  13. class A {
  14. public:
  15. A(int v) : v(v) { print_created(this, v); }
  16. ~A() { print_destroyed(this); }
  17. A(const A&) { print_copy_created(this); }
  18. A& operator=(const A &copy) { print_copy_assigned(this); v = copy.v; return *this; }
  19. std::string toString() { return "A[" + std::to_string(v) + "]"; }
  20. private:
  21. int v;
  22. };
  23. class B {
  24. public:
  25. B() { print_default_created(this); }
  26. ~B() { print_destroyed(this); }
  27. B(const B&) { print_copy_created(this); }
  28. B& operator=(const B &copy) { print_copy_assigned(this); a1 = copy.a1; a2 = copy.a2; return *this; }
  29. A &get_a1() { return a1; }
  30. A &get_a2() { return a2; }
  31. A a1{1};
  32. A a2{2};
  33. };
  34. test_initializer modules([](py::module &m) {
  35. py::module m_sub = m.def_submodule("submodule");
  36. m_sub.def("submodule_func", &submodule_func);
  37. py::class_<A>(m_sub, "A")
  38. .def(py::init<int>())
  39. .def("__repr__", &A::toString);
  40. py::class_<B>(m_sub, "B")
  41. .def(py::init<>())
  42. .def("get_a1", &B::get_a1, "Return the internal A 1", py::return_value_policy::reference_internal)
  43. .def("get_a2", &B::get_a2, "Return the internal A 2", py::return_value_policy::reference_internal)
  44. .def_readwrite("a1", &B::a1) // def_readonly uses an internal reference return policy by default
  45. .def_readwrite("a2", &B::a2);
  46. m.attr("OD") = py::module::import("collections").attr("OrderedDict");
  47. // Registering two things with the same name
  48. m.def("duplicate_registration", []() {
  49. class Dupe1 { };
  50. class Dupe2 { };
  51. class Dupe3 { };
  52. class DupeException { };
  53. auto dm = py::module("dummy");
  54. auto failures = py::list();
  55. py::class_<Dupe1>(dm, "Dupe1");
  56. py::class_<Dupe2>(dm, "Dupe2");
  57. dm.def("dupe1_factory", []() { return Dupe1(); });
  58. py::exception<DupeException>(dm, "DupeException");
  59. try {
  60. py::class_<Dupe1>(dm, "Dupe1");
  61. failures.append("Dupe1 class");
  62. } catch (std::runtime_error &) {}
  63. try {
  64. dm.def("Dupe1", []() { return Dupe1(); });
  65. failures.append("Dupe1 function");
  66. } catch (std::runtime_error &) {}
  67. try {
  68. py::class_<Dupe3>(dm, "dupe1_factory");
  69. failures.append("dupe1_factory");
  70. } catch (std::runtime_error &) {}
  71. try {
  72. py::exception<Dupe3>(dm, "Dupe2");
  73. failures.append("Dupe2");
  74. } catch (std::runtime_error &) {}
  75. try {
  76. dm.def("DupeException", []() { return 30; });
  77. failures.append("DupeException1");
  78. } catch (std::runtime_error &) {}
  79. try {
  80. py::class_<DupeException>(dm, "DupeException");
  81. failures.append("DupeException2");
  82. } catch (std::runtime_error &) {}
  83. return failures;
  84. });
  85. });