@@ -162,14 +162,16 @@ Additional call policies
162
162
========================
163
163
164
164
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
173
175
indices start at one, while zero refers to the return value. For methods, index
174
176
``1 `` refers to the implicit ``this `` pointer, while regular arguments begin at
175
177
index ``2 ``. Arbitrarily many call policies can be specified. When a ``Nurse ``
@@ -194,10 +196,33 @@ container:
194
196
Patient != 0) and ``with_custodian_and_ward_postcall `` (if Nurse/Patient ==
195
197
0) policies from Boost.Python.
196
198
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
+
197
222
.. seealso ::
198
223
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.
201
226
202
227
.. _python_objects_as_args :
203
228
0 commit comments