test_call_policies.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. tests/test_call_policies.cpp -- keep_alive and call_guard
  3. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  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. struct CustomGuard {
  9. static bool enabled;
  10. CustomGuard() { enabled = true; }
  11. ~CustomGuard() { enabled = false; }
  12. static const char *report_status() { return enabled ? "guarded" : "unguarded"; }
  13. };
  14. bool CustomGuard::enabled = false;
  15. struct DependentGuard {
  16. static bool enabled;
  17. DependentGuard() { enabled = CustomGuard::enabled; }
  18. ~DependentGuard() { enabled = false; }
  19. static const char *report_status() { return enabled ? "guarded" : "unguarded"; }
  20. };
  21. bool DependentGuard::enabled = false;
  22. TEST_SUBMODULE(call_policies, m) {
  23. // Parent/Child are used in:
  24. // test_keep_alive_argument, test_keep_alive_return_value, test_alive_gc_derived,
  25. // test_alive_gc_multi_derived, test_return_none, test_keep_alive_constructor
  26. class Child {
  27. public:
  28. Child() { py::print("Allocating child."); }
  29. Child(const Child &) = default;
  30. Child(Child &&) = default;
  31. ~Child() { py::print("Releasing child."); }
  32. };
  33. py::class_<Child>(m, "Child")
  34. .def(py::init<>());
  35. class Parent {
  36. public:
  37. Parent() { py::print("Allocating parent."); }
  38. Parent(const Parent& parent) = default;
  39. ~Parent() { py::print("Releasing parent."); }
  40. void addChild(Child *) { }
  41. Child *returnChild() { return new Child(); }
  42. Child *returnNullChild() { return nullptr; }
  43. };
  44. py::class_<Parent>(m, "Parent")
  45. .def(py::init<>())
  46. .def(py::init([](Child *) { return new Parent(); }), py::keep_alive<1, 2>())
  47. .def("addChild", &Parent::addChild)
  48. .def("addChildKeepAlive", &Parent::addChild, py::keep_alive<1, 2>())
  49. .def("returnChild", &Parent::returnChild)
  50. .def("returnChildKeepAlive", &Parent::returnChild, py::keep_alive<1, 0>())
  51. .def("returnNullChildKeepAliveChild", &Parent::returnNullChild, py::keep_alive<1, 0>())
  52. .def("returnNullChildKeepAliveParent", &Parent::returnNullChild, py::keep_alive<0, 1>());
  53. #if !defined(PYPY_VERSION)
  54. // test_alive_gc
  55. class ParentGC : public Parent {
  56. public:
  57. using Parent::Parent;
  58. };
  59. py::class_<ParentGC, Parent>(m, "ParentGC", py::dynamic_attr())
  60. .def(py::init<>());
  61. #endif
  62. // test_call_guard
  63. m.def("unguarded_call", &CustomGuard::report_status);
  64. m.def("guarded_call", &CustomGuard::report_status, py::call_guard<CustomGuard>());
  65. m.def("multiple_guards_correct_order", []() {
  66. return CustomGuard::report_status() + std::string(" & ") + DependentGuard::report_status();
  67. }, py::call_guard<CustomGuard, DependentGuard>());
  68. m.def("multiple_guards_wrong_order", []() {
  69. return DependentGuard::report_status() + std::string(" & ") + CustomGuard::report_status();
  70. }, py::call_guard<DependentGuard, CustomGuard>());
  71. #if defined(WITH_THREAD) && !defined(PYPY_VERSION)
  72. // `py::call_guard<py::gil_scoped_release>()` should work in PyPy as well,
  73. // but it's unclear how to test it without `PyGILState_GetThisThreadState`.
  74. auto report_gil_status = []() {
  75. auto is_gil_held = false;
  76. if (auto tstate = py::detail::get_thread_state_unchecked())
  77. is_gil_held = (tstate == PyGILState_GetThisThreadState());
  78. return is_gil_held ? "GIL held" : "GIL released";
  79. };
  80. m.def("with_gil", report_gil_status);
  81. m.def("without_gil", report_gil_status, py::call_guard<py::gil_scoped_release>());
  82. #endif
  83. }