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