blob: d9886e2a2837d7b47dd89eae6c26c441ecce3075 [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
9Return value policies
10=====================
11
Wenzel Jakob6ba98652016-10-24 23:48:20 +020012Python and C++ use fundamentally different ways of managing the memory and
13lifetime of objects managed by them. This can lead to issues when creating
14bindings for functions that return a non-trivial type. Just by looking at the
15type information, it is not clear whether Python should take charge of the
16returned value and eventually free its resources, or if this is handled on the
17C++ side. For this reason, pybind11 provides a several `return value policy`
18annotations that can be passed to the :func:`module::def` and
19:func:`class_::def` functions. The default policy is
20:enum:`return_value_policy::automatic`.
Dean Moldovan67b52d82016-10-16 19:12:43 +020021
Wenzel Jakob6ba98652016-10-24 23:48:20 +020022Return value policies are tricky, and it's very important to get them right.
23Just to illustrate what can go wrong, consider the following simple example:
Dean Moldovan67b52d82016-10-16 19:12:43 +020024
25.. code-block:: cpp
26
Wenzel Jakob6ba98652016-10-24 23:48:20 +020027 /* Function declaration */
28 Data *get_data() { return _data; /* (pointer to a static data structure) */ }
29 ...
Dean Moldovan67b52d82016-10-16 19:12:43 +020030
Wenzel Jakob6ba98652016-10-24 23:48:20 +020031 /* Binding code */
32 m.def("get_data", &get_data); // <-- KABOOM, will cause crash when called from Python
33
34What's going on here? When ``get_data()`` is called from Python, the return
35value (a native C++ type) must be wrapped to turn it into a usable Python type.
36In this case, the default return value policy (:enum:`return_value_policy::automatic`)
37causes pybind11 to assume ownership of the static ``_data`` instance.
38
39When Python's garbage collector eventually deletes the Python
40wrapper, pybind11 will also attempt to delete the C++ instance (via ``operator
41delete()``) due to the implied ownership. At this point, the entire application
42will come crashing down, though errors could also be more subtle and involve
43silent data corruption.
44
45In the above example, the policy :enum:`return_value_policy::reference` should have
46been specified so that the global data instance is only *referenced* without any
47implied transfer of ownership, i.e.:
48
49.. code-block:: cpp
50
51 m.def("get_data", &get_data, return_value_policy::reference);
52
53On the other hand, this is not the right policy for many other situations,
54where ignoring ownership could lead to resource leaks.
55As a developer using pybind11, it's important to be familiar with the different
56return value policies, including which situation calls for which one of them.
57The following table provides an overview of available policies:
Dean Moldovan67b52d82016-10-16 19:12:43 +020058
59.. tabularcolumns:: |p{0.5\textwidth}|p{0.45\textwidth}|
60
61+--------------------------------------------------+----------------------------------------------------------------------------+
62| Return value policy | Description |
63+==================================================+============================================================================+
Dean Moldovan67b52d82016-10-16 19:12:43 +020064| :enum:`return_value_policy::take_ownership` | Reference an existing object (i.e. do not create a new copy) and take |
65| | ownership. Python will call the destructor and delete operator when the |
66| | object's reference count reaches zero. Undefined behavior ensues when the |
Wenzel Jakob6ba98652016-10-24 23:48:20 +020067| | C++ side does the same, or when the data was not dynamically allocated. |
Dean Moldovan67b52d82016-10-16 19:12:43 +020068+--------------------------------------------------+----------------------------------------------------------------------------+
69| :enum:`return_value_policy::copy` | Create a new copy of the returned object, which will be owned by Python. |
70| | This policy is comparably safe because the lifetimes of the two instances |
71| | are decoupled. |
72+--------------------------------------------------+----------------------------------------------------------------------------+
73| :enum:`return_value_policy::move` | Use ``std::move`` to move the return value contents into a new instance |
74| | that will be owned by Python. This policy is comparably safe because the |
75| | lifetimes of the two instances (move source and destination) are decoupled.|
76+--------------------------------------------------+----------------------------------------------------------------------------+
77| :enum:`return_value_policy::reference` | Reference an existing object, but do not take ownership. The C++ side is |
78| | responsible for managing the object's lifetime and deallocating it when |
79| | it is no longer used. Warning: undefined behavior will ensue when the C++ |
80| | side deletes an object that is still referenced and used by Python. |
81+--------------------------------------------------+----------------------------------------------------------------------------+
82| :enum:`return_value_policy::reference_internal` | Indicates that the lifetime of the return value is tied to the lifetime |
83| | of a parent object, namely the implicit ``this``, or ``self`` argument of |
84| | the called method or property. Internally, this policy works just like |
85| | :enum:`return_value_policy::reference` but additionally applies a |
86| | ``keep_alive<0, 1>`` *call policy* (described in the next section) that |
87| | prevents the parent object from being garbage collected as long as the |
88| | return value is referenced by Python. This is the default policy for |
89| | property getters created via ``def_property``, ``def_readwrite``, etc. |
90+--------------------------------------------------+----------------------------------------------------------------------------+
Wenzel Jakob6ba98652016-10-24 23:48:20 +020091| :enum:`return_value_policy::automatic` | This is the default return value policy, which falls back to the policy |
92| | :enum:`return_value_policy::take_ownership` when the return value is a |
93| | pointer. Otherwise, it uses :enum:`return_value::move` or |
94| | :enum:`return_value::copy` for rvalue and lvalue references, respectively. |
95| | See above for a description of what all of these different policies do. |
96+--------------------------------------------------+----------------------------------------------------------------------------+
97| :enum:`return_value_policy::automatic_reference` | As above, but use policy :enum:`return_value_policy::reference` when the |
98| | return value is a pointer. This is the default conversion policy for |
99| | function arguments when calling Python functions manually from C++ code |
100| | (i.e. via handle::operator()). You probably won't need to use this. |
101+--------------------------------------------------+----------------------------------------------------------------------------+
102
103Return value policies can also be applied to properties, in which case the
104arguments must be passed through the :class:`cpp_function` constructor:
105
106.. code-block:: cpp
107
108 class_<MyClass>(m, "MyClass")
109 def_property("data"
110 py::cpp_function(&MyClass::getData, py::return_value_policy::copy),
111 py::cpp_function(&MyClass::setData)
112 );
Dean Moldovan67b52d82016-10-16 19:12:43 +0200113
114.. warning::
115
116 Code with invalid return value policies might access unitialized memory or
117 free data structures multiple times, which can lead to hard-to-debug
118 non-determinism and segmentation faults, hence it is worth spending the
119 time to understand all the different options in the table above.
120
Wenzel Jakob6ba98652016-10-24 23:48:20 +0200121.. note::
122
123 One important aspect of the above policies is that they only apply to
124 instances which pybind11 has *not* seen before, in which case the policy
125 clarifies essential questions about the return value's lifetime and
126 ownership. When pybind11 knows the instance already (as identified by its
127 type and address in memory), it will return the existing Python object
128 wrapper rather than creating a new copy.
Dean Moldovan67b52d82016-10-16 19:12:43 +0200129
130.. note::
131
132 The next section on :ref:`call_policies` discusses *call policies* that can be
133 specified *in addition* to a return value policy from the list above. Call
134 policies indicate reference relationships that can involve both return values
135 and parameters of functions.
136
137.. note::
138
139 As an alternative to elaborate call policies and lifetime management logic,
140 consider using smart pointers (see the section on :ref:`smart_pointers` for
141 details). Smart pointers can tell whether an object is still referenced from
142 C++ or Python, which generally eliminates the kinds of inconsistencies that
143 can lead to crashes or undefined behavior. For functions returning smart
144 pointers, it is not necessary to specify a return value policy.
145
146.. _call_policies:
147
148Additional call policies
149========================
150
151In addition to the above return value policies, further `call policies` can be
152specified to indicate dependencies between parameters. There is currently just
153one policy named ``keep_alive<Nurse, Patient>``, which indicates that the
154argument with index ``Patient`` should be kept alive at least until the
155argument with index ``Nurse`` is freed by the garbage collector. Argument
156indices start at one, while zero refers to the return value. For methods, index
157``1`` refers to the implicit ``this`` pointer, while regular arguments begin at
158index ``2``. Arbitrarily many call policies can be specified. When a ``Nurse``
159with value ``None`` is detected at runtime, the call policy does nothing.
160
161This feature internally relies on the ability to create a *weak reference* to
162the nurse object, which is permitted by all classes exposed via pybind11. When
163the nurse object does not support weak references, an exception will be thrown.
164
165Consider the following example: here, the binding code for a list append
166operation ties the lifetime of the newly added element to the underlying
167container:
168
169.. code-block:: cpp
170
171 py::class_<List>(m, "List")
172 .def("append", &List::append, py::keep_alive<1, 2>());
173
174.. note::
175
176 ``keep_alive`` is analogous to the ``with_custodian_and_ward`` (if Nurse,
177 Patient != 0) and ``with_custodian_and_ward_postcall`` (if Nurse/Patient ==
178 0) policies from Boost.Python.
179
180.. seealso::
181
182 The file :file:`tests/test_keep_alive.cpp` contains a complete example
183 that demonstrates using :class:`keep_alive` in more detail.
184
185.. _python_objects_as_args:
186
187Python objects as arguments
188===========================
189
190pybind11 exposes all major Python types using thin C++ wrapper classes. These
191wrapper classes can also be used as parameters of functions in bindings, which
192makes it possible to directly work with native Python types on the C++ side.
193For instance, the following statement iterates over a Python ``dict``:
194
195.. code-block:: cpp
196
197 void print_dict(py::dict dict) {
198 /* Easily interact with Python types */
199 for (auto item : dict)
200 std::cout << "key=" << item.first << ", "
201 << "value=" << item.second << std::endl;
202 }
203
204It can be exported:
205
206.. code-block:: cpp
207
208 m.def("print_dict", &print_dict);
209
210And used in Python as usual:
211
212.. code-block:: pycon
213
214 >>> print_dict({'foo': 123, 'bar': 'hello'})
215 key=foo, value=123
216 key=bar, value=hello
217
218For more information on using Python objects in C++, see :doc:`/advanced/pycpp/index`.
219
220Accepting \*args and \*\*kwargs
221===============================
222
223Python provides a useful mechanism to define functions that accept arbitrary
224numbers of arguments and keyword arguments:
225
226.. code-block:: python
227
228 def generic(*args, **kwargs):
229 ... # do something with args and kwargs
230
231Such functions can also be created using pybind11:
232
233.. code-block:: cpp
234
235 void generic(py::args args, py::kwargs kwargs) {
236 /// .. do something with args
237 if (kwargs)
238 /// .. do something with kwargs
239 }
240
241 /// Binding code
242 m.def("generic", &generic);
243
244The class ``py::args`` derives from ``py::tuple`` and ``py::kwargs`` derives
245from ``py::dict``. Note that the ``kwargs`` argument is invalid if no keyword
246arguments were actually provided. Please refer to the other examples for
247details on how to iterate over these, and on how to cast their entries into
248C++ objects. A demonstration is also available in
249``tests/test_kwargs_and_defaults.cpp``.
250
251.. warning::
252
253 Unlike Python, pybind11 does not allow combining normal parameters with the
254 ``args`` / ``kwargs`` special parameters.
255
256Default arguments revisited
257===========================
258
259The section on :ref:`default_args` previously discussed basic usage of default
260arguments using pybind11. One noteworthy aspect of their implementation is that
261default arguments are converted to Python objects right at declaration time.
262Consider the following example:
263
264.. code-block:: cpp
265
266 py::class_<MyClass>("MyClass")
267 .def("myFunction", py::arg("arg") = SomeType(123));
268
269In this case, pybind11 must already be set up to deal with values of the type
270``SomeType`` (via a prior instantiation of ``py::class_<SomeType>``), or an
271exception will be thrown.
272
273Another aspect worth highlighting is that the "preview" of the default argument
274in the function signature is generated using the object's ``__repr__`` method.
275If not available, the signature may not be very helpful, e.g.:
276
277.. code-block:: pycon
278
279 FUNCTIONS
280 ...
281 | myFunction(...)
282 | Signature : (MyClass, arg : SomeType = <SomeType object at 0x101b7b080>) -> NoneType
283 ...
284
285The first way of addressing this is by defining ``SomeType.__repr__``.
286Alternatively, it is possible to specify the human-readable preview of the
287default argument manually using the ``arg_v`` notation:
288
289.. code-block:: cpp
290
291 py::class_<MyClass>("MyClass")
292 .def("myFunction", py::arg_v("arg", SomeType(123), "SomeType(123)"));
293
294Sometimes it may be necessary to pass a null pointer value as a default
295argument. In this case, remember to cast it to the underlying type in question,
296like so:
297
298.. code-block:: cpp
299
300 py::class_<MyClass>("MyClass")
301 .def("myFunction", py::arg("arg") = (SomeType *) nullptr);