test_modules.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. TEST_SUBMODULE(modules, m) {
  11. // test_nested_modules
  12. // This is intentionally "py::module" to verify it still can be used in place of "py::module_"
  13. py::module m_sub = m.def_submodule("subsubmodule");
  14. m_sub.def("submodule_func", []() { return "submodule_func()"; });
  15. // test_reference_internal
  16. class A {
  17. public:
  18. A(int v) : v(v) { print_created(this, v); }
  19. ~A() { print_destroyed(this); }
  20. A(const A&) { print_copy_created(this); }
  21. A& operator=(const A &copy) { print_copy_assigned(this); v = copy.v; return *this; }
  22. std::string toString() { return "A[" + std::to_string(v) + "]"; }
  23. private:
  24. int v;
  25. };
  26. py::class_<A>(m_sub, "A")
  27. .def(py::init<int>())
  28. .def("__repr__", &A::toString);
  29. class B {
  30. public:
  31. B() { print_default_created(this); }
  32. ~B() { print_destroyed(this); }
  33. B(const B&) { print_copy_created(this); }
  34. B& operator=(const B &copy) { print_copy_assigned(this); a1 = copy.a1; a2 = copy.a2; return *this; }
  35. A &get_a1() { return a1; }
  36. A &get_a2() { return a2; }
  37. A a1{1};
  38. A a2{2};
  39. };
  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. // This is intentionally "py::module" to verify it still can be used in place of "py::module_"
  47. m.attr("OD") = py::module::import("collections").attr("OrderedDict");
  48. // test_duplicate_registration
  49. // Registering two things with the same name
  50. m.def("duplicate_registration", []() {
  51. class Dupe1 { };
  52. class Dupe2 { };
  53. class Dupe3 { };
  54. class DupeException { };
  55. // Go ahead and leak, until we have a non-leaking py::module_ constructor
  56. auto dm = py::module_::create_extension_module("dummy", nullptr, new py::module_::module_def);
  57. auto failures = py::list();
  58. py::class_<Dupe1>(dm, "Dupe1");
  59. py::class_<Dupe2>(dm, "Dupe2");
  60. dm.def("dupe1_factory", []() { return Dupe1(); });
  61. py::exception<DupeException>(dm, "DupeException");
  62. try {
  63. py::class_<Dupe1>(dm, "Dupe1");
  64. failures.append("Dupe1 class");
  65. } catch (std::runtime_error &) {}
  66. try {
  67. dm.def("Dupe1", []() { return Dupe1(); });
  68. failures.append("Dupe1 function");
  69. } catch (std::runtime_error &) {}
  70. try {
  71. py::class_<Dupe3>(dm, "dupe1_factory");
  72. failures.append("dupe1_factory");
  73. } catch (std::runtime_error &) {}
  74. try {
  75. py::exception<Dupe3>(dm, "Dupe2");
  76. failures.append("Dupe2");
  77. } catch (std::runtime_error &) {}
  78. try {
  79. dm.def("DupeException", []() { return 30; });
  80. failures.append("DupeException1");
  81. } catch (std::runtime_error &) {}
  82. try {
  83. py::class_<DupeException>(dm, "DupeException");
  84. failures.append("DupeException2");
  85. } catch (std::runtime_error &) {}
  86. return failures;
  87. });
  88. }