Scarab  v2.9.1
Project 8 C++ Utility Library
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 
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")
44  .def(py::init<>());
45 
46  class Parent {
47  public:
48  Parent() { py::print("Allocating parent."); }
49  ~Parent() { py::print("Releasing parent."); }
50  void addChild(Child *) { }
51  Child *returnChild() { return new Child(); }
52  Child *returnNullChild() { return nullptr; }
53  };
54  py::class_<Parent>(m, "Parent")
55  .def(py::init<>())
56  .def(py::init([](Child *) { return new Parent(); }), py::keep_alive<1, 2>())
57  .def("addChild", &Parent::addChild)
58  .def("addChildKeepAlive", &Parent::addChild, py::keep_alive<1, 2>())
59  .def("returnChild", &Parent::returnChild)
60  .def("returnChildKeepAlive", &Parent::returnChild, py::keep_alive<1, 0>())
61  .def("returnNullChildKeepAliveChild", &Parent::returnNullChild, py::keep_alive<1, 0>())
62  .def("returnNullChildKeepAliveParent", &Parent::returnNullChild, py::keep_alive<0, 1>());
63 
64 #if !defined(PYPY_VERSION)
65  // test_alive_gc
66  class ParentGC : public Parent {
67  public:
68  using Parent::Parent;
69  };
71  .def(py::init<>());
72 #endif
73 
74  // test_call_guard
75  m.def("unguarded_call", &CustomGuard::report_status);
77 
78  m.def("multiple_guards_correct_order", []() {
79  return CustomGuard::report_status() + std::string(" & ") + DependentGuard::report_status();
81 
82  m.def("multiple_guards_wrong_order", []() {
83  return DependentGuard::report_status() + std::string(" & ") + CustomGuard::report_status();
85 
86 #if defined(WITH_THREAD) && !defined(PYPY_VERSION)
87  // `py::call_guard<py::gil_scoped_release>()` should work in PyPy as well,
88  // but it's unclear how to test it without `PyGILState_GetThisThreadState`.
89  auto report_gil_status = []() {
90  auto is_gil_held = false;
91  if (auto tstate = py::detail::get_thread_state_unchecked())
92  is_gil_held = (tstate == PyGILState_GetThisThreadState());
93 
94  return is_gil_held ? "GIL held" : "GIL released";
95  };
96 
97  m.def("with_gil", report_gil_status);
98  m.def("without_gil", report_gil_status, py::call_guard<py::gil_scoped_release>());
99 #endif
100 }
test_initializer call_policies("call_policies", test_submodule_call_policies)
PyThreadState * get_thread_state_unchecked()
Definition: cast.h:465
static bool enabled
Annotation which enables dynamic attributes, i.e. adds __dict__ to a class.
Definition: attr.h:51
static const char * report_status()
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Definition: pybind11.h:1371
Keep patient alive while nurse lives.
Definition: attr.h:45
static const char * report_status()
void print(Args &&...args)
Definition: pybind11.h:1849
#define TEST_SUBMODULE(name, variable)
bool typename Extra class_ & def(const char *name_, Func &&f, const Extra &... extra)
Definition: pybind11.h:1110