38 Child() {
py::print(
"Allocating child."); }
39 Child(
const Child &) =
default;
40 Child(Child &&) =
default;
41 ~Child() {
py::print(
"Releasing child."); }
43 py::class_<Child>(
m,
"Child").def(py::init<>());
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(); }
55 py::class_<Parent>(
m,
"Parent")
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>());
66 m.def(
"free_function", [](Parent *, Child *) {}, py::keep_alive<1, 2>());
67 m.def(
"invalid_arg_index", [] {}, py::keep_alive<0, 1>());
69 #if !defined(PYPY_VERSION)
71 class ParentGC :
public Parent {
75 py::class_<ParentGC, Parent>(
m,
"ParentGC", py::dynamic_attr()).def(py::init<>());
83 "multiple_guards_correct_order",
88 py::call_guard<CustomGuard, DependentGuard>());
91 "multiple_guards_wrong_order",
96 py::call_guard<DependentGuard, CustomGuard>());
98 #if !defined(PYPY_VERSION)
101 auto report_gil_status = []() {
102 auto is_gil_held =
false;
104 is_gil_held = (tstate == PyGILState_GetThisThreadState());
107 return is_gil_held ?
"GIL held" :
"GIL released";
110 m.def(
"with_gil", report_gil_status);
111 m.def(
"without_gil", report_gil_status, py::call_guard<py::gil_scoped_release>());