blob: cdf318c0fc14939bc8eb05ef94c2afef3713623a [file] [log] [blame]
Dean Moldovan67b52d82016-10-16 19:12:43 +02001Functions
2#########
3
4Before proceeding with this section, make sure that you are already familiar
5with the basics of binding functions and classes, as explained in :doc:`/basics`
6and :doc:`/classes`. The following guide is applicable to both free and member
7functions, i.e. *methods* in Python.
8
Jason Rhinelander17d02832017-01-16 20:35:14 -05009.. _return_value_policies:
10
Dean Moldovan67b52d82016-10-16 19:12:43 +020011Return value policies
12=====================
13
Wenzel Jakob6ba98652016-10-24 23:48:20 +020014Python and C++ use fundamentally different ways of managing the memory and
15lifetime of objects managed by them. This can lead to issues when creating
16bindings for functions that return a non-trivial type. Just by looking at the
17type information, it is not clear whether Python should take charge of the
18returned value and eventually free its resources, or if this is handled on the
Dean Moldovan57a9bbc2017-01-31 16:54:08 +010019C++ side. For this reason, pybind11 provides a several *return value policy*
Wenzel Jakob6ba98652016-10-24 23:48:20 +020020annotations that can be passed to the :func:`module::def` and
21:func:`class_::def` functions. The default policy is
22:enum:`return_value_policy::automatic`.
Dean Moldovan67b52d82016-10-16 19:12:43 +020023
Wenzel Jakob6ba98652016-10-24 23:48:20 +020024Return value policies are tricky, and it's very important to get them right.
25Just to illustrate what can go wrong, consider the following simple example:
Dean Moldovan67b52d82016-10-16 19:12:43 +020026
27.. code-block:: cpp
28
Dean Moldovan57a9bbc2017-01-31 16:54:08 +010029 /* Function declaration */
Wenzel Jakob6ba98652016-10-24 23:48:20 +020030 Data *get_data() { return _data; /* (pointer to a static data structure) */ }
31 ...
Dean Moldovan67b52d82016-10-16 19:12:43 +020032
Dean Moldovan57a9bbc2017-01-31 16:54:08 +010033 /* Binding code */
Wenzel Jakob6ba98652016-10-24 23:48:20 +020034 m.def("get_data", &get_data); // <-- KABOOM, will cause crash when called from Python
35
36What's going on here? When ``get_data()`` is called from Python, the return
37value (a native C++ type) must be wrapped to turn it into a usable Python type.
38In this case, the default return value policy (:enum:`return_value_policy::automatic`)
39causes pybind11 to assume ownership of the static ``_data`` instance.
40
41When Python's garbage collector eventually deletes the Python
42wrapper, pybind11 will also attempt to delete the C++ instance (via ``operator
43delete()``) due to the implied ownership. At this point, the entire application
44will come crashing down, though errors could also be more subtle and involve
45silent data corruption.
46
47In the above example, the policy :enum:`return_value_policy::reference` should have
48been specified so that the global data instance is only *referenced* without any
Dean Moldovan57a9bbc2017-01-31 16:54:08 +010049implied transfer of ownership, i.e.:
Wenzel Jakob6ba98652016-10-24 23:48:20 +020050
51.. code-block:: cpp
52
53 m.def("get_data", &get_data, return_value_policy::reference);
54
55On the other hand, this is not the right policy for many other situations,
56where ignoring ownership could lead to resource leaks.
57As a developer using pybind11, it's important to be familiar with the different
58return value policies, including which situation calls for which one of them.
59The following table provides an overview of available policies:
Dean Moldovan67b52d82016-10-16 19:12:43 +020060
61.. tabularcolumns:: |p{0.5\textwidth}|p{0.45\textwidth}|
62
63+--------------------------------------------------+----------------------------------------------------------------------------+
64| Return value policy | Description |
65+==================================================+============================================================================+
Dean Moldovan67b52d82016-10-16 19:12:43 +020066| :enum:`return_value_policy::take_ownership` | Reference an existing object (i.e. do not create a new copy) and take |
67| | ownership. Python will call the destructor and delete operator when the |
68| | object's reference count reaches zero. Undefined behavior ensues when the |
Wenzel Jakob6ba98652016-10-24 23:48:20 +020069| | C++ side does the same, or when the data was not dynamically allocated. |
Dean Moldovan67b52d82016-10-16 19:12:43 +020070+--------------------------------------------------+----------------------------------------------------------------------------+
71| :enum:`return_value_policy::copy` | Create a new copy of the returned object, which will be owned by Python. |
72| | This policy is comparably safe because the lifetimes of the two instances |
73| | are decoupled. |
74+--------------------------------------------------+----------------------------------------------------------------------------+
75| :enum:`return_value_policy::move` | Use ``std::move`` to move the return value contents into a new instance |
76| | that will be owned by Python. This policy is comparably safe because the |
77| | lifetimes of the two instances (move source and destination) are decoupled.|
78+--------------------------------------------------+----------------------------------------------------------------------------+
79| :enum:`return_value_policy::reference` | Reference an existing object, but do not take ownership. The C++ side is |
80| | responsible for managing the object's lifetime and deallocating it when |
81| | it is no longer used. Warning: undefined behavior will ensue when the C++ |
82| | side deletes an object that is still referenced and used by Python. |
83+--------------------------------------------------+----------------------------------------------------------------------------+
84| :enum:`return_value_policy::reference_internal` | Indicates that the lifetime of the return value is tied to the lifetime |
85| | of a parent object, namely the implicit ``this``, or ``self`` argument of |
86| | the called method or property. Internally, this policy works just like |
87| | :enum:`return_value_policy::reference` but additionally applies a |
88| | ``keep_alive<0, 1>`` *call policy* (described in the next section) that |
89| | prevents the parent object from being garbage collected as long as the |
90| | return value is referenced by Python. This is the default policy for |
91| | property getters created via ``def_property``, ``def_readwrite``, etc. |
92+--------------------------------------------------+----------------------------------------------------------------------------+
jbarlow837830e852017-01-13 02:17:29 -080093| :enum:`return_value_policy::automatic` | **Default policy.** This policy falls back to the policy |
Wenzel Jakob6ba98652016-10-24 23:48:20 +020094| | :enum:`return_value_policy::take_ownership` when the return value is a |
thorinke72eaa42017-02-17 12:57:39 +010095| | pointer. Otherwise, it uses :enum:`return_value_policy::move` or |
96| | :enum:`return_value_policy::copy` for rvalue and lvalue references, |
97| | respectively. See above for a description of what all of these different |
98| | policies do. |
Wenzel Jakob6ba98652016-10-24 23:48:20 +020099+--------------------------------------------------+----------------------------------------------------------------------------+
100| :enum:`return_value_policy::automatic_reference` | As above, but use policy :enum:`return_value_policy::reference` when the |
101| | return value is a pointer. This is the default conversion policy for |
102| | function arguments when calling Python functions manually from C++ code |
103| | (i.e. via handle::operator()). You probably won't need to use this. |
104+--------------------------------------------------+----------------------------------------------------------------------------+
105
Dean Moldovan03f627e2016-11-01 11:44:57 +0100106Return value policies can also be applied to properties:
Wenzel Jakob6ba98652016-10-24 23:48:20 +0200107
108.. code-block:: cpp
109
110 class_<MyClass>(m, "MyClass")
Dean Moldovan03f627e2016-11-01 11:44:57 +0100111 .def_property("data", &MyClass::getData, &MyClass::setData,
112 py::return_value_policy::copy);
113
114Technically, the code above applies the policy to both the getter and the
115setter function, however, the setter doesn't really care about *return*
116value policies which makes this a convenient terse syntax. Alternatively,
117targeted arguments can be passed through the :class:`cpp_function` constructor:
118
119.. code-block:: cpp
120
121 class_<MyClass>(m, "MyClass")
122 .def_property("data"
Wenzel Jakob6ba98652016-10-24 23:48:20 +0200123 py::cpp_function(&MyClass::getData, py::return_value_policy::copy),
124 py::cpp_function(&MyClass::setData)
125 );
Dean Moldovan67b52d82016-10-16 19:12:43 +0200126
127.. warning::
128
129 Code with invalid return value policies might access unitialized memory or
130 free data structures multiple times, which can lead to hard-to-debug
131 non-determinism and segmentation faults, hence it is worth spending the
132 time to understand all the different options in the table above.
133
Wenzel Jakob6ba98652016-10-24 23:48:20 +0200134.. note::
135
136 One important aspect of the above policies is that they only apply to
137 instances which pybind11 has *not* seen before, in which case the policy
138 clarifies essential questions about the return value's lifetime and
139 ownership. When pybind11 knows the instance already (as identified by its
140 type and address in memory), it will return the existing Python object
141 wrapper rather than creating a new copy.
Dean Moldovan67b52d82016-10-16 19:12:43 +0200142
143.. note::
144
145 The next section on :ref:`call_policies` discusses *call policies* that can be
146 specified *in addition* to a return value policy from the list above. Call
147 policies indicate reference relationships that can involve both return values
148 and parameters of functions.
149
150.. note::
151
152 As an alternative to elaborate call policies and lifetime management logic,
153 consider using smart pointers (see the section on :ref:`smart_pointers` for
154 details). Smart pointers can tell whether an object is still referenced from
155 C++ or Python, which generally eliminates the kinds of inconsistencies that
156 can lead to crashes or undefined behavior. For functions returning smart
157 pointers, it is not necessary to specify a return value policy.
158
159.. _call_policies:
160
161Additional call policies
162========================
163
Dean Moldovan57a9bbc2017-01-31 16:54:08 +0100164In addition to the above return value policies, further *call policies* can be
Dean Moldovan1ac19032017-03-16 11:22:26 +0100165specified to indicate dependencies between parameters or ensure a certain state
166for the function call.
jbarlow837830e852017-01-13 02:17:29 -0800167
Dean Moldovan1ac19032017-03-16 11:22:26 +0100168Keep alive
169----------
170
171In general, this policy is required when the C++ object is any kind of container
172and another object is being added to the container. ``keep_alive<Nurse, Patient>``
173indicates that the argument with index ``Patient`` should be kept alive at least
174until the argument with index ``Nurse`` is freed by the garbage collector. Argument
Dean Moldovan67b52d82016-10-16 19:12:43 +0200175indices start at one, while zero refers to the return value. For methods, index
176``1`` refers to the implicit ``this`` pointer, while regular arguments begin at
177index ``2``. Arbitrarily many call policies can be specified. When a ``Nurse``
178with value ``None`` is detected at runtime, the call policy does nothing.
179
Bruce Merryb490b442017-09-01 09:27:00 +0200180When the nurse is not a pybind11-registered type, the implementation internally
181relies on the ability to create a *weak reference* to the nurse object. When
182the nurse object is not a pybind11-registered type and does not support weak
183references, an exception will be thrown.
Dean Moldovan67b52d82016-10-16 19:12:43 +0200184
185Consider the following example: here, the binding code for a list append
186operation ties the lifetime of the newly added element to the underlying
187container:
188
189.. code-block:: cpp
190
191 py::class_<List>(m, "List")
192 .def("append", &List::append, py::keep_alive<1, 2>());
193
194.. note::
195
196 ``keep_alive`` is analogous to the ``with_custodian_and_ward`` (if Nurse,
197 Patient != 0) and ``with_custodian_and_ward_postcall`` (if Nurse/Patient ==
198 0) policies from Boost.Python.
199
Dean Moldovan1ac19032017-03-16 11:22:26 +0100200Call guard
201----------
202
203The ``call_guard<T>`` policy allows any scope guard type ``T`` to be placed
204around the function call. For example, this definition:
205
206.. code-block:: cpp
207
208 m.def("foo", foo, py::call_guard<T>());
209
210is equivalent to the following pseudocode:
211
212.. code-block:: cpp
213
214 m.def("foo", [](args...) {
215 T scope_guard;
216 return foo(args...); // forwarded arguments
217 });
218
219The only requirement is that ``T`` is default-constructible, but otherwise any
220scope guard will work. This is very useful in combination with `gil_scoped_release`.
221See :ref:`gil`.
222
223Multiple guards can also be specified as ``py::call_guard<T1, T2, T3...>``. The
224constructor order is left to right and destruction happens in reverse.
225
Dean Moldovan67b52d82016-10-16 19:12:43 +0200226.. seealso::
227
Dean Moldovan1ac19032017-03-16 11:22:26 +0100228 The file :file:`tests/test_call_policies.cpp` contains a complete example
229 that demonstrates using `keep_alive` and `call_guard` in more detail.
Dean Moldovan67b52d82016-10-16 19:12:43 +0200230
231.. _python_objects_as_args:
232
233Python objects as arguments
234===========================
235
236pybind11 exposes all major Python types using thin C++ wrapper classes. These
237wrapper classes can also be used as parameters of functions in bindings, which
238makes it possible to directly work with native Python types on the C++ side.
239For instance, the following statement iterates over a Python ``dict``:
240
241.. code-block:: cpp
242
243 void print_dict(py::dict dict) {
244 /* Easily interact with Python types */
245 for (auto item : dict)
myd73499b815ad2017-01-13 18:15:52 +0800246 std::cout << "key=" << std::string(py::str(item.first)) << ", "
247 << "value=" << std::string(py::str(item.second)) << std::endl;
Dean Moldovan67b52d82016-10-16 19:12:43 +0200248 }
249
250It can be exported:
251
252.. code-block:: cpp
253
254 m.def("print_dict", &print_dict);
255
256And used in Python as usual:
257
258.. code-block:: pycon
259
260 >>> print_dict({'foo': 123, 'bar': 'hello'})
261 key=foo, value=123
262 key=bar, value=hello
263
264For more information on using Python objects in C++, see :doc:`/advanced/pycpp/index`.
265
266Accepting \*args and \*\*kwargs
267===============================
268
269Python provides a useful mechanism to define functions that accept arbitrary
270numbers of arguments and keyword arguments:
271
272.. code-block:: python
273
274 def generic(*args, **kwargs):
275 ... # do something with args and kwargs
276
277Such functions can also be created using pybind11:
278
279.. code-block:: cpp
280
281 void generic(py::args args, py::kwargs kwargs) {
282 /// .. do something with args
283 if (kwargs)
284 /// .. do something with kwargs
285 }
286
287 /// Binding code
288 m.def("generic", &generic);
289
290The class ``py::args`` derives from ``py::tuple`` and ``py::kwargs`` derives
Jason Rhinelander2686da82017-01-21 23:42:14 -0500291from ``py::dict``.
Dean Moldovan67b52d82016-10-16 19:12:43 +0200292
Jason Rhinelander2686da82017-01-21 23:42:14 -0500293You may also use just one or the other, and may combine these with other
294arguments as long as the ``py::args`` and ``py::kwargs`` arguments are the last
295arguments accepted by the function.
Dean Moldovan67b52d82016-10-16 19:12:43 +0200296
Jason Rhinelander2686da82017-01-21 23:42:14 -0500297Please refer to the other examples for details on how to iterate over these,
298and on how to cast their entries into C++ objects. A demonstration is also
299available in ``tests/test_kwargs_and_defaults.cpp``.
300
301.. note::
302
303 When combining \*args or \*\*kwargs with :ref:`keyword_args` you should
304 *not* include ``py::arg`` tags for the ``py::args`` and ``py::kwargs``
305 arguments.
Dean Moldovan67b52d82016-10-16 19:12:43 +0200306
307Default arguments revisited
308===========================
309
310The section on :ref:`default_args` previously discussed basic usage of default
311arguments using pybind11. One noteworthy aspect of their implementation is that
312default arguments are converted to Python objects right at declaration time.
313Consider the following example:
314
315.. code-block:: cpp
316
317 py::class_<MyClass>("MyClass")
318 .def("myFunction", py::arg("arg") = SomeType(123));
319
320In this case, pybind11 must already be set up to deal with values of the type
321``SomeType`` (via a prior instantiation of ``py::class_<SomeType>``), or an
322exception will be thrown.
323
324Another aspect worth highlighting is that the "preview" of the default argument
325in the function signature is generated using the object's ``__repr__`` method.
326If not available, the signature may not be very helpful, e.g.:
327
328.. code-block:: pycon
329
330 FUNCTIONS
331 ...
332 | myFunction(...)
333 | Signature : (MyClass, arg : SomeType = <SomeType object at 0x101b7b080>) -> NoneType
334 ...
335
336The first way of addressing this is by defining ``SomeType.__repr__``.
337Alternatively, it is possible to specify the human-readable preview of the
338default argument manually using the ``arg_v`` notation:
339
340.. code-block:: cpp
341
342 py::class_<MyClass>("MyClass")
343 .def("myFunction", py::arg_v("arg", SomeType(123), "SomeType(123)"));
344
345Sometimes it may be necessary to pass a null pointer value as a default
346argument. In this case, remember to cast it to the underlying type in question,
347like so:
348
349.. code-block:: cpp
350
351 py::class_<MyClass>("MyClass")
352 .def("myFunction", py::arg("arg") = (SomeType *) nullptr);
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500353
Jason Rhinelander17d02832017-01-16 20:35:14 -0500354.. _nonconverting_arguments:
355
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500356Non-converting arguments
357========================
358
359Certain argument types may support conversion from one type to another. Some
360examples of conversions are:
361
362* :ref:`implicit_conversions` declared using ``py::implicitly_convertible<A,B>()``
363* Calling a method accepting a double with an integer argument
364* Calling a ``std::complex<float>`` argument with a non-complex python type
365 (for example, with a float). (Requires the optional ``pybind11/complex.h``
366 header).
367* Calling a function taking an Eigen matrix reference with a numpy array of the
368 wrong type or of an incompatible data layout. (Requires the optional
369 ``pybind11/eigen.h`` header).
370
371This behaviour is sometimes undesirable: the binding code may prefer to raise
372an error rather than convert the argument. This behaviour can be obtained
373through ``py::arg`` by calling the ``.noconvert()`` method of the ``py::arg``
374object, such as:
375
376.. code-block:: cpp
377
378 m.def("floats_only", [](double f) { return 0.5 * f; }, py::arg("f").noconvert());
379 m.def("floats_preferred", [](double f) { return 0.5 * f; }, py::arg("f"));
380
381Attempting the call the second function (the one without ``.noconvert()``) with
382an integer will succeed, but attempting to call the ``.noconvert()`` version
383will fail with a ``TypeError``:
384
385.. code-block:: pycon
386
387 >>> floats_preferred(4)
388 2.0
389 >>> floats_only(4)
390 Traceback (most recent call last):
391 File "<stdin>", line 1, in <module>
392 TypeError: floats_only(): incompatible function arguments. The following argument types are supported:
393 1. (f: float) -> float
394
395 Invoked with: 4
396
397You may, of course, combine this with the :var:`_a` shorthand notation (see
398:ref:`keyword_args`) and/or :ref:`default_args`. It is also permitted to omit
399the argument name by using the ``py::arg()`` constructor without an argument
400name, i.e. by specifying ``py::arg().noconvert()``.
401
402.. note::
403
404 When specifying ``py::arg`` options it is necessary to provide the same
405 number of options as the bound function has arguments. Thus if you want to
406 enable no-convert behaviour for just one of several arguments, you will
407 need to specify a ``py::arg()`` annotation for each argument with the
408 no-convert argument modified to ``py::arg().noconvert()``.
Jason Rhinelandere5505892017-02-03 18:25:34 -0500409
Dean Moldovandb46a892017-08-13 22:25:15 +0200410.. _none_arguments:
411
Jason Rhinelander4e1e4a52017-05-17 11:55:43 -0400412Allow/Prohibiting None arguments
413================================
414
415When a C++ type registered with :class:`py::class_` is passed as an argument to
416a function taking the instance as pointer or shared holder (e.g. ``shared_ptr``
417or a custom, copyable holder as described in :ref:`smart_pointers`), pybind
418allows ``None`` to be passed from Python which results in calling the C++
419function with ``nullptr`` (or an empty holder) for the argument.
420
421To explicitly enable or disable this behaviour, using the
422``.none`` method of the :class:`py::arg` object:
423
424.. code-block:: cpp
425
426 py::class_<Dog>(m, "Dog").def(py::init<>());
427 py::class_<Cat>(m, "Cat").def(py::init<>());
428 m.def("bark", [](Dog *dog) -> std::string {
429 if (dog) return "woof!"; /* Called with a Dog instance */
430 else return "(no dog)"; /* Called with None, d == nullptr */
431 }, py::arg("dog").none(true));
432 m.def("meow", [](Cat *cat) -> std::string {
433 // Can't be called with None argument
434 return "meow";
435 }, py::arg("cat").none(false));
436
437With the above, the Python call ``bark(None)`` will return the string ``"(no
Jason Rhinelander4f9ee6e2017-05-26 23:20:48 -0400438dog)"``, while attempting to call ``meow(None)`` will raise a ``TypeError``:
Jason Rhinelander4e1e4a52017-05-17 11:55:43 -0400439
440.. code-block:: pycon
441
442 >>> from animals import Dog, Cat, bark, meow
443 >>> bark(Dog())
444 'woof!'
445 >>> meow(Cat())
446 'meow'
447 >>> bark(None)
448 '(no dog)'
449 >>> meow(None)
450 Traceback (most recent call last):
451 File "<stdin>", line 1, in <module>
452 TypeError: meow(): incompatible function arguments. The following argument types are supported:
453 1. (cat: animals.Cat) -> str
454
455 Invoked with: None
456
457The default behaviour when the tag is unspecified is to allow ``None``.
458
Jason Rhinelandere5505892017-02-03 18:25:34 -0500459Overload resolution order
460=========================
461
462When a function or method with multiple overloads is called from Python,
463pybind11 determines which overload to call in two passes. The first pass
464attempts to call each overload without allowing argument conversion (as if
465every argument had been specified as ``py::arg().noconvert()`` as decribed
466above).
467
468If no overload succeeds in the no-conversion first pass, a second pass is
469attempted in which argument conversion is allowed (except where prohibited via
470an explicit ``py::arg().noconvert()`` attribute in the function definition).
471
472If the second pass also fails a ``TypeError`` is raised.
473
474Within each pass, overloads are tried in the order they were registered with
475pybind11.
476
477What this means in practice is that pybind11 will prefer any overload that does
478not require conversion of arguments to an overload that does, but otherwise prefers
479earlier-defined overloads to later-defined ones.
480
481.. note::
482
483 pybind11 does *not* further prioritize based on the number/pattern of
484 overloaded arguments. That is, pybind11 does not prioritize a function
485 requiring one conversion over one requiring three, but only prioritizes
486 overloads requiring no conversion at all to overloads that require
487 conversion of at least one argument.