Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 1 | Functions |
| 2 | ######### |
| 3 | |
| 4 | Before proceeding with this section, make sure that you are already familiar |
| 5 | with the basics of binding functions and classes, as explained in :doc:`/basics` |
| 6 | and :doc:`/classes`. The following guide is applicable to both free and member |
| 7 | functions, i.e. *methods* in Python. |
| 8 | |
| 9 | Return value policies |
| 10 | ===================== |
| 11 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 12 | Python and C++ use fundamentally different ways of managing the memory and |
| 13 | lifetime of objects managed by them. This can lead to issues when creating |
| 14 | bindings for functions that return a non-trivial type. Just by looking at the |
| 15 | type information, it is not clear whether Python should take charge of the |
| 16 | returned value and eventually free its resources, or if this is handled on the |
Dean Moldovan | 57a9bbc | 2017-01-31 16:54:08 +0100 | [diff] [blame] | 17 | C++ side. For this reason, pybind11 provides a several *return value policy* |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 18 | annotations 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 Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 21 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 22 | Return value policies are tricky, and it's very important to get them right. |
| 23 | Just to illustrate what can go wrong, consider the following simple example: |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 24 | |
| 25 | .. code-block:: cpp |
| 26 | |
Dean Moldovan | 57a9bbc | 2017-01-31 16:54:08 +0100 | [diff] [blame] | 27 | /* Function declaration */ |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 28 | Data *get_data() { return _data; /* (pointer to a static data structure) */ } |
| 29 | ... |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 30 | |
Dean Moldovan | 57a9bbc | 2017-01-31 16:54:08 +0100 | [diff] [blame] | 31 | /* Binding code */ |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 32 | m.def("get_data", &get_data); // <-- KABOOM, will cause crash when called from Python |
| 33 | |
| 34 | What's going on here? When ``get_data()`` is called from Python, the return |
| 35 | value (a native C++ type) must be wrapped to turn it into a usable Python type. |
| 36 | In this case, the default return value policy (:enum:`return_value_policy::automatic`) |
| 37 | causes pybind11 to assume ownership of the static ``_data`` instance. |
| 38 | |
| 39 | When Python's garbage collector eventually deletes the Python |
| 40 | wrapper, pybind11 will also attempt to delete the C++ instance (via ``operator |
| 41 | delete()``) due to the implied ownership. At this point, the entire application |
| 42 | will come crashing down, though errors could also be more subtle and involve |
| 43 | silent data corruption. |
| 44 | |
| 45 | In the above example, the policy :enum:`return_value_policy::reference` should have |
| 46 | been specified so that the global data instance is only *referenced* without any |
Dean Moldovan | 57a9bbc | 2017-01-31 16:54:08 +0100 | [diff] [blame] | 47 | implied transfer of ownership, i.e.: |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 48 | |
| 49 | .. code-block:: cpp |
| 50 | |
| 51 | m.def("get_data", &get_data, return_value_policy::reference); |
| 52 | |
| 53 | On the other hand, this is not the right policy for many other situations, |
| 54 | where ignoring ownership could lead to resource leaks. |
| 55 | As a developer using pybind11, it's important to be familiar with the different |
| 56 | return value policies, including which situation calls for which one of them. |
| 57 | The following table provides an overview of available policies: |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 58 | |
| 59 | .. tabularcolumns:: |p{0.5\textwidth}|p{0.45\textwidth}| |
| 60 | |
| 61 | +--------------------------------------------------+----------------------------------------------------------------------------+ |
| 62 | | Return value policy | Description | |
| 63 | +==================================================+============================================================================+ |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 64 | | :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 Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 67 | | | C++ side does the same, or when the data was not dynamically allocated. | |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 68 | +--------------------------------------------------+----------------------------------------------------------------------------+ |
| 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 | +--------------------------------------------------+----------------------------------------------------------------------------+ |
jbarlow83 | 7830e85 | 2017-01-13 02:17:29 -0800 | [diff] [blame] | 91 | | :enum:`return_value_policy::automatic` | **Default policy.** This policy falls back to the policy | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 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 | |
Dean Moldovan | 03f627e | 2016-11-01 11:44:57 +0100 | [diff] [blame] | 103 | Return value policies can also be applied to properties: |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 104 | |
| 105 | .. code-block:: cpp |
| 106 | |
| 107 | class_<MyClass>(m, "MyClass") |
Dean Moldovan | 03f627e | 2016-11-01 11:44:57 +0100 | [diff] [blame] | 108 | .def_property("data", &MyClass::getData, &MyClass::setData, |
| 109 | py::return_value_policy::copy); |
| 110 | |
| 111 | Technically, the code above applies the policy to both the getter and the |
| 112 | setter function, however, the setter doesn't really care about *return* |
| 113 | value policies which makes this a convenient terse syntax. Alternatively, |
| 114 | targeted arguments can be passed through the :class:`cpp_function` constructor: |
| 115 | |
| 116 | .. code-block:: cpp |
| 117 | |
| 118 | class_<MyClass>(m, "MyClass") |
| 119 | .def_property("data" |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 120 | py::cpp_function(&MyClass::getData, py::return_value_policy::copy), |
| 121 | py::cpp_function(&MyClass::setData) |
| 122 | ); |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 123 | |
| 124 | .. warning:: |
| 125 | |
| 126 | Code with invalid return value policies might access unitialized memory or |
| 127 | free data structures multiple times, which can lead to hard-to-debug |
| 128 | non-determinism and segmentation faults, hence it is worth spending the |
| 129 | time to understand all the different options in the table above. |
| 130 | |
Wenzel Jakob | 6ba9865 | 2016-10-24 23:48:20 +0200 | [diff] [blame] | 131 | .. note:: |
| 132 | |
| 133 | One important aspect of the above policies is that they only apply to |
| 134 | instances which pybind11 has *not* seen before, in which case the policy |
| 135 | clarifies essential questions about the return value's lifetime and |
| 136 | ownership. When pybind11 knows the instance already (as identified by its |
| 137 | type and address in memory), it will return the existing Python object |
| 138 | wrapper rather than creating a new copy. |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 139 | |
| 140 | .. note:: |
| 141 | |
| 142 | The next section on :ref:`call_policies` discusses *call policies* that can be |
| 143 | specified *in addition* to a return value policy from the list above. Call |
| 144 | policies indicate reference relationships that can involve both return values |
| 145 | and parameters of functions. |
| 146 | |
| 147 | .. note:: |
| 148 | |
| 149 | As an alternative to elaborate call policies and lifetime management logic, |
| 150 | consider using smart pointers (see the section on :ref:`smart_pointers` for |
| 151 | details). Smart pointers can tell whether an object is still referenced from |
| 152 | C++ or Python, which generally eliminates the kinds of inconsistencies that |
| 153 | can lead to crashes or undefined behavior. For functions returning smart |
| 154 | pointers, it is not necessary to specify a return value policy. |
| 155 | |
| 156 | .. _call_policies: |
| 157 | |
| 158 | Additional call policies |
| 159 | ======================== |
| 160 | |
Dean Moldovan | 57a9bbc | 2017-01-31 16:54:08 +0100 | [diff] [blame] | 161 | In addition to the above return value policies, further *call policies* can be |
| 162 | specified to indicate dependencies between parameters. In general, call policies |
| 163 | are required when the C++ object is any kind of container and another object is being |
jbarlow83 | 7830e85 | 2017-01-13 02:17:29 -0800 | [diff] [blame] | 164 | added to the container. |
| 165 | |
| 166 | There is currently just |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 167 | one policy named ``keep_alive<Nurse, Patient>``, which indicates that the |
| 168 | argument with index ``Patient`` should be kept alive at least until the |
| 169 | argument with index ``Nurse`` is freed by the garbage collector. Argument |
| 170 | indices start at one, while zero refers to the return value. For methods, index |
| 171 | ``1`` refers to the implicit ``this`` pointer, while regular arguments begin at |
| 172 | index ``2``. Arbitrarily many call policies can be specified. When a ``Nurse`` |
| 173 | with value ``None`` is detected at runtime, the call policy does nothing. |
| 174 | |
| 175 | This feature internally relies on the ability to create a *weak reference* to |
| 176 | the nurse object, which is permitted by all classes exposed via pybind11. When |
| 177 | the nurse object does not support weak references, an exception will be thrown. |
| 178 | |
| 179 | Consider the following example: here, the binding code for a list append |
| 180 | operation ties the lifetime of the newly added element to the underlying |
| 181 | container: |
| 182 | |
| 183 | .. code-block:: cpp |
| 184 | |
| 185 | py::class_<List>(m, "List") |
| 186 | .def("append", &List::append, py::keep_alive<1, 2>()); |
| 187 | |
| 188 | .. note:: |
| 189 | |
| 190 | ``keep_alive`` is analogous to the ``with_custodian_and_ward`` (if Nurse, |
| 191 | Patient != 0) and ``with_custodian_and_ward_postcall`` (if Nurse/Patient == |
| 192 | 0) policies from Boost.Python. |
| 193 | |
| 194 | .. seealso:: |
| 195 | |
| 196 | The file :file:`tests/test_keep_alive.cpp` contains a complete example |
| 197 | that demonstrates using :class:`keep_alive` in more detail. |
| 198 | |
| 199 | .. _python_objects_as_args: |
| 200 | |
| 201 | Python objects as arguments |
| 202 | =========================== |
| 203 | |
| 204 | pybind11 exposes all major Python types using thin C++ wrapper classes. These |
| 205 | wrapper classes can also be used as parameters of functions in bindings, which |
| 206 | makes it possible to directly work with native Python types on the C++ side. |
| 207 | For instance, the following statement iterates over a Python ``dict``: |
| 208 | |
| 209 | .. code-block:: cpp |
| 210 | |
| 211 | void print_dict(py::dict dict) { |
| 212 | /* Easily interact with Python types */ |
| 213 | for (auto item : dict) |
myd7349 | 9b815ad | 2017-01-13 18:15:52 +0800 | [diff] [blame] | 214 | std::cout << "key=" << std::string(py::str(item.first)) << ", " |
| 215 | << "value=" << std::string(py::str(item.second)) << std::endl; |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | It can be exported: |
| 219 | |
| 220 | .. code-block:: cpp |
| 221 | |
| 222 | m.def("print_dict", &print_dict); |
| 223 | |
| 224 | And used in Python as usual: |
| 225 | |
| 226 | .. code-block:: pycon |
| 227 | |
| 228 | >>> print_dict({'foo': 123, 'bar': 'hello'}) |
| 229 | key=foo, value=123 |
| 230 | key=bar, value=hello |
| 231 | |
| 232 | For more information on using Python objects in C++, see :doc:`/advanced/pycpp/index`. |
| 233 | |
| 234 | Accepting \*args and \*\*kwargs |
| 235 | =============================== |
| 236 | |
| 237 | Python provides a useful mechanism to define functions that accept arbitrary |
| 238 | numbers of arguments and keyword arguments: |
| 239 | |
| 240 | .. code-block:: python |
| 241 | |
| 242 | def generic(*args, **kwargs): |
| 243 | ... # do something with args and kwargs |
| 244 | |
| 245 | Such functions can also be created using pybind11: |
| 246 | |
| 247 | .. code-block:: cpp |
| 248 | |
| 249 | void generic(py::args args, py::kwargs kwargs) { |
| 250 | /// .. do something with args |
| 251 | if (kwargs) |
| 252 | /// .. do something with kwargs |
| 253 | } |
| 254 | |
| 255 | /// Binding code |
| 256 | m.def("generic", &generic); |
| 257 | |
| 258 | The class ``py::args`` derives from ``py::tuple`` and ``py::kwargs`` derives |
Jason Rhinelander | 2686da8 | 2017-01-21 23:42:14 -0500 | [diff] [blame] | 259 | from ``py::dict``. |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 260 | |
Jason Rhinelander | 2686da8 | 2017-01-21 23:42:14 -0500 | [diff] [blame] | 261 | You may also use just one or the other, and may combine these with other |
| 262 | arguments as long as the ``py::args`` and ``py::kwargs`` arguments are the last |
| 263 | arguments accepted by the function. |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 264 | |
Jason Rhinelander | 2686da8 | 2017-01-21 23:42:14 -0500 | [diff] [blame] | 265 | Please refer to the other examples for details on how to iterate over these, |
| 266 | and on how to cast their entries into C++ objects. A demonstration is also |
| 267 | available in ``tests/test_kwargs_and_defaults.cpp``. |
| 268 | |
| 269 | .. note:: |
| 270 | |
| 271 | When combining \*args or \*\*kwargs with :ref:`keyword_args` you should |
| 272 | *not* include ``py::arg`` tags for the ``py::args`` and ``py::kwargs`` |
| 273 | arguments. |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 274 | |
| 275 | Default arguments revisited |
| 276 | =========================== |
| 277 | |
| 278 | The section on :ref:`default_args` previously discussed basic usage of default |
| 279 | arguments using pybind11. One noteworthy aspect of their implementation is that |
| 280 | default arguments are converted to Python objects right at declaration time. |
| 281 | Consider the following example: |
| 282 | |
| 283 | .. code-block:: cpp |
| 284 | |
| 285 | py::class_<MyClass>("MyClass") |
| 286 | .def("myFunction", py::arg("arg") = SomeType(123)); |
| 287 | |
| 288 | In this case, pybind11 must already be set up to deal with values of the type |
| 289 | ``SomeType`` (via a prior instantiation of ``py::class_<SomeType>``), or an |
| 290 | exception will be thrown. |
| 291 | |
| 292 | Another aspect worth highlighting is that the "preview" of the default argument |
| 293 | in the function signature is generated using the object's ``__repr__`` method. |
| 294 | If not available, the signature may not be very helpful, e.g.: |
| 295 | |
| 296 | .. code-block:: pycon |
| 297 | |
| 298 | FUNCTIONS |
| 299 | ... |
| 300 | | myFunction(...) |
| 301 | | Signature : (MyClass, arg : SomeType = <SomeType object at 0x101b7b080>) -> NoneType |
| 302 | ... |
| 303 | |
| 304 | The first way of addressing this is by defining ``SomeType.__repr__``. |
| 305 | Alternatively, it is possible to specify the human-readable preview of the |
| 306 | default argument manually using the ``arg_v`` notation: |
| 307 | |
| 308 | .. code-block:: cpp |
| 309 | |
| 310 | py::class_<MyClass>("MyClass") |
| 311 | .def("myFunction", py::arg_v("arg", SomeType(123), "SomeType(123)")); |
| 312 | |
| 313 | Sometimes it may be necessary to pass a null pointer value as a default |
| 314 | argument. In this case, remember to cast it to the underlying type in question, |
| 315 | like so: |
| 316 | |
| 317 | .. code-block:: cpp |
| 318 | |
| 319 | py::class_<MyClass>("MyClass") |
| 320 | .def("myFunction", py::arg("arg") = (SomeType *) nullptr); |
Jason Rhinelander | abc29ca | 2017-01-23 03:50:00 -0500 | [diff] [blame] | 321 | |
| 322 | Non-converting arguments |
| 323 | ======================== |
| 324 | |
| 325 | Certain argument types may support conversion from one type to another. Some |
| 326 | examples of conversions are: |
| 327 | |
| 328 | * :ref:`implicit_conversions` declared using ``py::implicitly_convertible<A,B>()`` |
| 329 | * Calling a method accepting a double with an integer argument |
| 330 | * Calling a ``std::complex<float>`` argument with a non-complex python type |
| 331 | (for example, with a float). (Requires the optional ``pybind11/complex.h`` |
| 332 | header). |
| 333 | * Calling a function taking an Eigen matrix reference with a numpy array of the |
| 334 | wrong type or of an incompatible data layout. (Requires the optional |
| 335 | ``pybind11/eigen.h`` header). |
| 336 | |
| 337 | This behaviour is sometimes undesirable: the binding code may prefer to raise |
| 338 | an error rather than convert the argument. This behaviour can be obtained |
| 339 | through ``py::arg`` by calling the ``.noconvert()`` method of the ``py::arg`` |
| 340 | object, such as: |
| 341 | |
| 342 | .. code-block:: cpp |
| 343 | |
| 344 | m.def("floats_only", [](double f) { return 0.5 * f; }, py::arg("f").noconvert()); |
| 345 | m.def("floats_preferred", [](double f) { return 0.5 * f; }, py::arg("f")); |
| 346 | |
| 347 | Attempting the call the second function (the one without ``.noconvert()``) with |
| 348 | an integer will succeed, but attempting to call the ``.noconvert()`` version |
| 349 | will fail with a ``TypeError``: |
| 350 | |
| 351 | .. code-block:: pycon |
| 352 | |
| 353 | >>> floats_preferred(4) |
| 354 | 2.0 |
| 355 | >>> floats_only(4) |
| 356 | Traceback (most recent call last): |
| 357 | File "<stdin>", line 1, in <module> |
| 358 | TypeError: floats_only(): incompatible function arguments. The following argument types are supported: |
| 359 | 1. (f: float) -> float |
| 360 | |
| 361 | Invoked with: 4 |
| 362 | |
| 363 | You may, of course, combine this with the :var:`_a` shorthand notation (see |
| 364 | :ref:`keyword_args`) and/or :ref:`default_args`. It is also permitted to omit |
| 365 | the argument name by using the ``py::arg()`` constructor without an argument |
| 366 | name, i.e. by specifying ``py::arg().noconvert()``. |
| 367 | |
| 368 | .. note:: |
| 369 | |
| 370 | When specifying ``py::arg`` options it is necessary to provide the same |
| 371 | number of options as the bound function has arguments. Thus if you want to |
| 372 | enable no-convert behaviour for just one of several arguments, you will |
| 373 | need to specify a ``py::arg()`` annotation for each argument with the |
| 374 | no-convert argument modified to ``py::arg().noconvert()``. |
Jason Rhinelander | e550589 | 2017-02-03 18:25:34 -0500 | [diff] [blame] | 375 | |
| 376 | Overload resolution order |
| 377 | ========================= |
| 378 | |
| 379 | When a function or method with multiple overloads is called from Python, |
| 380 | pybind11 determines which overload to call in two passes. The first pass |
| 381 | attempts to call each overload without allowing argument conversion (as if |
| 382 | every argument had been specified as ``py::arg().noconvert()`` as decribed |
| 383 | above). |
| 384 | |
| 385 | If no overload succeeds in the no-conversion first pass, a second pass is |
| 386 | attempted in which argument conversion is allowed (except where prohibited via |
| 387 | an explicit ``py::arg().noconvert()`` attribute in the function definition). |
| 388 | |
| 389 | If the second pass also fails a ``TypeError`` is raised. |
| 390 | |
| 391 | Within each pass, overloads are tried in the order they were registered with |
| 392 | pybind11. |
| 393 | |
| 394 | What this means in practice is that pybind11 will prefer any overload that does |
| 395 | not require conversion of arguments to an overload that does, but otherwise prefers |
| 396 | earlier-defined overloads to later-defined ones. |
| 397 | |
| 398 | .. note:: |
| 399 | |
| 400 | pybind11 does *not* further prioritize based on the number/pattern of |
| 401 | overloaded arguments. That is, pybind11 does not prioritize a function |
| 402 | requiring one conversion over one requiring three, but only prioritizes |
| 403 | overloads requiring no conversion at all to overloads that require |
| 404 | conversion of at least one argument. |