test_modules.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. tests/test_modules.cpp -- nested modules, importing modules, and
  3. internal references
  4. Copyright (c) 2016 Wenzel Jakob <[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 "constructor_stats.h"
  10. TEST_SUBMODULE(modules, m) {
  11. // test_nested_modules
  12. py::module m_sub = m.def_submodule("subsubmodule");
  13. m_sub.def("submodule_func", []() { return "submodule_func()"; });
  14. // test_reference_internal
  15. class A {
  16. public:
  17. A(int v) : v(v) { print_created(this, v); }
  18. ~A() { print_destroyed(this); }
  19. A(const A&) { print_copy_created(this); }
  20. A& operator=(const A &copy) { print_copy_assigned(this); v = copy.v; return *this; }
  21. std::string toString() { return "A[" + std::to_string(v) + "]"; }
  22. private:
  23. int v;
  24. };
  25. py::class_<A>(m_sub, "A")
  26. .def(py::init<int>())
  27. .def("__repr__", &A::toString);
  28. class B {
  29. public:
  30. B() { print_default_created(this); }
  31. ~B() { print_destroyed(this); }
  32. B(const B&) { print_copy_created(this); }
  33. B& operator=(const B &copy) { print_copy_assigned(this); a1 = copy.a1; a2 = copy.a2; return *this; }
  34. A &get_a1() { return a1; }
  35. A &get_a2() { return a2; }
  36. A a1{1};
  37. A a2{2};
  38. };
  39. py::class_<B>(m_sub, "B")
  40. .def(py::init<>())
  41. .def("get_a1", &B::get_a1, "Return the internal A 1", py::return_value_policy::reference_internal)
  42. .def("get_a2", &B::get_a2, "Return the internal A 2", py::return_value_policy::reference_internal)
  43. .def_readwrite("a1", &B::a1) // def_readonly uses an internal reference return policy by default
  44. .def_readwrite("a2", &B::a2);
  45. m.attr("OD") = py::module::import("collections").attr("OrderedDict");
  46. // test_duplicate_registration
  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. }