blob: 65e54a0c5f13992a143f807e1d366bd56f165134 [file] [log] [blame]
Dean Moldovan1ac19032017-03-16 11:22:26 +01001/*
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 Moldovan1ac19032017-03-16 11:22:26 +010012struct 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 Moldovan1ac19032017-03-16 11:22:26 +010020bool CustomGuard::enabled = false;
21
22struct 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 Moldovan1ac19032017-03-16 11:22:26 +010030bool DependentGuard::enabled = false;
31
Jason Rhinelander391c7542017-07-25 16:47:36 -040032TEST_SUBMODULE(call_policies, m) {
33 // Parent/Child are used in:
34 // test_keep_alive_argument, test_keep_alive_return_value, test_alive_gc_derived,
35 // test_alive_gc_multi_derived, test_return_none
36 class Child {
37 public:
38 Child() { py::print("Allocating child."); }
39 ~Child() { py::print("Releasing child."); }
40 };
41 py::class_<Child>(m, "Child")
42 .def(py::init<>());
Dean Moldovan1ac19032017-03-16 11:22:26 +010043
Jason Rhinelander391c7542017-07-25 16:47:36 -040044 class Parent {
45 public:
46 Parent() { py::print("Allocating parent."); }
47 ~Parent() { py::print("Releasing parent."); }
48 void addChild(Child *) { }
49 Child *returnChild() { return new Child(); }
50 Child *returnNullChild() { return nullptr; }
51 };
52 py::class_<Parent>(m, "Parent")
53 .def(py::init<>())
54 .def("addChild", &Parent::addChild)
55 .def("addChildKeepAlive", &Parent::addChild, py::keep_alive<1, 2>())
56 .def("returnChild", &Parent::returnChild)
57 .def("returnChildKeepAlive", &Parent::returnChild, py::keep_alive<1, 0>())
58 .def("returnNullChildKeepAliveChild", &Parent::returnNullChild, py::keep_alive<1, 0>())
59 .def("returnNullChildKeepAliveParent", &Parent::returnNullChild, py::keep_alive<0, 1>());
60
61#if !defined(PYPY_VERSION)
62 // test_alive_gc
63 class ParentGC : public Parent {
64 public:
65 using Parent::Parent;
66 };
67 py::class_<ParentGC, Parent>(m, "ParentGC", py::dynamic_attr())
68 .def(py::init<>());
69#endif
70
71 // test_call_guard
Dean Moldovan1ac19032017-03-16 11:22:26 +010072 m.def("unguarded_call", &CustomGuard::report_status);
73 m.def("guarded_call", &CustomGuard::report_status, py::call_guard<CustomGuard>());
74
75 m.def("multiple_guards_correct_order", []() {
76 return CustomGuard::report_status() + std::string(" & ") + DependentGuard::report_status();
77 }, py::call_guard<CustomGuard, DependentGuard>());
78
79 m.def("multiple_guards_wrong_order", []() {
80 return DependentGuard::report_status() + std::string(" & ") + CustomGuard::report_status();
81 }, py::call_guard<DependentGuard, CustomGuard>());
82
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
91 return is_gil_held ? "GIL held" : "GIL released";
92 };
93
94 m.def("with_gil", report_gil_status);
95 m.def("without_gil", report_gil_status, py::call_guard<py::gil_scoped_release>());
96#endif
Jason Rhinelander391c7542017-07-25 16:47:36 -040097}