blob: ba95c2053f98465f0b68f956c650d92ada1ac298 [file] [log] [blame]
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001.. _advanced:
2
3Advanced topics
4###############
5
Wenzel Jakob93296692015-10-13 23:21:54 +02006For brevity, the rest of this chapter assumes that the following two lines are
7present:
8
9.. code-block:: cpp
10
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020011 #include <pybind11/pybind11.h>
Wenzel Jakob93296692015-10-13 23:21:54 +020012
Wenzel Jakob10e62e12015-10-15 22:46:07 +020013 namespace py = pybind11;
Wenzel Jakob93296692015-10-13 23:21:54 +020014
Wenzel Jakobde3ad072016-02-02 11:38:21 +010015Exporting constants and mutable objects
16=======================================
17
18To expose a C++ constant, use the ``attr`` function to register it in a module
19as shown below. The ``int_`` class is one of many small wrapper objects defined
20in ``pybind11/pytypes.h``. General objects (including integers) can also be
21converted using the function ``cast``.
22
23.. code-block:: cpp
24
25 PYBIND11_PLUGIN(example) {
26 py::module m("example", "pybind11 example plugin");
27 m.attr("MY_CONSTANT") = py::int_(123);
28 m.attr("MY_CONSTANT_2") = py::cast(new MyObject());
29 }
30
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020031Operator overloading
32====================
33
Wenzel Jakob93296692015-10-13 23:21:54 +020034Suppose that we're given the following ``Vector2`` class with a vector addition
35and scalar multiplication operation, all implemented using overloaded operators
36in C++.
37
38.. code-block:: cpp
39
40 class Vector2 {
41 public:
42 Vector2(float x, float y) : x(x), y(y) { }
43
Wenzel Jakob93296692015-10-13 23:21:54 +020044 Vector2 operator+(const Vector2 &v) const { return Vector2(x + v.x, y + v.y); }
45 Vector2 operator*(float value) const { return Vector2(x * value, y * value); }
46 Vector2& operator+=(const Vector2 &v) { x += v.x; y += v.y; return *this; }
47 Vector2& operator*=(float v) { x *= v; y *= v; return *this; }
48
Wenzel Jakobf64feaf2016-04-28 14:33:45 +020049 friend Vector2 operator*(float f, const Vector2 &v) {
50 return Vector2(f * v.x, f * v.y);
51 }
Wenzel Jakob93296692015-10-13 23:21:54 +020052
Wenzel Jakobf64feaf2016-04-28 14:33:45 +020053 std::string toString() const {
54 return "[" + std::to_string(x) + ", " + std::to_string(y) + "]";
55 }
Wenzel Jakob93296692015-10-13 23:21:54 +020056 private:
57 float x, y;
58 };
59
60The following snippet shows how the above operators can be conveniently exposed
61to Python.
62
63.. code-block:: cpp
64
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020065 #include <pybind11/operators.h>
Wenzel Jakob93296692015-10-13 23:21:54 +020066
Wenzel Jakobb1b71402015-10-18 16:48:30 +020067 PYBIND11_PLUGIN(example) {
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020068 py::module m("example", "pybind11 example plugin");
Wenzel Jakob93296692015-10-13 23:21:54 +020069
70 py::class_<Vector2>(m, "Vector2")
71 .def(py::init<float, float>())
72 .def(py::self + py::self)
73 .def(py::self += py::self)
74 .def(py::self *= float())
75 .def(float() * py::self)
76 .def("__repr__", &Vector2::toString);
77
78 return m.ptr();
79 }
80
81Note that a line like
82
83.. code-block:: cpp
84
85 .def(py::self * float())
86
87is really just short hand notation for
88
89.. code-block:: cpp
90
91 .def("__mul__", [](const Vector2 &a, float b) {
92 return a * b;
93 })
94
95This can be useful for exposing additional operators that don't exist on the
96C++ side, or to perform other types of customization.
97
98.. note::
99
100 To use the more convenient ``py::self`` notation, the additional
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200101 header file :file:`pybind11/operators.h` must be included.
Wenzel Jakob93296692015-10-13 23:21:54 +0200102
103.. seealso::
104
105 The file :file:`example/example3.cpp` contains a complete example that
106 demonstrates how to work with overloaded operators in more detail.
107
108Callbacks and passing anonymous functions
109=========================================
110
111The C++11 standard brought lambda functions and the generic polymorphic
112function wrapper ``std::function<>`` to the C++ programming language, which
113enable powerful new ways of working with functions. Lambda functions come in
114two flavors: stateless lambda function resemble classic function pointers that
115link to an anonymous piece of code, while stateful lambda functions
116additionally depend on captured variables that are stored in an anonymous
117*lambda closure object*.
118
119Here is a simple example of a C++ function that takes an arbitrary function
120(stateful or stateless) with signature ``int -> int`` as an argument and runs
121it with the value 10.
122
123.. code-block:: cpp
124
125 int func_arg(const std::function<int(int)> &f) {
126 return f(10);
127 }
128
129The example below is more involved: it takes a function of signature ``int -> int``
130and returns another function of the same kind. The return value is a stateful
131lambda function, which stores the value ``f`` in the capture object and adds 1 to
132its return value upon execution.
133
134.. code-block:: cpp
135
136 std::function<int(int)> func_ret(const std::function<int(int)> &f) {
137 return [f](int i) {
138 return f(i) + 1;
139 };
140 }
141
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200142After including the extra header file :file:`pybind11/functional.h`, it is almost
Wenzel Jakob93296692015-10-13 23:21:54 +0200143trivial to generate binding code for both of these functions.
144
145.. code-block:: cpp
146
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200147 #include <pybind11/functional.h>
Wenzel Jakob93296692015-10-13 23:21:54 +0200148
Wenzel Jakobb1b71402015-10-18 16:48:30 +0200149 PYBIND11_PLUGIN(example) {
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200150 py::module m("example", "pybind11 example plugin");
Wenzel Jakob93296692015-10-13 23:21:54 +0200151
152 m.def("func_arg", &func_arg);
153 m.def("func_ret", &func_ret);
154
155 return m.ptr();
156 }
157
158The following interactive session shows how to call them from Python.
159
160.. code-block:: python
161
162 $ python
163 >>> import example
164 >>> def square(i):
165 ... return i * i
166 ...
167 >>> example.func_arg(square)
168 100L
169 >>> square_plus_1 = example.func_ret(square)
170 >>> square_plus_1(4)
171 17L
172 >>>
173
174.. note::
175
176 This functionality is very useful when generating bindings for callbacks in
177 C++ libraries (e.g. a graphical user interface library).
178
179 The file :file:`example/example5.cpp` contains a complete example that
180 demonstrates how to work with callbacks and anonymous functions in more detail.
181
Wenzel Jakoba4175d62015-11-17 08:30:34 +0100182.. warning::
183
184 Keep in mind that passing a function from C++ to Python (or vice versa)
185 will instantiate a piece of wrapper code that translates function
186 invocations between the two languages. Copying the same function back and
187 forth between Python and C++ many times in a row will cause these wrappers
188 to accumulate, which can decrease performance.
189
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200190Overriding virtual functions in Python
191======================================
192
Wenzel Jakob93296692015-10-13 23:21:54 +0200193Suppose that a C++ class or interface has a virtual function that we'd like to
194to override from within Python (we'll focus on the class ``Animal``; ``Dog`` is
195given as a specific example of how one would do this with traditional C++
196code).
197
198.. code-block:: cpp
199
200 class Animal {
201 public:
202 virtual ~Animal() { }
203 virtual std::string go(int n_times) = 0;
204 };
205
206 class Dog : public Animal {
207 public:
208 std::string go(int n_times) {
209 std::string result;
210 for (int i=0; i<n_times; ++i)
211 result += "woof! ";
212 return result;
213 }
214 };
215
216Let's also suppose that we are given a plain function which calls the
217function ``go()`` on an arbitrary ``Animal`` instance.
218
219.. code-block:: cpp
220
221 std::string call_go(Animal *animal) {
222 return animal->go(3);
223 }
224
225Normally, the binding code for these classes would look as follows:
226
227.. code-block:: cpp
228
Wenzel Jakobb1b71402015-10-18 16:48:30 +0200229 PYBIND11_PLUGIN(example) {
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200230 py::module m("example", "pybind11 example plugin");
Wenzel Jakob93296692015-10-13 23:21:54 +0200231
232 py::class_<Animal> animal(m, "Animal");
233 animal
234 .def("go", &Animal::go);
235
236 py::class_<Dog>(m, "Dog", animal)
237 .def(py::init<>());
238
239 m.def("call_go", &call_go);
240
241 return m.ptr();
242 }
243
244However, these bindings are impossible to extend: ``Animal`` is not
245constructible, and we clearly require some kind of "trampoline" that
246redirects virtual calls back to Python.
247
248Defining a new type of ``Animal`` from within Python is possible but requires a
249helper class that is defined as follows:
250
251.. code-block:: cpp
252
253 class PyAnimal : public Animal {
254 public:
255 /* Inherit the constructors */
256 using Animal::Animal;
257
258 /* Trampoline (need one for each virtual function) */
259 std::string go(int n_times) {
Wenzel Jakobb1b71402015-10-18 16:48:30 +0200260 PYBIND11_OVERLOAD_PURE(
Wenzel Jakob93296692015-10-13 23:21:54 +0200261 std::string, /* Return type */
262 Animal, /* Parent class */
263 go, /* Name of function */
264 n_times /* Argument(s) */
265 );
266 }
267 };
268
Wenzel Jakobb1b71402015-10-18 16:48:30 +0200269The macro :func:`PYBIND11_OVERLOAD_PURE` should be used for pure virtual
270functions, and :func:`PYBIND11_OVERLOAD` should be used for functions which have
Wenzel Jakob1e3be732016-05-24 23:42:05 +0200271a default implementation.
272
273There are also two alternate macros :func:`PYBIND11_OVERLOAD_PURE_NAME` and
274:func:`PYBIND11_OVERLOAD_NAME` which take a string-valued name argument
275after the *Name of the function* slot. This is useful when the C++ and Python
276versions of the function have different names, e.g. ``operator()`` vs ``__call__``.
277
278The binding code also needs a few minor adaptations (highlighted):
Wenzel Jakob93296692015-10-13 23:21:54 +0200279
280.. code-block:: cpp
281 :emphasize-lines: 4,6,7
282
Wenzel Jakobb1b71402015-10-18 16:48:30 +0200283 PYBIND11_PLUGIN(example) {
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200284 py::module m("example", "pybind11 example plugin");
Wenzel Jakob93296692015-10-13 23:21:54 +0200285
Wenzel Jakob86d825f2016-05-26 13:19:27 +0200286 py::class_<Animal, std::unique_ptr<Animal>, PyAnimal /* <--- trampoline*/> animal(m, "Animal");
Wenzel Jakob93296692015-10-13 23:21:54 +0200287 animal
Wenzel Jakob93296692015-10-13 23:21:54 +0200288 .def(py::init<>())
289 .def("go", &Animal::go);
290
291 py::class_<Dog>(m, "Dog", animal)
292 .def(py::init<>());
293
294 m.def("call_go", &call_go);
295
296 return m.ptr();
297 }
298
Wenzel Jakob86d825f2016-05-26 13:19:27 +0200299Importantly, pybind11 is made aware of the trampoline trampoline helper class
300by specifying it as the *third* template argument to :class:`class_`. The
301second argument with the unique pointer is simply the default holder type used
302by pybind11. Following this, we are able to define a constructor as usual.
Wenzel Jakob93296692015-10-13 23:21:54 +0200303
304The Python session below shows how to override ``Animal::go`` and invoke it via
305a virtual method call.
306
Wenzel Jakobde3ad072016-02-02 11:38:21 +0100307.. code-block:: python
Wenzel Jakob93296692015-10-13 23:21:54 +0200308
309 >>> from example import *
310 >>> d = Dog()
311 >>> call_go(d)
312 u'woof! woof! woof! '
313 >>> class Cat(Animal):
314 ... def go(self, n_times):
315 ... return "meow! " * n_times
316 ...
317 >>> c = Cat()
318 >>> call_go(c)
319 u'meow! meow! meow! '
320
Wenzel Jakobbd986fe2016-05-21 10:48:30 +0200321.. warning::
322
Wenzel Jakob86d825f2016-05-26 13:19:27 +0200323 The :func:`PYBIND11_OVERLOAD_*` calls are all just macros, which means that
324 they can get confused by commas in a template argument such as
325 ``PYBIND11_OVERLOAD(MyReturnValue<T1, T2>, myFunc)``. In this case, the
326 preprocessor assumes that the comma indicates the beginnning of the next
327 parameter. Use a ``typedef`` to bind the template to another name and use
328 it in the macro to avoid this problem.
Wenzel Jakobbd986fe2016-05-21 10:48:30 +0200329
Wenzel Jakob93296692015-10-13 23:21:54 +0200330.. seealso::
331
332 The file :file:`example/example12.cpp` contains a complete example that
333 demonstrates how to override virtual functions using pybind11 in more
334 detail.
335
Wenzel Jakobecdd8682015-12-07 18:17:58 +0100336
337Global Interpreter Lock (GIL)
338=============================
339
340The classes :class:`gil_scoped_release` and :class:`gil_scoped_acquire` can be
341used to acquire and release the global interpreter lock in the body of a C++
342function call. In this way, long-running C++ code can be parallelized using
343multiple Python threads. Taking the previous section as an example, this could
344be realized as follows (important changes highlighted):
345
346.. code-block:: cpp
347 :emphasize-lines: 8,9,33,34
348
349 class PyAnimal : public Animal {
350 public:
351 /* Inherit the constructors */
352 using Animal::Animal;
353
354 /* Trampoline (need one for each virtual function) */
355 std::string go(int n_times) {
356 /* Acquire GIL before calling Python code */
Wenzel Jakoba4caa852015-12-14 12:39:02 +0100357 py::gil_scoped_acquire acquire;
Wenzel Jakobecdd8682015-12-07 18:17:58 +0100358
359 PYBIND11_OVERLOAD_PURE(
360 std::string, /* Return type */
361 Animal, /* Parent class */
362 go, /* Name of function */
363 n_times /* Argument(s) */
364 );
365 }
366 };
367
368 PYBIND11_PLUGIN(example) {
369 py::module m("example", "pybind11 example plugin");
370
Wenzel Jakob86d825f2016-05-26 13:19:27 +0200371 py::class_<Animal, std::unique_ptr<Animal>, PyAnimal> animal(m, "Animal");
Wenzel Jakobecdd8682015-12-07 18:17:58 +0100372 animal
Wenzel Jakobecdd8682015-12-07 18:17:58 +0100373 .def(py::init<>())
374 .def("go", &Animal::go);
375
376 py::class_<Dog>(m, "Dog", animal)
377 .def(py::init<>());
378
379 m.def("call_go", [](Animal *animal) -> std::string {
380 /* Release GIL before calling into (potentially long-running) C++ code */
Wenzel Jakoba4caa852015-12-14 12:39:02 +0100381 py::gil_scoped_release release;
Wenzel Jakobecdd8682015-12-07 18:17:58 +0100382 return call_go(animal);
383 });
384
385 return m.ptr();
386 }
387
Wenzel Jakob93296692015-10-13 23:21:54 +0200388Passing STL data structures
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200389===========================
390
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200391When including the additional header file :file:`pybind11/stl.h`, conversions
Wenzel Jakob978e3762016-04-07 18:00:41 +0200392between ``std::vector<>``, ``std::list<>``, ``std::set<>``, and ``std::map<>``
393and the Python ``list``, ``set`` and ``dict`` data structures are automatically
394enabled. The types ``std::pair<>`` and ``std::tuple<>`` are already supported
395out of the box with just the core :file:`pybind11/pybind11.h` header.
Wenzel Jakob93296692015-10-13 23:21:54 +0200396
397.. note::
398
Wenzel Jakob44db04f2015-12-14 12:40:45 +0100399 Arbitrary nesting of any of these types is supported.
Wenzel Jakob93296692015-10-13 23:21:54 +0200400
401.. seealso::
402
403 The file :file:`example/example2.cpp` contains a complete example that
404 demonstrates how to pass STL data types in more detail.
405
Wenzel Jakobb2825952016-04-13 23:33:00 +0200406Binding sequence data types, iterators, the slicing protocol, etc.
407==================================================================
Wenzel Jakob93296692015-10-13 23:21:54 +0200408
409Please refer to the supplemental example for details.
410
411.. seealso::
412
413 The file :file:`example/example6.cpp` contains a complete example that
414 shows how to bind a sequence data type, including length queries
415 (``__len__``), iterators (``__iter__``), the slicing protocol and other
416 kinds of useful operations.
417
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200418Return value policies
419=====================
420
Wenzel Jakob93296692015-10-13 23:21:54 +0200421Python and C++ use wildly different ways of managing the memory and lifetime of
422objects managed by them. This can lead to issues when creating bindings for
423functions that return a non-trivial type. Just by looking at the type
424information, it is not clear whether Python should take charge of the returned
425value and eventually free its resources, or if this is handled on the C++ side.
426For this reason, pybind11 provides a several `return value policy` annotations
427that can be passed to the :func:`module::def` and :func:`class_::def`
Wenzel Jakob61d67f02015-12-14 12:53:06 +0100428functions. The default policy is :enum:`return_value_policy::automatic`.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200429
Wenzel Jakobf64feaf2016-04-28 14:33:45 +0200430.. tabularcolumns:: |p{0.5\textwidth}|p{0.45\textwidth}|
431
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200432+--------------------------------------------------+----------------------------------------------------------------------------+
433| Return value policy | Description |
434+==================================================+============================================================================+
435| :enum:`return_value_policy::automatic` | This is the default return value policy, which falls back to the policy |
436| | :enum:`return_value_policy::take_ownership` when the return value is a |
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200437| | pointer. Otherwise, it uses :enum:`return_value::move` or |
438| | :enum:`return_value::copy` for rvalue and lvalue references, respectively. |
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200439| | See below for a description of what all of these different policies do. |
440+--------------------------------------------------+----------------------------------------------------------------------------+
441| :enum:`return_value_policy::automatic_reference` | As above, but use policy :enum:`return_value_policy::reference` when the |
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200442| | return value is a pointer. You probably won't need to use this. |
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200443+--------------------------------------------------+----------------------------------------------------------------------------+
444| :enum:`return_value_policy::take_ownership` | Reference an existing object (i.e. do not create a new copy) and take |
445| | ownership. Python will call the destructor and delete operator when the |
446| | object's reference count reaches zero. Undefined behavior ensues when the |
447| | C++ side does the same.. |
448+--------------------------------------------------+----------------------------------------------------------------------------+
449| :enum:`return_value_policy::copy` | Create a new copy of the returned object, which will be owned by Python. |
450| | This policy is comparably safe because the lifetimes of the two instances |
451| | are decoupled. |
452+--------------------------------------------------+----------------------------------------------------------------------------+
453| :enum:`return_value_policy::move` | Use ``std::move`` to move the return value contents into a new instance |
454| | that will be owned by Python. This policy is comparably safe because the |
455| | lifetimes of the two instances (move source and destination) are decoupled.|
456+--------------------------------------------------+----------------------------------------------------------------------------+
457| :enum:`return_value_policy::reference` | Reference an existing object, but do not take ownership. The C++ side is |
458| | responsible for managing the object's lifetime and deallocating it when |
459| | it is no longer used. Warning: undefined behavior will ensue when the C++ |
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200460| | side deletes an object that is still referenced and used by Python. |
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200461+--------------------------------------------------+----------------------------------------------------------------------------+
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200462| :enum:`return_value_policy::reference_internal` | This policy only applies to methods and properties. It references the |
463| | object without taking ownership similar to the above |
464| | :enum:`return_value_policy::reference` policy. In contrast to that policy, |
465| | the function or property's implicit ``this`` argument (called the *parent*)|
466| | is considered to be the the owner of the return value (the *child*). |
467| | pybind11 then couples the lifetime of the parent to the child via a |
468| | reference relationship that ensures that the parent cannot be garbage |
469| | collected while Python is still using the child. More advanced variations |
470| | of this scheme are also possible using combinations of |
471| | :enum:`return_value_policy::reference` and the :class:`keep_alive` call |
472| | policy described next. |
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200473+--------------------------------------------------+----------------------------------------------------------------------------+
Wenzel Jakob93296692015-10-13 23:21:54 +0200474
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200475The following example snippet shows a use case of the
Wenzel Jakob93296692015-10-13 23:21:54 +0200476:enum:`return_value_policy::reference_internal` policy.
477
478.. code-block:: cpp
479
480 class Example {
481 public:
482 Internal &get_internal() { return internal; }
483 private:
484 Internal internal;
485 };
486
Wenzel Jakobb1b71402015-10-18 16:48:30 +0200487 PYBIND11_PLUGIN(example) {
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200488 py::module m("example", "pybind11 example plugin");
Wenzel Jakob93296692015-10-13 23:21:54 +0200489
490 py::class_<Example>(m, "Example")
491 .def(py::init<>())
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200492 .def("get_internal", &Example::get_internal, "Return the internal data",
493 py::return_value_policy::reference_internal);
Wenzel Jakob93296692015-10-13 23:21:54 +0200494
495 return m.ptr();
496 }
497
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200498.. warning::
499
500 Code with invalid call policies might access unitialized memory or free
501 data structures multiple times, which can lead to hard-to-debug
502 non-determinism and segmentation faults, hence it is worth spending the
503 time to understand all the different options in the table above.
504
505.. note::
506
507 The next section on :ref:`call_policies` discusses *call policies* that can be
508 specified *in addition* to a return value policy from the list above. Call
509 policies indicate reference relationships that can involve both return values
510 and parameters of functions.
511
512.. note::
513
514 As an alternative to elaborate call policies and lifetime management logic,
515 consider using smart pointers (see the section on :ref:`smart_pointers` for
516 details). Smart pointers can tell whether an object is still referenced from
517 C++ or Python, which generally eliminates the kinds of inconsistencies that
518 can lead to crashes or undefined behavior. For functions returning smart
519 pointers, it is not necessary to specify a return value policy.
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100520
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200521.. _call_policies:
522
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100523Additional call policies
524========================
525
526In addition to the above return value policies, further `call policies` can be
527specified to indicate dependencies between parameters. There is currently just
528one policy named ``keep_alive<Nurse, Patient>``, which indicates that the
529argument with index ``Patient`` should be kept alive at least until the
530argument with index ``Nurse`` is freed by the garbage collector; argument
Wenzel Jakob8e93df82016-05-01 02:36:58 +0200531indices start at one, while zero refers to the return value. For methods, index
532one refers to the implicit ``this`` pointer, while regular arguments begin at
533index two. Arbitrarily many call policies can be specified.
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100534
Wenzel Jakob8e93df82016-05-01 02:36:58 +0200535Consider the following example: the binding code for a list append operation
536that ties the lifetime of the newly added element to the underlying container
537might be declared as follows:
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100538
539.. code-block:: cpp
540
541 py::class_<List>(m, "List")
542 .def("append", &List::append, py::keep_alive<1, 2>());
543
544.. note::
545
546 ``keep_alive`` is analogous to the ``with_custodian_and_ward`` (if Nurse,
547 Patient != 0) and ``with_custodian_and_ward_postcall`` (if Nurse/Patient ==
548 0) policies from Boost.Python.
549
Wenzel Jakob61587162016-01-18 22:38:52 +0100550.. seealso::
551
552 The file :file:`example/example13.cpp` contains a complete example that
553 demonstrates using :class:`keep_alive` in more detail.
554
Wenzel Jakob93296692015-10-13 23:21:54 +0200555Implicit type conversions
556=========================
557
558Suppose that instances of two types ``A`` and ``B`` are used in a project, and
Wenzel Jakob8e93df82016-05-01 02:36:58 +0200559that an ``A`` can easily be converted into an instance of type ``B`` (examples of this
Wenzel Jakob93296692015-10-13 23:21:54 +0200560could be a fixed and an arbitrary precision number type).
561
562.. code-block:: cpp
563
564 py::class_<A>(m, "A")
565 /// ... members ...
566
567 py::class_<B>(m, "B")
568 .def(py::init<A>())
569 /// ... members ...
570
571 m.def("func",
572 [](const B &) { /* .... */ }
573 );
574
575To invoke the function ``func`` using a variable ``a`` containing an ``A``
576instance, we'd have to write ``func(B(a))`` in Python. On the other hand, C++
577will automatically apply an implicit type conversion, which makes it possible
578to directly write ``func(a)``.
579
580In this situation (i.e. where ``B`` has a constructor that converts from
581``A``), the following statement enables similar implicit conversions on the
582Python side:
583
584.. code-block:: cpp
585
586 py::implicitly_convertible<A, B>();
587
Wenzel Jakob9f0dfce2016-04-06 17:38:18 +0200588Unique pointers
589===============
590
591Given a class ``Example`` with Python bindings, it's possible to return
592instances wrapped in C++11 unique pointers, like so
593
594.. code-block:: cpp
595
596 std::unique_ptr<Example> create_example() { return std::unique_ptr<Example>(new Example()); }
597
598.. code-block:: cpp
599
600 m.def("create_example", &create_example);
601
602In other words, there is nothing special that needs to be done. While returning
603unique pointers in this way is allowed, it is *illegal* to use them as function
604arguments. For instance, the following function signature cannot be processed
605by pybind11.
606
607.. code-block:: cpp
608
609 void do_something_with_example(std::unique_ptr<Example> ex) { ... }
610
611The above signature would imply that Python needs to give up ownership of an
612object that is passed to this function, which is generally not possible (for
613instance, the object might be referenced elsewhere).
614
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200615.. _smart_pointers:
616
Wenzel Jakob93296692015-10-13 23:21:54 +0200617Smart pointers
618==============
619
Wenzel Jakob9f0dfce2016-04-06 17:38:18 +0200620This section explains how to pass values that are wrapped in "smart" pointer
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200621types with internal reference counting. For the simpler C++11 unique pointers,
622refer to the previous section.
Wenzel Jakob9f0dfce2016-04-06 17:38:18 +0200623
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200624The binding generator for classes, :class:`class_`, takes an optional second
Wenzel Jakob93296692015-10-13 23:21:54 +0200625template type, which denotes a special *holder* type that is used to manage
626references to the object. When wrapping a type named ``Type``, the default
627value of this template parameter is ``std::unique_ptr<Type>``, which means that
628the object is deallocated when Python's reference count goes to zero.
629
Wenzel Jakob1853b652015-10-18 15:38:50 +0200630It is possible to switch to other types of reference counting wrappers or smart
631pointers, which is useful in codebases that rely on them. For instance, the
632following snippet causes ``std::shared_ptr`` to be used instead.
Wenzel Jakob93296692015-10-13 23:21:54 +0200633
634.. code-block:: cpp
635
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100636 py::class_<Example, std::shared_ptr<Example> /* <- holder type */> obj(m, "Example");
Wenzel Jakob5ef12192015-12-15 17:07:35 +0100637
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100638Note that any particular class can only be associated with a single holder type.
Wenzel Jakob93296692015-10-13 23:21:54 +0200639
Wenzel Jakob1853b652015-10-18 15:38:50 +0200640To enable transparent conversions for functions that take shared pointers as an
Wenzel Jakob5ef12192015-12-15 17:07:35 +0100641argument or that return them, a macro invocation similar to the following must
Wenzel Jakob1853b652015-10-18 15:38:50 +0200642be declared at the top level before any binding code:
643
644.. code-block:: cpp
645
Wenzel Jakobb1b71402015-10-18 16:48:30 +0200646 PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
Wenzel Jakob1853b652015-10-18 15:38:50 +0200647
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100648.. note::
Wenzel Jakob61d67f02015-12-14 12:53:06 +0100649
650 The first argument of :func:`PYBIND11_DECLARE_HOLDER_TYPE` should be a
651 placeholder name that is used as a template parameter of the second
652 argument. Thus, feel free to use any identifier, but use it consistently on
653 both sides; also, don't use the name of a type that already exists in your
654 codebase.
655
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100656One potential stumbling block when using holder types is that they need to be
657applied consistently. Can you guess what's broken about the following binding
658code?
Wenzel Jakob6e213c92015-11-24 23:05:58 +0100659
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100660.. code-block:: cpp
Wenzel Jakob6e213c92015-11-24 23:05:58 +0100661
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100662 class Child { };
Wenzel Jakob5ef12192015-12-15 17:07:35 +0100663
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100664 class Parent {
665 public:
666 Parent() : child(std::make_shared<Child>()) { }
667 Child *get_child() { return child.get(); } /* Hint: ** DON'T DO THIS ** */
668 private:
669 std::shared_ptr<Child> child;
670 };
Wenzel Jakob5ef12192015-12-15 17:07:35 +0100671
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100672 PYBIND11_PLUGIN(example) {
673 py::module m("example");
Wenzel Jakob5ef12192015-12-15 17:07:35 +0100674
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100675 py::class_<Child, std::shared_ptr<Child>>(m, "Child");
676
677 py::class_<Parent, std::shared_ptr<Parent>>(m, "Parent")
678 .def(py::init<>())
679 .def("get_child", &Parent::get_child);
680
681 return m.ptr();
682 }
683
684The following Python code will cause undefined behavior (and likely a
685segmentation fault).
686
687.. code-block:: python
688
689 from example import Parent
690 print(Parent().get_child())
691
692The problem is that ``Parent::get_child()`` returns a pointer to an instance of
693``Child``, but the fact that this instance is already managed by
694``std::shared_ptr<...>`` is lost when passing raw pointers. In this case,
695pybind11 will create a second independent ``std::shared_ptr<...>`` that also
696claims ownership of the pointer. In the end, the object will be freed **twice**
697since these shared pointers have no way of knowing about each other.
698
699There are two ways to resolve this issue:
700
7011. For types that are managed by a smart pointer class, never use raw pointers
702 in function arguments or return values. In other words: always consistently
703 wrap pointers into their designated holder types (such as
704 ``std::shared_ptr<...>``). In this case, the signature of ``get_child()``
705 should be modified as follows:
706
707.. code-block:: cpp
708
709 std::shared_ptr<Child> get_child() { return child; }
710
7112. Adjust the definition of ``Child`` by specifying
712 ``std::enable_shared_from_this<T>`` (see cppreference_ for details) as a
713 base class. This adds a small bit of information to ``Child`` that allows
714 pybind11 to realize that there is already an existing
715 ``std::shared_ptr<...>`` and communicate with it. In this case, the
716 declaration of ``Child`` should look as follows:
Wenzel Jakob5ef12192015-12-15 17:07:35 +0100717
Wenzel Jakob6e213c92015-11-24 23:05:58 +0100718.. _cppreference: http://en.cppreference.com/w/cpp/memory/enable_shared_from_this
719
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100720.. code-block:: cpp
721
722 class Child : public std::enable_shared_from_this<Child> { };
723
Wenzel Jakob5ef12192015-12-15 17:07:35 +0100724.. seealso::
725
726 The file :file:`example/example8.cpp` contains a complete example that
727 demonstrates how to work with custom reference-counting holder types in
728 more detail.
729
Wenzel Jakob93296692015-10-13 23:21:54 +0200730.. _custom_constructors:
731
732Custom constructors
733===================
734
735The syntax for binding constructors was previously introduced, but it only
736works when a constructor with the given parameters actually exists on the C++
737side. To extend this to more general cases, let's take a look at what actually
738happens under the hood: the following statement
739
740.. code-block:: cpp
741
742 py::class_<Example>(m, "Example")
743 .def(py::init<int>());
744
745is short hand notation for
746
747.. code-block:: cpp
748
749 py::class_<Example>(m, "Example")
750 .def("__init__",
751 [](Example &instance, int arg) {
752 new (&instance) Example(arg);
753 }
754 );
755
756In other words, :func:`init` creates an anonymous function that invokes an
757in-place constructor. Memory allocation etc. is already take care of beforehand
758within pybind11.
759
760Catching and throwing exceptions
761================================
762
763When C++ code invoked from Python throws an ``std::exception``, it is
764automatically converted into a Python ``Exception``. pybind11 defines multiple
765special exception classes that will map to different types of Python
766exceptions:
767
Wenzel Jakobf64feaf2016-04-28 14:33:45 +0200768.. tabularcolumns:: |p{0.5\textwidth}|p{0.45\textwidth}|
769
Wenzel Jakob978e3762016-04-07 18:00:41 +0200770+--------------------------------------+------------------------------+
771| C++ exception type | Python exception type |
772+======================================+==============================+
773| :class:`std::exception` | ``RuntimeError`` |
774+--------------------------------------+------------------------------+
775| :class:`std::bad_alloc` | ``MemoryError`` |
776+--------------------------------------+------------------------------+
777| :class:`std::domain_error` | ``ValueError`` |
778+--------------------------------------+------------------------------+
779| :class:`std::invalid_argument` | ``ValueError`` |
780+--------------------------------------+------------------------------+
781| :class:`std::length_error` | ``ValueError`` |
782+--------------------------------------+------------------------------+
783| :class:`std::out_of_range` | ``ValueError`` |
784+--------------------------------------+------------------------------+
785| :class:`std::range_error` | ``ValueError`` |
786+--------------------------------------+------------------------------+
787| :class:`pybind11::stop_iteration` | ``StopIteration`` (used to |
788| | implement custom iterators) |
789+--------------------------------------+------------------------------+
790| :class:`pybind11::index_error` | ``IndexError`` (used to |
791| | indicate out of bounds |
792| | accesses in ``__getitem__``, |
793| | ``__setitem__``, etc.) |
794+--------------------------------------+------------------------------+
Sergey Lyskova95bde12016-05-08 19:31:55 -0400795| :class:`pybind11::value_error` | ``ValueError`` (used to |
796| | indicate wrong value passed |
797| | in ``container.remove(...)`` |
798+--------------------------------------+------------------------------+
Wenzel Jakob978e3762016-04-07 18:00:41 +0200799| :class:`pybind11::error_already_set` | Indicates that the Python |
800| | exception flag has already |
801| | been initialized |
802+--------------------------------------+------------------------------+
Wenzel Jakob93296692015-10-13 23:21:54 +0200803
804When a Python function invoked from C++ throws an exception, it is converted
805into a C++ exception of type :class:`error_already_set` whose string payload
806contains a textual summary.
807
808There is also a special exception :class:`cast_error` that is thrown by
809:func:`handle::call` when the input arguments cannot be converted to Python
810objects.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200811
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200812.. _opaque:
813
814Treating STL data structures as opaque objects
815==============================================
816
817pybind11 heavily relies on a template matching mechanism to convert parameters
818and return values that are constructed from STL data types such as vectors,
819linked lists, hash tables, etc. This even works in a recursive manner, for
820instance to deal with lists of hash maps of pairs of elementary and custom
821types, etc.
822
823However, a fundamental limitation of this approach is that internal conversions
824between Python and C++ types involve a copy operation that prevents
825pass-by-reference semantics. What does this mean?
826
827Suppose we bind the following function
828
829.. code-block:: cpp
830
831 void append_1(std::vector<int> &v) {
832 v.push_back(1);
833 }
834
835and call it from Python, the following happens:
836
837.. code-block:: python
838
839 >>> v = [5, 6]
840 >>> append_1(v)
841 >>> print(v)
842 [5, 6]
843
844As you can see, when passing STL data structures by reference, modifications
845are not propagated back the Python side. A similar situation arises when
846exposing STL data structures using the ``def_readwrite`` or ``def_readonly``
847functions:
848
849.. code-block:: cpp
850
851 /* ... definition ... */
852
853 class MyClass {
854 std::vector<int> contents;
855 };
856
857 /* ... binding code ... */
858
859 py::class_<MyClass>(m, "MyClass")
860 .def(py::init<>)
861 .def_readwrite("contents", &MyClass::contents);
862
863In this case, properties can be read and written in their entirety. However, an
864``append`` operaton involving such a list type has no effect:
865
866.. code-block:: python
867
868 >>> m = MyClass()
869 >>> m.contents = [5, 6]
870 >>> print(m.contents)
871 [5, 6]
872 >>> m.contents.append(7)
873 >>> print(m.contents)
874 [5, 6]
875
876To deal with both of the above situations, pybind11 provides a macro named
877``PYBIND11_MAKE_OPAQUE(T)`` that disables the template-based conversion
878machinery of types, thus rendering them *opaque*. The contents of opaque
879objects are never inspected or extracted, hence they can be passed by
880reference. For instance, to turn ``std::vector<int>`` into an opaque type, add
881the declaration
882
883.. code-block:: cpp
884
885 PYBIND11_MAKE_OPAQUE(std::vector<int>);
886
887before any binding code (e.g. invocations to ``class_::def()``, etc.). This
888macro must be specified at the top level, since instantiates a partial template
889overload. If your binding code consists of multiple compilation units, it must
890be present in every file preceding any usage of ``std::vector<int>``. Opaque
891types must also have a corresponding ``class_`` declaration to associate them
892with a name in Python, and to define a set of available operations:
893
894.. code-block:: cpp
895
896 py::class_<std::vector<int>>(m, "IntVector")
897 .def(py::init<>())
898 .def("clear", &std::vector<int>::clear)
899 .def("pop_back", &std::vector<int>::pop_back)
900 .def("__len__", [](const std::vector<int> &v) { return v.size(); })
901 .def("__iter__", [](std::vector<int> &v) {
902 return py::make_iterator(v.begin(), v.end());
903 }, py::keep_alive<0, 1>()) /* Keep vector alive while iterator is used */
904 // ....
905
906
907.. seealso::
908
909 The file :file:`example/example14.cpp` contains a complete example that
910 demonstrates how to create and expose opaque types using pybind11 in more
911 detail.
912
913.. _eigen:
914
915Transparent conversion of dense and sparse Eigen data types
916===========================================================
917
918Eigen [#f1]_ is C++ header-based library for dense and sparse linear algebra. Due to
919its popularity and widespread adoption, pybind11 provides transparent
920conversion support between Eigen and Scientific Python linear algebra data types.
921
922Specifically, when including the optional header file :file:`pybind11/eigen.h`,
Wenzel Jakob178c8a82016-05-10 15:59:01 +0100923pybind11 will automatically and transparently convert
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200924
9251. Static and dynamic Eigen dense vectors and matrices to instances of
926 ``numpy.ndarray`` (and vice versa).
927
9281. Eigen sparse vectors and matrices to instances of
929 ``scipy.sparse.csr_matrix``/``scipy.sparse.csc_matrix`` (and vice versa).
930
931This makes it possible to bind most kinds of functions that rely on these types.
932One major caveat are functions that take Eigen matrices *by reference* and modify
933them somehow, in which case the information won't be propagated to the caller.
934
935.. code-block:: cpp
936
937 /* The Python bindings of this function won't replicate
938 the intended effect of modifying the function argument */
939 void scale_by_2(Eigen::Vector3f &v) {
940 v *= 2;
941 }
942
943To see why this is, refer to the section on :ref:`opaque` (although that
944section specifically covers STL data types, the underlying issue is the same).
945The next two sections discuss an efficient alternative for exposing the
946underlying native Eigen types as opaque objects in a way that still integrates
947with NumPy and SciPy.
948
949.. [#f1] http://eigen.tuxfamily.org
950
951.. seealso::
952
953 The file :file:`example/eigen.cpp` contains a complete example that
954 shows how to pass Eigen sparse and dense data types in more detail.
955
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200956Buffer protocol
957===============
958
959Python supports an extremely general and convenient approach for exchanging
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200960data between plugin libraries. Types can expose a buffer view [#f2]_, which
961provides fast direct access to the raw internal data representation. Suppose we
962want to bind the following simplistic Matrix class:
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200963
964.. code-block:: cpp
965
966 class Matrix {
967 public:
968 Matrix(size_t rows, size_t cols) : m_rows(rows), m_cols(cols) {
969 m_data = new float[rows*cols];
970 }
971 float *data() { return m_data; }
972 size_t rows() const { return m_rows; }
973 size_t cols() const { return m_cols; }
974 private:
975 size_t m_rows, m_cols;
976 float *m_data;
977 };
978
979The following binding code exposes the ``Matrix`` contents as a buffer object,
Wenzel Jakob8e93df82016-05-01 02:36:58 +0200980making it possible to cast Matrices into NumPy arrays. It is even possible to
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200981completely avoid copy operations with Python expressions like
982``np.array(matrix_instance, copy = False)``.
983
984.. code-block:: cpp
985
986 py::class_<Matrix>(m, "Matrix")
987 .def_buffer([](Matrix &m) -> py::buffer_info {
988 return py::buffer_info(
Wenzel Jakob876eeab2016-05-04 22:22:48 +0200989 m.data(), /* Pointer to buffer */
990 sizeof(float), /* Size of one scalar */
991 py::format_descriptor<float>::value, /* Python struct-style format descriptor */
992 2, /* Number of dimensions */
993 { m.rows(), m.cols() }, /* Buffer dimensions */
994 { sizeof(float) * m.rows(), /* Strides (in bytes) for each index */
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200995 sizeof(float) }
996 );
997 });
998
999The snippet above binds a lambda function, which can create ``py::buffer_info``
1000description records on demand describing a given matrix. The contents of
1001``py::buffer_info`` mirror the Python buffer protocol specification.
1002
1003.. code-block:: cpp
1004
1005 struct buffer_info {
1006 void *ptr;
1007 size_t itemsize;
1008 std::string format;
1009 int ndim;
1010 std::vector<size_t> shape;
1011 std::vector<size_t> strides;
1012 };
1013
1014To create a C++ function that can take a Python buffer object as an argument,
1015simply use the type ``py::buffer`` as one of its arguments. Buffers can exist
1016in a great variety of configurations, hence some safety checks are usually
1017necessary in the function body. Below, you can see an basic example on how to
1018define a custom constructor for the Eigen double precision matrix
1019(``Eigen::MatrixXd``) type, which supports initialization from compatible
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001020buffer objects (e.g. a NumPy matrix).
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001021
1022.. code-block:: cpp
1023
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001024 /* Bind MatrixXd (or some other Eigen type) to Python */
1025 typedef Eigen::MatrixXd Matrix;
1026
1027 typedef Matrix::Scalar Scalar;
1028 constexpr bool rowMajor = Matrix::Flags & Eigen::RowMajorBit;
1029
1030 py::class_<Matrix>(m, "Matrix")
1031 .def("__init__", [](Matrix &m, py::buffer b) {
Wenzel Jakobe7628532016-05-05 10:04:44 +02001032 typedef Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic> Strides;
Wenzel Jakobe7628532016-05-05 10:04:44 +02001033
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001034 /* Request a buffer descriptor from Python */
1035 py::buffer_info info = b.request();
1036
1037 /* Some sanity checks ... */
Wenzel Jakobe7628532016-05-05 10:04:44 +02001038 if (info.format != py::format_descriptor<Scalar>::value)
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001039 throw std::runtime_error("Incompatible format: expected a double array!");
1040
1041 if (info.ndim != 2)
1042 throw std::runtime_error("Incompatible buffer dimension!");
1043
Wenzel Jakobe7628532016-05-05 10:04:44 +02001044 auto strides = Strides(
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001045 info.strides[rowMajor ? 0 : 1] / sizeof(Scalar),
1046 info.strides[rowMajor ? 1 : 0] / sizeof(Scalar));
Wenzel Jakobe7628532016-05-05 10:04:44 +02001047
1048 auto map = Eigen::Map<Matrix, 0, Strides>(
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001049 static_cat<Scalar *>(info.ptr), info.shape[0], info.shape[1], strides);
Wenzel Jakobe7628532016-05-05 10:04:44 +02001050
1051 new (&m) Matrix(map);
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001052 });
1053
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001054For reference, the ``def_buffer()`` call for this Eigen data type should look
1055as follows:
1056
1057.. code-block:: cpp
1058
1059 .def_buffer([](Matrix &m) -> py::buffer_info {
1060 return py::buffer_info(
1061 m.data(), /* Pointer to buffer */
1062 sizeof(Scalar), /* Size of one scalar */
1063 /* Python struct-style format descriptor */
1064 py::format_descriptor<Scalar>::value,
1065 /* Number of dimensions */
1066 2,
1067 /* Buffer dimensions */
1068 { (size_t) m.rows(),
1069 (size_t) m.cols() },
1070 /* Strides (in bytes) for each index */
1071 { sizeof(Scalar) * (rowMajor ? m.cols() : 1),
1072 sizeof(Scalar) * (rowMajor ? 1 : m.rows()) }
1073 );
1074 })
1075
1076For a much easier approach of binding Eigen types (although with some
1077limitations), refer to the section on :ref:`eigen`.
1078
Wenzel Jakob93296692015-10-13 23:21:54 +02001079.. seealso::
1080
1081 The file :file:`example/example7.cpp` contains a complete example that
1082 demonstrates using the buffer protocol with pybind11 in more detail.
1083
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001084.. [#f2] http://docs.python.org/3/c-api/buffer.html
Wenzel Jakob978e3762016-04-07 18:00:41 +02001085
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001086NumPy support
1087=============
1088
1089By exchanging ``py::buffer`` with ``py::array`` in the above snippet, we can
1090restrict the function so that it only accepts NumPy arrays (rather than any
Wenzel Jakob978e3762016-04-07 18:00:41 +02001091type of Python object satisfying the buffer protocol).
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001092
1093In many situations, we want to define a function which only accepts a NumPy
Wenzel Jakob93296692015-10-13 23:21:54 +02001094array of a certain data type. This is possible via the ``py::array_t<T>``
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001095template. For instance, the following function requires the argument to be a
Wenzel Jakobf1032df2016-05-05 10:00:00 +02001096NumPy array containing double precision values.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001097
1098.. code-block:: cpp
1099
Wenzel Jakob93296692015-10-13 23:21:54 +02001100 void f(py::array_t<double> array);
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001101
Wenzel Jakobf1032df2016-05-05 10:00:00 +02001102When it is invoked with a different type (e.g. an integer or a list of
1103integers), the binding code will attempt to cast the input into a NumPy array
1104of the requested type. Note that this feature requires the
1105:file:``pybind11/numpy.h`` header to be included.
1106
1107Data in NumPy arrays is not guaranteed to packed in a dense manner;
1108furthermore, entries can be separated by arbitrary column and row strides.
1109Sometimes, it can be useful to require a function to only accept dense arrays
1110using either the C (row-major) or Fortran (column-major) ordering. This can be
1111accomplished via a second template argument with values ``py::array::c_style``
1112or ``py::array::f_style``.
1113
1114.. code-block:: cpp
1115
Wenzel Jakobb47a9de2016-05-19 16:02:09 +02001116 void f(py::array_t<double, py::array::c_style | py::array::forcecast> array);
Wenzel Jakobf1032df2016-05-05 10:00:00 +02001117
Wenzel Jakobb47a9de2016-05-19 16:02:09 +02001118The ``py::array::forcecast`` argument is the default value of the second
1119template paramenter, and it ensures that non-conforming arguments are converted
1120into an array satisfying the specified requirements instead of trying the next
1121function overload.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001122
1123Vectorizing functions
1124=====================
1125
1126Suppose we want to bind a function with the following signature to Python so
1127that it can process arbitrary NumPy array arguments (vectors, matrices, general
1128N-D arrays) in addition to its normal arguments:
1129
1130.. code-block:: cpp
1131
1132 double my_func(int x, float y, double z);
1133
Wenzel Jakob8f4eb002015-10-15 18:13:33 +02001134After including the ``pybind11/numpy.h`` header, this is extremely simple:
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001135
1136.. code-block:: cpp
1137
1138 m.def("vectorized_func", py::vectorize(my_func));
1139
1140Invoking the function like below causes 4 calls to be made to ``my_func`` with
Wenzel Jakob8e93df82016-05-01 02:36:58 +02001141each of the array elements. The significant advantage of this compared to
Wenzel Jakob978e3762016-04-07 18:00:41 +02001142solutions like ``numpy.vectorize()`` is that the loop over the elements runs
1143entirely on the C++ side and can be crunched down into a tight, optimized loop
1144by the compiler. The result is returned as a NumPy array of type
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001145``numpy.dtype.float64``.
1146
1147.. code-block:: python
1148
1149 >>> x = np.array([[1, 3],[5, 7]])
1150 >>> y = np.array([[2, 4],[6, 8]])
1151 >>> z = 3
1152 >>> result = vectorized_func(x, y, z)
1153
1154The scalar argument ``z`` is transparently replicated 4 times. The input
1155arrays ``x`` and ``y`` are automatically converted into the right types (they
1156are of type ``numpy.dtype.int64`` but need to be ``numpy.dtype.int32`` and
1157``numpy.dtype.float32``, respectively)
1158
Wenzel Jakob8e93df82016-05-01 02:36:58 +02001159Sometimes we might want to explicitly exclude an argument from the vectorization
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001160because it makes little sense to wrap it in a NumPy array. For instance,
1161suppose the function signature was
1162
1163.. code-block:: cpp
1164
1165 double my_func(int x, float y, my_custom_type *z);
1166
1167This can be done with a stateful Lambda closure:
1168
1169.. code-block:: cpp
1170
1171 // Vectorize a lambda function with a capture object (e.g. to exclude some arguments from the vectorization)
1172 m.def("vectorized_func",
Wenzel Jakob93296692015-10-13 23:21:54 +02001173 [](py::array_t<int> x, py::array_t<float> y, my_custom_type *z) {
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001174 auto stateful_closure = [z](int x, float y) { return my_func(x, y, z); };
1175 return py::vectorize(stateful_closure)(x, y);
1176 }
1177 );
1178
Wenzel Jakob61587162016-01-18 22:38:52 +01001179In cases where the computation is too complicated to be reduced to
1180``vectorize``, it will be necessary to create and access the buffer contents
1181manually. The following snippet contains a complete example that shows how this
1182works (the code is somewhat contrived, since it could have been done more
1183simply using ``vectorize``).
1184
1185.. code-block:: cpp
1186
1187 #include <pybind11/pybind11.h>
1188 #include <pybind11/numpy.h>
1189
1190 namespace py = pybind11;
1191
1192 py::array_t<double> add_arrays(py::array_t<double> input1, py::array_t<double> input2) {
1193 auto buf1 = input1.request(), buf2 = input2.request();
1194
1195 if (buf1.ndim != 1 || buf2.ndim != 1)
1196 throw std::runtime_error("Number of dimensions must be one");
1197
1198 if (buf1.shape[0] != buf2.shape[0])
1199 throw std::runtime_error("Input shapes must match");
1200
1201 auto result = py::array(py::buffer_info(
1202 nullptr, /* Pointer to data (nullptr -> ask NumPy to allocate!) */
1203 sizeof(double), /* Size of one item */
Nils Wernerf7048f22016-05-19 11:17:17 +02001204 py::format_descriptor<double>::value(), /* Buffer format */
Wenzel Jakob61587162016-01-18 22:38:52 +01001205 buf1.ndim, /* How many dimensions? */
1206 { buf1.shape[0] }, /* Number of elements for each dimension */
1207 { sizeof(double) } /* Strides for each dimension */
1208 ));
1209
1210 auto buf3 = result.request();
1211
1212 double *ptr1 = (double *) buf1.ptr,
1213 *ptr2 = (double *) buf2.ptr,
1214 *ptr3 = (double *) buf3.ptr;
1215
1216 for (size_t idx = 0; idx < buf1.shape[0]; idx++)
1217 ptr3[idx] = ptr1[idx] + ptr2[idx];
1218
1219 return result;
1220 }
1221
1222 PYBIND11_PLUGIN(test) {
1223 py::module m("test");
1224 m.def("add_arrays", &add_arrays, "Add two NumPy arrays");
1225 return m.ptr();
1226 }
1227
Wenzel Jakob93296692015-10-13 23:21:54 +02001228.. seealso::
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001229
Wenzel Jakob93296692015-10-13 23:21:54 +02001230 The file :file:`example/example10.cpp` contains a complete example that
1231 demonstrates using :func:`vectorize` in more detail.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001232
Wenzel Jakob93296692015-10-13 23:21:54 +02001233Functions taking Python objects as arguments
1234============================================
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001235
Wenzel Jakob93296692015-10-13 23:21:54 +02001236pybind11 exposes all major Python types using thin C++ wrapper classes. These
1237wrapper classes can also be used as parameters of functions in bindings, which
1238makes it possible to directly work with native Python types on the C++ side.
1239For instance, the following statement iterates over a Python ``dict``:
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001240
Wenzel Jakob93296692015-10-13 23:21:54 +02001241.. code-block:: cpp
1242
1243 void print_dict(py::dict dict) {
1244 /* Easily interact with Python types */
1245 for (auto item : dict)
1246 std::cout << "key=" << item.first << ", "
1247 << "value=" << item.second << std::endl;
1248 }
1249
1250Available types include :class:`handle`, :class:`object`, :class:`bool_`,
Wenzel Jakob27e8e102016-01-17 22:36:37 +01001251:class:`int_`, :class:`float_`, :class:`str`, :class:`bytes`, :class:`tuple`,
Wenzel Jakobf64feaf2016-04-28 14:33:45 +02001252:class:`list`, :class:`dict`, :class:`slice`, :class:`none`, :class:`capsule`,
1253:class:`iterable`, :class:`iterator`, :class:`function`, :class:`buffer`,
1254:class:`array`, and :class:`array_t`.
Wenzel Jakob93296692015-10-13 23:21:54 +02001255
Wenzel Jakob436b7312015-10-20 01:04:30 +02001256In this kind of mixed code, it is often necessary to convert arbitrary C++
1257types to Python, which can be done using :func:`cast`:
1258
1259.. code-block:: cpp
1260
1261 MyClass *cls = ..;
1262 py::object obj = py::cast(cls);
1263
1264The reverse direction uses the following syntax:
1265
1266.. code-block:: cpp
1267
1268 py::object obj = ...;
1269 MyClass *cls = obj.cast<MyClass *>();
1270
1271When conversion fails, both directions throw the exception :class:`cast_error`.
Wenzel Jakob178c8a82016-05-10 15:59:01 +01001272It is also possible to call python functions via ``operator()``.
1273
1274.. code-block:: cpp
1275
1276 py::function f = <...>;
1277 py::object result_py = f(1234, "hello", some_instance);
1278 MyClass &result = result_py.cast<MyClass>();
1279
1280The special ``f(*args)`` and ``f(*args, **kwargs)`` syntax is also supported to
1281supply arbitrary argument and keyword lists, although these cannot be mixed
1282with other parameters.
1283
1284.. code-block:: cpp
1285
1286 py::function f = <...>;
1287 py::tuple args = py::make_tuple(1234);
1288 py::dict kwargs;
1289 kwargs["y"] = py::cast(5678);
1290 py::object result = f(*args, **kwargs);
Wenzel Jakob436b7312015-10-20 01:04:30 +02001291
Wenzel Jakob93296692015-10-13 23:21:54 +02001292.. seealso::
1293
1294 The file :file:`example/example2.cpp` contains a complete example that
Wenzel Jakob178c8a82016-05-10 15:59:01 +01001295 demonstrates passing native Python types in more detail. The file
1296 :file:`example/example11.cpp` discusses usage of ``args`` and ``kwargs``.
Wenzel Jakob2ac50442016-01-17 22:36:35 +01001297
1298Default arguments revisited
1299===========================
1300
1301The section on :ref:`default_args` previously discussed basic usage of default
1302arguments using pybind11. One noteworthy aspect of their implementation is that
1303default arguments are converted to Python objects right at declaration time.
1304Consider the following example:
1305
1306.. code-block:: cpp
1307
1308 py::class_<MyClass>("MyClass")
1309 .def("myFunction", py::arg("arg") = SomeType(123));
1310
1311In this case, pybind11 must already be set up to deal with values of the type
1312``SomeType`` (via a prior instantiation of ``py::class_<SomeType>``), or an
1313exception will be thrown.
1314
1315Another aspect worth highlighting is that the "preview" of the default argument
1316in the function signature is generated using the object's ``__repr__`` method.
1317If not available, the signature may not be very helpful, e.g.:
1318
1319.. code-block:: python
1320
1321 FUNCTIONS
1322 ...
1323 | myFunction(...)
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001324 | Signature : (MyClass, arg : SomeType = <SomeType object at 0x101b7b080>) -> NoneType
Wenzel Jakob2ac50442016-01-17 22:36:35 +01001325 ...
1326
1327The first way of addressing this is by defining ``SomeType.__repr__``.
1328Alternatively, it is possible to specify the human-readable preview of the
1329default argument manually using the ``arg_t`` notation:
1330
1331.. code-block:: cpp
1332
1333 py::class_<MyClass>("MyClass")
1334 .def("myFunction", py::arg_t<SomeType>("arg", SomeType(123), "SomeType(123)"));
1335
Wenzel Jakobc769fce2016-03-03 12:03:30 +01001336Sometimes it may be necessary to pass a null pointer value as a default
1337argument. In this case, remember to cast it to the underlying type in question,
1338like so:
1339
1340.. code-block:: cpp
1341
1342 py::class_<MyClass>("MyClass")
1343 .def("myFunction", py::arg("arg") = (SomeType *) nullptr);
1344
Wenzel Jakob178c8a82016-05-10 15:59:01 +01001345Binding functions that accept arbitrary numbers of arguments and keywords arguments
1346===================================================================================
1347
1348Python provides a useful mechanism to define functions that accept arbitrary
1349numbers of arguments and keyword arguments:
1350
1351.. code-block:: cpp
1352
1353 def generic(*args, **kwargs):
1354 # .. do something with args and kwargs
1355
1356Such functions can also be created using pybind11:
1357
1358.. code-block:: cpp
1359
1360 void generic(py::args args, py::kwargs kwargs) {
1361 /// .. do something with args
1362 if (kwargs)
1363 /// .. do something with kwargs
1364 }
1365
1366 /// Binding code
1367 m.def("generic", &generic);
1368
1369(See ``example/example11.cpp``). The class ``py::args`` derives from
1370``py::list`` and ``py::kwargs`` derives from ``py::dict`` Note that the
1371``kwargs`` argument is invalid if no keyword arguments were actually provided.
1372Please refer to the other examples for details on how to iterate over these,
1373and on how to cast their entries into C++ objects.
1374
Wenzel Jakob2dfbade2016-01-17 22:36:37 +01001375Partitioning code over multiple extension modules
1376=================================================
1377
Wenzel Jakob90d2f5e2016-04-11 14:30:11 +02001378It's straightforward to split binding code over multiple extension modules,
1379while referencing types that are declared elsewhere. Everything "just" works
1380without any special precautions. One exception to this rule occurs when
1381extending a type declared in another extension module. Recall the basic example
1382from Section :ref:`inheritance`.
Wenzel Jakob2dfbade2016-01-17 22:36:37 +01001383
1384.. code-block:: cpp
1385
1386 py::class_<Pet> pet(m, "Pet");
1387 pet.def(py::init<const std::string &>())
1388 .def_readwrite("name", &Pet::name);
1389
1390 py::class_<Dog>(m, "Dog", pet /* <- specify parent */)
1391 .def(py::init<const std::string &>())
1392 .def("bark", &Dog::bark);
1393
1394Suppose now that ``Pet`` bindings are defined in a module named ``basic``,
1395whereas the ``Dog`` bindings are defined somewhere else. The challenge is of
1396course that the variable ``pet`` is not available anymore though it is needed
1397to indicate the inheritance relationship to the constructor of ``class_<Dog>``.
1398However, it can be acquired as follows:
1399
1400.. code-block:: cpp
1401
1402 py::object pet = (py::object) py::module::import("basic").attr("Pet");
1403
1404 py::class_<Dog>(m, "Dog", pet)
1405 .def(py::init<const std::string &>())
1406 .def("bark", &Dog::bark);
1407
Wenzel Jakob8d862b32016-03-06 13:37:22 +01001408Alternatively, we can rely on the ``base`` tag, which performs an automated
1409lookup of the corresponding Python type. However, this also requires invoking
1410the ``import`` function once to ensure that the pybind11 binding code of the
1411module ``basic`` has been executed.
1412
Wenzel Jakob8d862b32016-03-06 13:37:22 +01001413.. code-block:: cpp
1414
1415 py::module::import("basic");
1416
1417 py::class_<Dog>(m, "Dog", py::base<Pet>())
1418 .def(py::init<const std::string &>())
1419 .def("bark", &Dog::bark);
Wenzel Jakobeda978e2016-03-15 15:05:40 +01001420
Wenzel Jakob978e3762016-04-07 18:00:41 +02001421Naturally, both methods will fail when there are cyclic dependencies.
1422
Wenzel Jakob90d2f5e2016-04-11 14:30:11 +02001423Note that compiling code which has its default symbol visibility set to
1424*hidden* (e.g. via the command line flag ``-fvisibility=hidden`` on GCC/Clang) can interfere with the
1425ability to access types defined in another extension module. Workarounds
1426include changing the global symbol visibility (not recommended, because it will
1427lead unnecessarily large binaries) or manually exporting types that are
1428accessed by multiple extension modules:
1429
1430.. code-block:: cpp
1431
1432 #ifdef _WIN32
1433 # define EXPORT_TYPE __declspec(dllexport)
1434 #else
1435 # define EXPORT_TYPE __attribute__ ((visibility("default")))
1436 #endif
1437
1438 class EXPORT_TYPE Dog : public Animal {
1439 ...
1440 };
1441
1442
Wenzel Jakob1c329aa2016-04-13 02:37:36 +02001443Pickling support
1444================
1445
1446Python's ``pickle`` module provides a powerful facility to serialize and
1447de-serialize a Python object graph into a binary data stream. To pickle and
Wenzel Jakob3d0e6ff2016-04-13 11:48:10 +02001448unpickle C++ classes using pybind11, two additional functions must be provided.
Wenzel Jakob1c329aa2016-04-13 02:37:36 +02001449Suppose the class in question has the following signature:
1450
1451.. code-block:: cpp
1452
1453 class Pickleable {
1454 public:
1455 Pickleable(const std::string &value) : m_value(value) { }
1456 const std::string &value() const { return m_value; }
1457
1458 void setExtra(int extra) { m_extra = extra; }
1459 int extra() const { return m_extra; }
1460 private:
1461 std::string m_value;
1462 int m_extra = 0;
1463 };
1464
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001465The binding code including the requisite ``__setstate__`` and ``__getstate__`` methods [#f3]_
Wenzel Jakob1c329aa2016-04-13 02:37:36 +02001466looks as follows:
1467
1468.. code-block:: cpp
1469
1470 py::class_<Pickleable>(m, "Pickleable")
1471 .def(py::init<std::string>())
1472 .def("value", &Pickleable::value)
1473 .def("extra", &Pickleable::extra)
1474 .def("setExtra", &Pickleable::setExtra)
1475 .def("__getstate__", [](const Pickleable &p) {
1476 /* Return a tuple that fully encodes the state of the object */
1477 return py::make_tuple(p.value(), p.extra());
1478 })
1479 .def("__setstate__", [](Pickleable &p, py::tuple t) {
1480 if (t.size() != 2)
1481 throw std::runtime_error("Invalid state!");
1482
Wenzel Jakobd40885a2016-04-13 13:30:05 +02001483 /* Invoke the in-place constructor. Note that this is needed even
1484 when the object just has a trivial default constructor */
Wenzel Jakob1c329aa2016-04-13 02:37:36 +02001485 new (&p) Pickleable(t[0].cast<std::string>());
1486
1487 /* Assign any additional state */
1488 p.setExtra(t[1].cast<int>());
1489 });
1490
1491An instance can now be pickled as follows:
1492
1493.. code-block:: python
1494
1495 try:
1496 import cPickle as pickle # Use cPickle on Python 2.7
1497 except ImportError:
1498 import pickle
1499
1500 p = Pickleable("test_value")
1501 p.setExtra(15)
Wenzel Jakob81e09752016-04-30 23:13:03 +02001502 data = pickle.dumps(p, 2)
Wenzel Jakob1c329aa2016-04-13 02:37:36 +02001503
Wenzel Jakob81e09752016-04-30 23:13:03 +02001504Note that only the cPickle module is supported on Python 2.7. The second
1505argument to ``dumps`` is also crucial: it selects the pickle protocol version
15062, since the older version 1 is not supported. Newer versions are also fine—for
1507instance, specify ``-1`` to always use the latest available version. Beware:
1508failure to follow these instructions will cause important pybind11 memory
1509allocation routines to be skipped during unpickling, which will likely lead to
1510memory corruption and/or segmentation faults.
Wenzel Jakob1c329aa2016-04-13 02:37:36 +02001511
1512.. seealso::
1513
1514 The file :file:`example/example15.cpp` contains a complete example that
1515 demonstrates how to pickle and unpickle types using pybind11 in more detail.
1516
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001517.. [#f3] http://docs.python.org/3/library/pickle.html#pickling-class-instances
Wenzel Jakobef7a9b92016-04-13 18:41:59 +02001518
1519Generating documentation using Sphinx
1520=====================================
1521
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001522Sphinx [#f4]_ has the ability to inspect the signatures and documentation
Wenzel Jakobef7a9b92016-04-13 18:41:59 +02001523strings in pybind11-based extension modules to automatically generate beautiful
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001524documentation in a variety formats. The pbtest repository [#f5]_ contains a
Wenzel Jakobef7a9b92016-04-13 18:41:59 +02001525simple example repository which uses this approach.
1526
1527There are two potential gotchas when using this approach: first, make sure that
1528the resulting strings do not contain any :kbd:`TAB` characters, which break the
1529docstring parsing routines. You may want to use C++11 raw string literals,
1530which are convenient for multi-line comments. Conveniently, any excess
1531indentation will be automatically be removed by Sphinx. However, for this to
1532work, it is important that all lines are indented consistently, i.e.:
1533
1534.. code-block:: cpp
1535
1536 // ok
1537 m.def("foo", &foo, R"mydelimiter(
1538 The foo function
1539
1540 Parameters
1541 ----------
1542 )mydelimiter");
1543
1544 // *not ok*
1545 m.def("foo", &foo, R"mydelimiter(The foo function
1546
1547 Parameters
1548 ----------
1549 )mydelimiter");
1550
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001551.. [#f4] http://www.sphinx-doc.org
1552.. [#f5] http://github.com/pybind/pbtest