Dean Moldovan | 1ac1903 | 2017-03-16 11:22:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | tests/test_call_policies.cpp -- keep_alive and call_guard |
| 3 | |
| 4 | Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> |
| 5 | |
| 6 | All rights reserved. Use of this source code is governed by a |
| 7 | BSD-style license that can be found in the LICENSE file. |
| 8 | */ |
| 9 | |
| 10 | #include "pybind11_tests.h" |
| 11 | |
Dean Moldovan | 1ac1903 | 2017-03-16 11:22:26 +0100 | [diff] [blame] | 12 | struct CustomGuard { |
| 13 | static bool enabled; |
| 14 | |
| 15 | CustomGuard() { enabled = true; } |
| 16 | ~CustomGuard() { enabled = false; } |
| 17 | |
| 18 | static const char *report_status() { return enabled ? "guarded" : "unguarded"; } |
| 19 | }; |
Dean Moldovan | 1ac1903 | 2017-03-16 11:22:26 +0100 | [diff] [blame] | 20 | bool CustomGuard::enabled = false; |
| 21 | |
| 22 | struct DependentGuard { |
| 23 | static bool enabled; |
| 24 | |
| 25 | DependentGuard() { enabled = CustomGuard::enabled; } |
| 26 | ~DependentGuard() { enabled = false; } |
| 27 | |
| 28 | static const char *report_status() { return enabled ? "guarded" : "unguarded"; } |
| 29 | }; |
Dean Moldovan | 1ac1903 | 2017-03-16 11:22:26 +0100 | [diff] [blame] | 30 | bool DependentGuard::enabled = false; |
| 31 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 32 | TEST_SUBMODULE(call_policies, m) { |
| 33 | // Parent/Child are used in: |
| 34 | // test_keep_alive_argument, test_keep_alive_return_value, test_alive_gc_derived, |
Dean Moldovan | 7939f4b | 2017-09-04 13:49:19 +0200 | [diff] [blame] | 35 | // test_alive_gc_multi_derived, test_return_none, test_keep_alive_constructor |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 36 | class Child { |
| 37 | public: |
| 38 | Child() { py::print("Allocating child."); } |
Francesco Biscani | ba33b2f | 2017-11-20 14:19:53 +0100 | [diff] [blame] | 39 | Child(const Child &) = default; |
| 40 | Child(Child &&) = default; |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 41 | ~Child() { py::print("Releasing child."); } |
| 42 | }; |
| 43 | py::class_<Child>(m, "Child") |
| 44 | .def(py::init<>()); |
Dean Moldovan | 1ac1903 | 2017-03-16 11:22:26 +0100 | [diff] [blame] | 45 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 46 | class Parent { |
| 47 | public: |
| 48 | Parent() { py::print("Allocating parent."); } |
Henry Schreiner | e428a7f | 2020-07-23 21:16:54 -0400 | [diff] [blame] | 49 | Parent(const Parent& parent) = default; |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 50 | ~Parent() { py::print("Releasing parent."); } |
| 51 | void addChild(Child *) { } |
| 52 | Child *returnChild() { return new Child(); } |
| 53 | Child *returnNullChild() { return nullptr; } |
| 54 | }; |
| 55 | py::class_<Parent>(m, "Parent") |
| 56 | .def(py::init<>()) |
Dean Moldovan | 7939f4b | 2017-09-04 13:49:19 +0200 | [diff] [blame] | 57 | .def(py::init([](Child *) { return new Parent(); }), py::keep_alive<1, 2>()) |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 58 | .def("addChild", &Parent::addChild) |
| 59 | .def("addChildKeepAlive", &Parent::addChild, py::keep_alive<1, 2>()) |
| 60 | .def("returnChild", &Parent::returnChild) |
| 61 | .def("returnChildKeepAlive", &Parent::returnChild, py::keep_alive<1, 0>()) |
| 62 | .def("returnNullChildKeepAliveChild", &Parent::returnNullChild, py::keep_alive<1, 0>()) |
| 63 | .def("returnNullChildKeepAliveParent", &Parent::returnNullChild, py::keep_alive<0, 1>()); |
| 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 | |
| 75 | // test_call_guard |
Dean Moldovan | 1ac1903 | 2017-03-16 11:22:26 +0100 | [diff] [blame] | 76 | m.def("unguarded_call", &CustomGuard::report_status); |
| 77 | m.def("guarded_call", &CustomGuard::report_status, py::call_guard<CustomGuard>()); |
| 78 | |
| 79 | m.def("multiple_guards_correct_order", []() { |
| 80 | return CustomGuard::report_status() + std::string(" & ") + DependentGuard::report_status(); |
| 81 | }, py::call_guard<CustomGuard, DependentGuard>()); |
| 82 | |
| 83 | m.def("multiple_guards_wrong_order", []() { |
| 84 | return DependentGuard::report_status() + std::string(" & ") + CustomGuard::report_status(); |
| 85 | }, py::call_guard<DependentGuard, CustomGuard>()); |
| 86 | |
| 87 | #if defined(WITH_THREAD) && !defined(PYPY_VERSION) |
| 88 | // `py::call_guard<py::gil_scoped_release>()` should work in PyPy as well, |
| 89 | // but it's unclear how to test it without `PyGILState_GetThisThreadState`. |
| 90 | auto report_gil_status = []() { |
| 91 | auto is_gil_held = false; |
| 92 | if (auto tstate = py::detail::get_thread_state_unchecked()) |
| 93 | is_gil_held = (tstate == PyGILState_GetThisThreadState()); |
| 94 | |
| 95 | return is_gil_held ? "GIL held" : "GIL released"; |
| 96 | }; |
| 97 | |
| 98 | m.def("with_gil", report_gil_status); |
| 99 | m.def("without_gil", report_gil_status, py::call_guard<py::gil_scoped_release>()); |
| 100 | #endif |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 101 | } |