Skip to content

Commit 29623ba

Browse files
committed
Add documentation and rename test_keep_alive to test_call_policies
1 parent 3f84dfc commit 29623ba

File tree

6 files changed

+62
-12
lines changed

6 files changed

+62
-12
lines changed

docs/advanced/functions.rst

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,16 @@ Additional call policies
162162
========================
163163

164164
In addition to the above return value policies, further *call policies* can be
165-
specified to indicate dependencies between parameters. In general, call policies
166-
are required when the C++ object is any kind of container and another object is being
167-
added to the container.
168-
169-
There is currently just
170-
one policy named ``keep_alive<Nurse, Patient>``, which indicates that the
171-
argument with index ``Patient`` should be kept alive at least until the
172-
argument with index ``Nurse`` is freed by the garbage collector. Argument
165+
specified to indicate dependencies between parameters or ensure a certain state
166+
for the function call.
167+
168+
Keep alive
169+
----------
170+
171+
In general, this policy is required when the C++ object is any kind of container
172+
and another object is being added to the container. ``keep_alive<Nurse, Patient>``
173+
indicates that the argument with index ``Patient`` should be kept alive at least
174+
until the argument with index ``Nurse`` is freed by the garbage collector. Argument
173175
indices start at one, while zero refers to the return value. For methods, index
174176
``1`` refers to the implicit ``this`` pointer, while regular arguments begin at
175177
index ``2``. Arbitrarily many call policies can be specified. When a ``Nurse``
@@ -194,10 +196,33 @@ container:
194196
Patient != 0) and ``with_custodian_and_ward_postcall`` (if Nurse/Patient ==
195197
0) policies from Boost.Python.
196198

199+
Call guard
200+
----------
201+
202+
The ``call_guard<T>`` policy allows any scope guard type ``T`` to be placed
203+
around the function call. For example, this definition:
204+
205+
.. code-block:: cpp
206+
207+
m.def("foo", foo, py::call_guard<T>());
208+
209+
is equivalent to the following pseudocode:
210+
211+
.. code-block:: cpp
212+
213+
m.def("foo", [](args...) {
214+
T scope_guard;
215+
return foo(args...); // forwarded arguments
216+
});
217+
218+
The only requirement is that ``T`` is default-constructible, but otherwise any
219+
scope guard will work. This is very useful in combination with `gil_scoped_release`.
220+
See :ref:`gil`.
221+
197222
.. seealso::
198223

199-
The file :file:`tests/test_keep_alive.cpp` contains a complete example
200-
that demonstrates using :class:`keep_alive` in more detail.
224+
The file :file:`tests/test_call_policies.cpp` contains a complete example
225+
that demonstrates using `keep_alive` and `call_guard` in more detail.
201226

202227
.. _python_objects_as_args:
203228

docs/advanced/misc.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ T2>, myFunc)``. In this case, the preprocessor assumes that the comma indicates
1515
the beginning of the next parameter. Use a ``typedef`` to bind the template to
1616
another name and use it in the macro to avoid this problem.
1717

18+
.. _gil:
1819

1920
Global Interpreter Lock (GIL)
2021
=============================
@@ -68,6 +69,13 @@ could be realized as follows (important changes highlighted):
6869
return m.ptr();
6970
}
7071
72+
The ``call_go`` wrapper can also be simplified using the `call_guard` policy
73+
(see :ref:`call_policies`) which yields the same result:
74+
75+
.. code-block:: cpp
76+
77+
m.def("call_go", &call_go, py::call_guard<py::gil_scoped_release>());
78+
7179
7280
Binding sequence data types, iterators, the slicing protocol, etc.
7381
==================================================================

include/pybind11/attr.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,24 @@ struct metaclass {
6767
/// Annotation to mark enums as an arithmetic type
6868
struct arithmetic { };
6969

70-
/// A call policy which places a guard variable (of type T) around the function call
70+
/** \rst
71+
A call policy which places a guard variable (of type T) around the function call.
72+
73+
For example, this definition:
74+
75+
.. code-block:: cpp
76+
77+
m.def("foo", foo, py::call_guard<T>());
78+
79+
is equivalent to the following pseudocode:
80+
81+
.. code-block:: cpp
82+
83+
m.def("foo", [](args...) {
84+
T scope_guard;
85+
return foo(args...); // forwarded arguments
86+
});
87+
\endrst */
7188
template <typename T>
7289
struct call_guard {
7390
static_assert(std::is_default_constructible<T>::value,

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ set(PYBIND11_TEST_FILES
4040
test_exceptions.cpp
4141
test_inheritance.cpp
4242
test_issues.cpp
43-
test_keep_alive.cpp
43+
test_call_policies.cpp
4444
test_kwargs_and_defaults.cpp
4545
test_methods_and_attributes.cpp
4646
test_modules.cpp
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)