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 | |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 27 | #if !defined(PYPY_VERSION) |
| 28 | class ParentGC : public Parent { |
| 29 | public: |
| 30 | using Parent::Parent; |
| 31 | }; |
| 32 | #endif |
| 33 | |
Dean Moldovan | 1ac1903 | 2017-03-16 11:22:26 +0100 | [diff] [blame] | 34 | test_initializer keep_alive([](py::module &m) { |
| 35 | py::class_<Parent>(m, "Parent") |
| 36 | .def(py::init<>()) |
| 37 | .def("addChild", &Parent::addChild) |
| 38 | .def("addChildKeepAlive", &Parent::addChild, py::keep_alive<1, 2>()) |
| 39 | .def("returnChild", &Parent::returnChild) |
| 40 | .def("returnChildKeepAlive", &Parent::returnChild, py::keep_alive<1, 0>()) |
| 41 | .def("returnNullChildKeepAliveChild", &Parent::returnNullChild, py::keep_alive<1, 0>()) |
| 42 | .def("returnNullChildKeepAliveParent", &Parent::returnNullChild, py::keep_alive<0, 1>()); |
| 43 | |
Bruce Merry | 9d698f7 | 2017-06-24 14:58:42 +0200 | [diff] [blame] | 44 | #if !defined(PYPY_VERSION) |
| 45 | py::class_<ParentGC, Parent>(m, "ParentGC", py::dynamic_attr()) |
| 46 | .def(py::init<>()); |
| 47 | #endif |
| 48 | |
Dean Moldovan | 1ac1903 | 2017-03-16 11:22:26 +0100 | [diff] [blame] | 49 | py::class_<Child>(m, "Child") |
| 50 | .def(py::init<>()); |
| 51 | }); |
| 52 | |
| 53 | struct CustomGuard { |
| 54 | static bool enabled; |
| 55 | |
| 56 | CustomGuard() { enabled = true; } |
| 57 | ~CustomGuard() { enabled = false; } |
| 58 | |
| 59 | static const char *report_status() { return enabled ? "guarded" : "unguarded"; } |
| 60 | }; |
| 61 | |
| 62 | bool CustomGuard::enabled = false; |
| 63 | |
| 64 | struct DependentGuard { |
| 65 | static bool enabled; |
| 66 | |
| 67 | DependentGuard() { enabled = CustomGuard::enabled; } |
| 68 | ~DependentGuard() { enabled = false; } |
| 69 | |
| 70 | static const char *report_status() { return enabled ? "guarded" : "unguarded"; } |
| 71 | }; |
| 72 | |
| 73 | bool DependentGuard::enabled = false; |
| 74 | |
| 75 | test_initializer call_guard([](py::module &pm) { |
| 76 | auto m = pm.def_submodule("call_policies"); |
| 77 | |
| 78 | m.def("unguarded_call", &CustomGuard::report_status); |
| 79 | m.def("guarded_call", &CustomGuard::report_status, py::call_guard<CustomGuard>()); |
| 80 | |
| 81 | m.def("multiple_guards_correct_order", []() { |
| 82 | return CustomGuard::report_status() + std::string(" & ") + DependentGuard::report_status(); |
| 83 | }, py::call_guard<CustomGuard, DependentGuard>()); |
| 84 | |
| 85 | m.def("multiple_guards_wrong_order", []() { |
| 86 | return DependentGuard::report_status() + std::string(" & ") + CustomGuard::report_status(); |
| 87 | }, py::call_guard<DependentGuard, CustomGuard>()); |
| 88 | |
| 89 | #if defined(WITH_THREAD) && !defined(PYPY_VERSION) |
| 90 | // `py::call_guard<py::gil_scoped_release>()` should work in PyPy as well, |
| 91 | // but it's unclear how to test it without `PyGILState_GetThisThreadState`. |
| 92 | auto report_gil_status = []() { |
| 93 | auto is_gil_held = false; |
| 94 | if (auto tstate = py::detail::get_thread_state_unchecked()) |
| 95 | is_gil_held = (tstate == PyGILState_GetThisThreadState()); |
| 96 | |
| 97 | return is_gil_held ? "GIL held" : "GIL released"; |
| 98 | }; |
| 99 | |
| 100 | m.def("with_gil", report_gil_status); |
| 101 | m.def("without_gil", report_gil_status, py::call_guard<py::gil_scoped_release>()); |
| 102 | #endif |
| 103 | }); |