test_call_policies.cpp
Go to the documentation of this file.
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 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 };
20 bool CustomGuard::enabled = false;
21 
23  static bool enabled;
24 
26  ~DependentGuard() { enabled = false; }
27 
28  static const char *report_status() { return enabled ? "guarded" : "unguarded"; }
29 };
30 bool DependentGuard::enabled = false;
31 
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,
35  // test_alive_gc_multi_derived, test_return_none, test_keep_alive_constructor
36  class Child {
37  public:
38  Child() { py::print("Allocating child."); }
39  Child(const Child &) = default;
40  Child(Child &&) = default;
41  ~Child() { py::print("Releasing child."); }
42  };
43  py::class_<Child>(m, "Child").def(py::init<>());
44 
45  class Parent {
46  public:
47  Parent() { py::print("Allocating parent."); }
48  Parent(const Parent &parent) = default;
49  ~Parent() { py::print("Releasing parent."); }
50  void addChild(Child *) {}
51  Child *returnChild() { return new Child(); }
52  Child *returnNullChild() { return nullptr; }
53  static Child *staticFunction(Parent *) { return new Child(); }
54  };
55  py::class_<Parent>(m, "Parent")
56  .def(py::init<>())
57  .def(py::init([](Child *) { return new Parent(); }), py::keep_alive<1, 2>())
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  .def_static("staticFunction", &Parent::staticFunction, py::keep_alive<1, 0>());
65 
66  m.def(
67  "free_function", [](Parent *, Child *) {}, py::keep_alive<1, 2>());
68  m.def(
69  "invalid_arg_index", [] {}, py::keep_alive<0, 1>());
70 
71 #if !defined(PYPY_VERSION)
72  // test_alive_gc
73  class ParentGC : public Parent {
74  public:
75  using Parent::Parent;
76  };
77  py::class_<ParentGC, Parent>(m, "ParentGC", py::dynamic_attr()).def(py::init<>());
78 #endif
79 
80  // test_call_guard
81  m.def("unguarded_call", &CustomGuard::report_status);
82  m.def("guarded_call", &CustomGuard::report_status, py::call_guard<CustomGuard>());
83 
84  m.def(
85  "multiple_guards_correct_order",
86  []() {
87  return CustomGuard::report_status() + std::string(" & ")
89  },
90  py::call_guard<CustomGuard, DependentGuard>());
91 
92  m.def(
93  "multiple_guards_wrong_order",
94  []() {
95  return DependentGuard::report_status() + std::string(" & ")
97  },
98  py::call_guard<DependentGuard, CustomGuard>());
99 
100 #if defined(WITH_THREAD) && !defined(PYPY_VERSION)
101  // `py::call_guard<py::gil_scoped_release>()` should work in PyPy as well,
102  // but it's unclear how to test it without `PyGILState_GetThisThreadState`.
103  auto report_gil_status = []() {
104  auto is_gil_held = false;
105  if (auto *tstate = py::detail::get_thread_state_unchecked()) {
106  is_gil_held = (tstate == PyGILState_GetThisThreadState());
107  }
108 
109  return is_gil_held ? "GIL held" : "GIL released";
110  };
111 
112  m.def("with_gil", report_gil_status);
113  m.def("without_gil", report_gil_status, py::call_guard<py::gil_scoped_release>());
114 #endif
115 }
Matrix3f m
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
TEST_SUBMODULE(call_policies, m)
static const char * report_status()
static const char * report_status()
static bool enabled
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Definition: pybind11.h:1882
PyThreadState * get_thread_state_unchecked()


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:45