You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

484 lines
22 KiB

2 months ago
  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 unitialized 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. This feature internally relies on the ability to create a *weak reference* to
  145. the nurse object, which is permitted by all classes exposed via pybind11. When
  146. the nurse object does not support weak references, an exception will be thrown.
  147. Consider the following example: here, the binding code for a list append
  148. operation ties the lifetime of the newly added element to the underlying
  149. container:
  150. .. code-block:: cpp
  151. py::class_<List>(m, "List")
  152. .def("append", &List::append, py::keep_alive<1, 2>());
  153. .. note::
  154. ``keep_alive`` is analogous to the ``with_custodian_and_ward`` (if Nurse,
  155. Patient != 0) and ``with_custodian_and_ward_postcall`` (if Nurse/Patient ==
  156. 0) policies from Boost.Python.
  157. Call guard
  158. ----------
  159. The ``call_guard<T>`` policy allows any scope guard type ``T`` to be placed
  160. around the function call. For example, this definition:
  161. .. code-block:: cpp
  162. m.def("foo", foo, py::call_guard<T>());
  163. is equivalent to the following pseudocode:
  164. .. code-block:: cpp
  165. m.def("foo", [](args...) {
  166. T scope_guard;
  167. return foo(args...); // forwarded arguments
  168. });
  169. The only requirement is that ``T`` is default-constructible, but otherwise any
  170. scope guard will work. This is very useful in combination with `gil_scoped_release`.
  171. See :ref:`gil`.
  172. Multiple guards can also be specified as ``py::call_guard<T1, T2, T3...>``. The
  173. constructor order is left to right and destruction happens in reverse.
  174. .. seealso::
  175. The file :file:`tests/test_call_policies.cpp` contains a complete example
  176. that demonstrates using `keep_alive` and `call_guard` in more detail.
  177. .. _python_objects_as_args:
  178. Python objects as arguments
  179. ===========================
  180. pybind11 exposes all major Python types using thin C++ wrapper classes. These
  181. wrapper classes can also be used as parameters of functions in bindings, which
  182. makes it possible to directly work with native Python types on the C++ side.
  183. For instance, the following statement iterates over a Python ``dict``:
  184. .. code-block:: cpp
  185. void print_dict(py::dict dict) {
  186. /* Easily interact with Python types */
  187. for (auto item : dict)
  188. std::cout << "key=" << std::string(py::str(item.first)) << ", "
  189. << "value=" << std::string(py::str(item.second)) << std::endl;
  190. }
  191. It can be exported:
  192. .. code-block:: cpp
  193. m.def("print_dict", &print_dict);
  194. And used in Python as usual:
  195. .. code-block:: pycon
  196. >>> print_dict({'foo': 123, 'bar': 'hello'})
  197. key=foo, value=123
  198. key=bar, value=hello
  199. For more information on using Python objects in C++, see :doc:`/advanced/pycpp/index`.
  200. Accepting \*args and \*\*kwargs
  201. ===============================
  202. Python provides a useful mechanism to define functions that accept arbitrary
  203. numbers of arguments and keyword arguments:
  204. .. code-block:: python
  205. def generic(*args, **kwargs):
  206. ... # do something with args and kwargs
  207. Such functions can also be created using pybind11:
  208. .. code-block:: cpp
  209. void generic(py::args args, py::kwargs kwargs) {
  210. /// .. do something with args
  211. if (kwargs)
  212. /// .. do something with kwargs
  213. }
  214. /// Binding code
  215. m.def("generic", &generic);
  216. The class ``py::args`` derives from ``py::tuple`` and ``py::kwargs`` derives
  217. from ``py::dict``.
  218. You may also use just one or the other, and may combine these with other
  219. arguments as long as the ``py::args`` and ``py::kwargs`` arguments are the last
  220. arguments accepted by the function.
  221. Please refer to the other examples for details on how to iterate over these,
  222. and on how to cast their entries into C++ objects. A demonstration is also
  223. available in ``tests/test_kwargs_and_defaults.cpp``.
  224. .. note::
  225. When combining \*args or \*\*kwargs with :ref:`keyword_args` you should
  226. *not* include ``py::arg`` tags for the ``py::args`` and ``py::kwargs``
  227. arguments.
  228. Default arguments revisited
  229. ===========================
  230. The section on :ref:`default_args` previously discussed basic usage of default
  231. arguments using pybind11. One noteworthy aspect of their implementation is that
  232. default arguments are converted to Python objects right at declaration time.
  233. Consider the following example:
  234. .. code-block:: cpp
  235. py::class_<MyClass>("MyClass")
  236. .def("myFunction", py::arg("arg") = SomeType(123));
  237. In this case, pybind11 must already be set up to deal with values of the type
  238. ``SomeType`` (via a prior instantiation of ``py::class_<SomeType>``), or an
  239. exception will be thrown.
  240. Another aspect worth highlighting is that the "preview" of the default argument
  241. in the function signature is generated using the object's ``__repr__`` method.
  242. If not available, the signature may not be very helpful, e.g.:
  243. .. code-block:: pycon
  244. FUNCTIONS
  245. ...
  246. | myFunction(...)
  247. | Signature : (MyClass, arg : SomeType = <SomeType object at 0x101b7b080>) -> NoneType
  248. ...
  249. The first way of addressing this is by defining ``SomeType.__repr__``.
  250. Alternatively, it is possible to specify the human-readable preview of the
  251. default argument manually using the ``arg_v`` notation:
  252. .. code-block:: cpp
  253. py::class_<MyClass>("MyClass")
  254. .def("myFunction", py::arg_v("arg", SomeType(123), "SomeType(123)"));
  255. Sometimes it may be necessary to pass a null pointer value as a default
  256. argument. In this case, remember to cast it to the underlying type in question,
  257. like so:
  258. .. code-block:: cpp
  259. py::class_<MyClass>("MyClass")
  260. .def("myFunction", py::arg("arg") = (SomeType *) nullptr);
  261. .. _nonconverting_arguments:
  262. Non-converting arguments
  263. ========================
  264. Certain argument types may support conversion from one type to another. Some
  265. examples of conversions are:
  266. * :ref:`implicit_conversions` declared using ``py::implicitly_convertible<A,B>()``
  267. * Calling a method accepting a double with an integer argument
  268. * Calling a ``std::complex<float>`` argument with a non-complex python type
  269. (for example, with a float). (Requires the optional ``pybind11/complex.h``
  270. header).
  271. * Calling a function taking an Eigen matrix reference with a numpy array of the
  272. wrong type or of an incompatible data layout. (Requires the optional
  273. ``pybind11/eigen.h`` header).
  274. This behaviour is sometimes undesirable: the binding code may prefer to raise
  275. an error rather than convert the argument. This behaviour can be obtained
  276. through ``py::arg`` by calling the ``.noconvert()`` method of the ``py::arg``
  277. object, such as:
  278. .. code-block:: cpp
  279. m.def("floats_only", [](double f) { return 0.5 * f; }, py::arg("f").noconvert());
  280. m.def("floats_preferred", [](double f) { return 0.5 * f; }, py::arg("f"));
  281. Attempting the call the second function (the one without ``.noconvert()``) with
  282. an integer will succeed, but attempting to call the ``.noconvert()`` version
  283. will fail with a ``TypeError``:
  284. .. code-block:: pycon
  285. >>> floats_preferred(4)
  286. 2.0
  287. >>> floats_only(4)
  288. Traceback (most recent call last):
  289. File "<stdin>", line 1, in <module>
  290. TypeError: floats_only(): incompatible function arguments. The following argument types are supported:
  291. 1. (f: float) -> float
  292. Invoked with: 4
  293. You may, of course, combine this with the :var:`_a` shorthand notation (see
  294. :ref:`keyword_args`) and/or :ref:`default_args`. It is also permitted to omit
  295. the argument name by using the ``py::arg()`` constructor without an argument
  296. name, i.e. by specifying ``py::arg().noconvert()``.
  297. .. note::
  298. When specifying ``py::arg`` options it is necessary to provide the same
  299. number of options as the bound function has arguments. Thus if you want to
  300. enable no-convert behaviour for just one of several arguments, you will
  301. need to specify a ``py::arg()`` annotation for each argument with the
  302. no-convert argument modified to ``py::arg().noconvert()``.
  303. Allow/Prohibiting None arguments
  304. ================================
  305. When a C++ type registered with :class:`py::class_` is passed as an argument to
  306. a function taking the instance as pointer or shared holder (e.g. ``shared_ptr``
  307. or a custom, copyable holder as described in :ref:`smart_pointers`), pybind
  308. allows ``None`` to be passed from Python which results in calling the C++
  309. function with ``nullptr`` (or an empty holder) for the argument.
  310. To explicitly enable or disable this behaviour, using the
  311. ``.none`` method of the :class:`py::arg` object:
  312. .. code-block:: cpp
  313. py::class_<Dog>(m, "Dog").def(py::init<>());
  314. py::class_<Cat>(m, "Cat").def(py::init<>());
  315. m.def("bark", [](Dog *dog) -> std::string {
  316. if (dog) return "woof!"; /* Called with a Dog instance */
  317. else return "(no dog)"; /* Called with None, d == nullptr */
  318. }, py::arg("dog").none(true));
  319. m.def("meow", [](Cat *cat) -> std::string {
  320. // Can't be called with None argument
  321. return "meow";
  322. }, py::arg("cat").none(false));
  323. With the above, the Python call ``bark(None)`` will return the string ``"(no
  324. dog)"``, while attempting to call ``meow(None)`` will raise a ``TypeError``:
  325. .. code-block:: pycon
  326. >>> from animals import Dog, Cat, bark, meow
  327. >>> bark(Dog())
  328. 'woof!'
  329. >>> meow(Cat())
  330. 'meow'
  331. >>> bark(None)
  332. '(no dog)'
  333. >>> meow(None)
  334. Traceback (most recent call last):
  335. File "<stdin>", line 1, in <module>
  336. TypeError: meow(): incompatible function arguments. The following argument types are supported:
  337. 1. (cat: animals.Cat) -> str
  338. Invoked with: None
  339. The default behaviour when the tag is unspecified is to allow ``None``.
  340. Overload resolution order
  341. =========================
  342. When a function or method with multiple overloads is called from Python,
  343. pybind11 determines which overload to call in two passes. The first pass
  344. attempts to call each overload without allowing argument conversion (as if
  345. every argument had been specified as ``py::arg().noconvert()`` as decribed
  346. above).
  347. If no overload succeeds in the no-conversion first pass, a second pass is
  348. attempted in which argument conversion is allowed (except where prohibited via
  349. an explicit ``py::arg().noconvert()`` attribute in the function definition).
  350. If the second pass also fails a ``TypeError`` is raised.
  351. Within each pass, overloads are tried in the order they were registered with
  352. pybind11.
  353. What this means in practice is that pybind11 will prefer any overload that does
  354. not require conversion of arguments to an overload that does, but otherwise prefers
  355. earlier-defined overloads to later-defined ones.
  356. .. note::
  357. pybind11 does *not* further prioritize based on the number/pattern of
  358. overloaded arguments. That is, pybind11 does not prioritize a function
  359. requiring one conversion over one requiring three, but only prioritizes
  360. overloads requiring no conversion at all to overloads that require
  361. conversion of at least one argument.