test_call_policies.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. tests/test_call_policies.cpp -- keep_alive and call_guard
  3. Copyright (c) 2016 Wenzel Jakob <[email protected]>
  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. #include "constructor_stats.h"
  9. struct CustomGuard {
  10. static bool enabled;
  11. CustomGuard() { enabled = true; }
  12. ~CustomGuard() { enabled = false; }
  13. static const char *report_status() { return enabled ? "guarded" : "unguarded"; }
  14. };
  15. bool CustomGuard::enabled = false;
  16. struct DependentGuard {
  17. static bool enabled;
  18. DependentGuard() { enabled = CustomGuard::enabled; }
  19. ~DependentGuard() { enabled = false; }
  20. static const char *report_status() { return enabled ? "guarded" : "unguarded"; }
  21. };
  22. bool DependentGuard::enabled = false;
  23. TEST_SUBMODULE(call_policies, m) {
  24. // Parent/Child are used in:
  25. // test_keep_alive_argument, test_keep_alive_return_value, test_alive_gc_derived,
  26. // test_alive_gc_multi_derived, test_return_none, test_keep_alive_constructor
  27. class Child {
  28. public:
  29. Child() { py::print("Allocating child."); }
  30. ~Child() { py::print("Releasing child."); }
  31. };
  32. py::class_<Child>(m, "Child")
  33. .def(py::init<>());
  34. class Parent {
  35. public:
  36. Parent() { py::print("Allocating parent."); }
  37. ~Parent() { py::print("Releasing parent."); }
  38. void addChild(Child *) { }
  39. Child *returnChild() { return new Child(); }
  40. Child *returnNullChild() { return nullptr; }
  41. };
  42. py::class_<Parent>(m, "Parent")
  43. .def(py::init<>())
  44. .def(py::init([](Child *) { return new Parent(); }), py::keep_alive<1, 2>())
  45. .def("addChild", &Parent::addChild)
  46. .def("addChildKeepAlive", &Parent::addChild, py::keep_alive<1, 2>())
  47. .def("returnChild", &Parent::returnChild)
  48. .def("returnChildKeepAlive", &Parent::returnChild, py::keep_alive<1, 0>())
  49. .def("returnNullChildKeepAliveChild", &Parent::returnNullChild, py::keep_alive<1, 0>())
  50. .def("returnNullChildKeepAliveParent", &Parent::returnNullChild, py::keep_alive<0, 1>());
  51. // test_keep_alive_single
  52. m.def("add_patient", [](py::object /*nurse*/, py::object /*patient*/) { }, py::keep_alive<1, 2>());
  53. m.def("get_patients", [](py::object nurse) {
  54. py::list patients;
  55. for (PyObject *p : pybind11::detail::get_internals().patients[nurse.ptr()])
  56. patients.append(py::reinterpret_borrow<py::object>(p));
  57. return patients;
  58. });
  59. m.def("refcount", [](py::handle h) {
  60. #ifdef PYPY_VERSION
  61. ConstructorStats::gc(); // PyPy doesn't update ref counts until GC occurs
  62. #endif
  63. return h.ref_count();
  64. });
  65. #if !defined(PYPY_VERSION)
  66. // test_alive_gc
  67. class ParentGC : public Parent {
  68. public:
  69. using Parent::Parent;
  70. };
  71. py::class_<ParentGC, Parent>(m, "ParentGC", py::dynamic_attr())
  72. .def(py::init<>());
  73. #endif
  74. // test_call_guard
  75. m.def("unguarded_call", &CustomGuard::report_status);
  76. m.def("guarded_call", &CustomGuard::report_status, py::call_guard<CustomGuard>());
  77. m.def("multiple_guards_correct_order", []() {
  78. return CustomGuard::report_status() + std::string(" & ") + DependentGuard::report_status();
  79. }, py::call_guard<CustomGuard, DependentGuard>());
  80. m.def("multiple_guards_wrong_order", []() {
  81. return DependentGuard::report_status() + std::string(" & ") + CustomGuard::report_status();
  82. }, py::call_guard<DependentGuard, CustomGuard>());
  83. #if defined(WITH_THREAD) && !defined(PYPY_VERSION)
  84. // `py::call_guard<py::gil_scoped_release>()` should work in PyPy as well,
  85. // but it's unclear how to test it without `PyGILState_GetThisThreadState`.
  86. auto report_gil_status = []() {
  87. auto is_gil_held = false;
  88. if (auto tstate = py::detail::get_thread_state_unchecked())
  89. is_gil_held = (tstate == PyGILState_GetThisThreadState());
  90. return is_gil_held ? "GIL held" : "GIL released";
  91. };
  92. m.def("with_gil", report_gil_status);
  93. m.def("without_gil", report_gil_status, py::call_guard<py::gil_scoped_release>());
  94. #endif
  95. }