blob: 15275923eb25a069a885f2d59a845945521f4337 [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
12class Child {
13public:
14 Child() { py::print("Allocating child."); }
15 ~Child() { py::print("Releasing child."); }
16};
17
18class Parent {
19public:
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 Merry9d698f72017-06-24 14:58:42 +020027#if !defined(PYPY_VERSION)
28class ParentGC : public Parent {
29public:
30 using Parent::Parent;
31};
32#endif
33
Dean Moldovan1ac19032017-03-16 11:22:26 +010034test_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 Merry9d698f72017-06-24 14:58:42 +020044#if !defined(PYPY_VERSION)
45 py::class_<ParentGC, Parent>(m, "ParentGC", py::dynamic_attr())
46 .def(py::init<>());
47#endif
48
Dean Moldovan1ac19032017-03-16 11:22:26 +010049 py::class_<Child>(m, "Child")
50 .def(py::init<>());
51});
52
53struct 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
62bool CustomGuard::enabled = false;
63
64struct 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
73bool DependentGuard::enabled = false;
74
75test_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});