functions.rst 22 KB

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