blob: 99af03976b7105835f18320d5e0841620cc9670a [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 Jakob93296692015-10-13 23:21:54 +0200271a default implementation. The binding code also needs a few minor adaptations
272(highlighted):
273
274.. code-block:: cpp
275 :emphasize-lines: 4,6,7
276
Wenzel Jakobb1b71402015-10-18 16:48:30 +0200277 PYBIND11_PLUGIN(example) {
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200278 py::module m("example", "pybind11 example plugin");
Wenzel Jakob93296692015-10-13 23:21:54 +0200279
280 py::class_<PyAnimal> animal(m, "Animal");
281 animal
282 .alias<Animal>()
283 .def(py::init<>())
284 .def("go", &Animal::go);
285
286 py::class_<Dog>(m, "Dog", animal)
287 .def(py::init<>());
288
289 m.def("call_go", &call_go);
290
291 return m.ptr();
292 }
293
294Importantly, the trampoline helper class is used as the template argument to
295:class:`class_`, and a call to :func:`class_::alias` informs the binding
296generator that this is merely an alias for the underlying type ``Animal``.
297Following this, we are able to define a constructor as usual.
298
299The Python session below shows how to override ``Animal::go`` and invoke it via
300a virtual method call.
301
Wenzel Jakobde3ad072016-02-02 11:38:21 +0100302.. code-block:: python
Wenzel Jakob93296692015-10-13 23:21:54 +0200303
304 >>> from example import *
305 >>> d = Dog()
306 >>> call_go(d)
307 u'woof! woof! woof! '
308 >>> class Cat(Animal):
309 ... def go(self, n_times):
310 ... return "meow! " * n_times
311 ...
312 >>> c = Cat()
313 >>> call_go(c)
314 u'meow! meow! meow! '
315
316.. seealso::
317
318 The file :file:`example/example12.cpp` contains a complete example that
319 demonstrates how to override virtual functions using pybind11 in more
320 detail.
321
Wenzel Jakobecdd8682015-12-07 18:17:58 +0100322
323Global Interpreter Lock (GIL)
324=============================
325
326The classes :class:`gil_scoped_release` and :class:`gil_scoped_acquire` can be
327used to acquire and release the global interpreter lock in the body of a C++
328function call. In this way, long-running C++ code can be parallelized using
329multiple Python threads. Taking the previous section as an example, this could
330be realized as follows (important changes highlighted):
331
332.. code-block:: cpp
333 :emphasize-lines: 8,9,33,34
334
335 class PyAnimal : public Animal {
336 public:
337 /* Inherit the constructors */
338 using Animal::Animal;
339
340 /* Trampoline (need one for each virtual function) */
341 std::string go(int n_times) {
342 /* Acquire GIL before calling Python code */
Wenzel Jakoba4caa852015-12-14 12:39:02 +0100343 py::gil_scoped_acquire acquire;
Wenzel Jakobecdd8682015-12-07 18:17:58 +0100344
345 PYBIND11_OVERLOAD_PURE(
346 std::string, /* Return type */
347 Animal, /* Parent class */
348 go, /* Name of function */
349 n_times /* Argument(s) */
350 );
351 }
352 };
353
354 PYBIND11_PLUGIN(example) {
355 py::module m("example", "pybind11 example plugin");
356
357 py::class_<PyAnimal> animal(m, "Animal");
358 animal
359 .alias<Animal>()
360 .def(py::init<>())
361 .def("go", &Animal::go);
362
363 py::class_<Dog>(m, "Dog", animal)
364 .def(py::init<>());
365
366 m.def("call_go", [](Animal *animal) -> std::string {
367 /* Release GIL before calling into (potentially long-running) C++ code */
Wenzel Jakoba4caa852015-12-14 12:39:02 +0100368 py::gil_scoped_release release;
Wenzel Jakobecdd8682015-12-07 18:17:58 +0100369 return call_go(animal);
370 });
371
372 return m.ptr();
373 }
374
Wenzel Jakob93296692015-10-13 23:21:54 +0200375Passing STL data structures
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200376===========================
377
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200378When including the additional header file :file:`pybind11/stl.h`, conversions
Wenzel Jakob978e3762016-04-07 18:00:41 +0200379between ``std::vector<>``, ``std::list<>``, ``std::set<>``, and ``std::map<>``
380and the Python ``list``, ``set`` and ``dict`` data structures are automatically
381enabled. The types ``std::pair<>`` and ``std::tuple<>`` are already supported
382out of the box with just the core :file:`pybind11/pybind11.h` header.
Wenzel Jakob93296692015-10-13 23:21:54 +0200383
384.. note::
385
Wenzel Jakob44db04f2015-12-14 12:40:45 +0100386 Arbitrary nesting of any of these types is supported.
Wenzel Jakob93296692015-10-13 23:21:54 +0200387
388.. seealso::
389
390 The file :file:`example/example2.cpp` contains a complete example that
391 demonstrates how to pass STL data types in more detail.
392
Wenzel Jakobb2825952016-04-13 23:33:00 +0200393Binding sequence data types, iterators, the slicing protocol, etc.
394==================================================================
Wenzel Jakob93296692015-10-13 23:21:54 +0200395
396Please refer to the supplemental example for details.
397
398.. seealso::
399
400 The file :file:`example/example6.cpp` contains a complete example that
401 shows how to bind a sequence data type, including length queries
402 (``__len__``), iterators (``__iter__``), the slicing protocol and other
403 kinds of useful operations.
404
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200405Return value policies
406=====================
407
Wenzel Jakob93296692015-10-13 23:21:54 +0200408Python and C++ use wildly different ways of managing the memory and lifetime of
409objects managed by them. This can lead to issues when creating bindings for
410functions that return a non-trivial type. Just by looking at the type
411information, it is not clear whether Python should take charge of the returned
412value and eventually free its resources, or if this is handled on the C++ side.
413For this reason, pybind11 provides a several `return value policy` annotations
414that can be passed to the :func:`module::def` and :func:`class_::def`
Wenzel Jakob61d67f02015-12-14 12:53:06 +0100415functions. The default policy is :enum:`return_value_policy::automatic`.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200416
Wenzel Jakobf64feaf2016-04-28 14:33:45 +0200417.. tabularcolumns:: |p{0.5\textwidth}|p{0.45\textwidth}|
418
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200419+--------------------------------------------------+----------------------------------------------------------------------------+
420| Return value policy | Description |
421+==================================================+============================================================================+
422| :enum:`return_value_policy::automatic` | This is the default return value policy, which falls back to the policy |
423| | :enum:`return_value_policy::take_ownership` when the return value is a |
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200424| | pointer. Otherwise, it uses :enum:`return_value::move` or |
425| | :enum:`return_value::copy` for rvalue and lvalue references, respectively. |
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200426| | See below for a description of what all of these different policies do. |
427+--------------------------------------------------+----------------------------------------------------------------------------+
428| :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 +0200429| | return value is a pointer. You probably won't need to use this. |
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200430+--------------------------------------------------+----------------------------------------------------------------------------+
431| :enum:`return_value_policy::take_ownership` | Reference an existing object (i.e. do not create a new copy) and take |
432| | ownership. Python will call the destructor and delete operator when the |
433| | object's reference count reaches zero. Undefined behavior ensues when the |
434| | C++ side does the same.. |
435+--------------------------------------------------+----------------------------------------------------------------------------+
436| :enum:`return_value_policy::copy` | Create a new copy of the returned object, which will be owned by Python. |
437| | This policy is comparably safe because the lifetimes of the two instances |
438| | are decoupled. |
439+--------------------------------------------------+----------------------------------------------------------------------------+
440| :enum:`return_value_policy::move` | Use ``std::move`` to move the return value contents into a new instance |
441| | that will be owned by Python. This policy is comparably safe because the |
442| | lifetimes of the two instances (move source and destination) are decoupled.|
443+--------------------------------------------------+----------------------------------------------------------------------------+
444| :enum:`return_value_policy::reference` | Reference an existing object, but do not take ownership. The C++ side is |
445| | responsible for managing the object's lifetime and deallocating it when |
446| | it is no longer used. Warning: undefined behavior will ensue when the C++ |
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200447| | side deletes an object that is still referenced and used by Python. |
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200448+--------------------------------------------------+----------------------------------------------------------------------------+
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200449| :enum:`return_value_policy::reference_internal` | This policy only applies to methods and properties. It references the |
450| | object without taking ownership similar to the above |
451| | :enum:`return_value_policy::reference` policy. In contrast to that policy, |
452| | the function or property's implicit ``this`` argument (called the *parent*)|
453| | is considered to be the the owner of the return value (the *child*). |
454| | pybind11 then couples the lifetime of the parent to the child via a |
455| | reference relationship that ensures that the parent cannot be garbage |
456| | collected while Python is still using the child. More advanced variations |
457| | of this scheme are also possible using combinations of |
458| | :enum:`return_value_policy::reference` and the :class:`keep_alive` call |
459| | policy described next. |
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200460+--------------------------------------------------+----------------------------------------------------------------------------+
Wenzel Jakob93296692015-10-13 23:21:54 +0200461
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200462The following example snippet shows a use case of the
Wenzel Jakob93296692015-10-13 23:21:54 +0200463:enum:`return_value_policy::reference_internal` policy.
464
465.. code-block:: cpp
466
467 class Example {
468 public:
469 Internal &get_internal() { return internal; }
470 private:
471 Internal internal;
472 };
473
Wenzel Jakobb1b71402015-10-18 16:48:30 +0200474 PYBIND11_PLUGIN(example) {
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200475 py::module m("example", "pybind11 example plugin");
Wenzel Jakob93296692015-10-13 23:21:54 +0200476
477 py::class_<Example>(m, "Example")
478 .def(py::init<>())
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200479 .def("get_internal", &Example::get_internal, "Return the internal data",
480 py::return_value_policy::reference_internal);
Wenzel Jakob93296692015-10-13 23:21:54 +0200481
482 return m.ptr();
483 }
484
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200485.. warning::
486
487 Code with invalid call policies might access unitialized memory or free
488 data structures multiple times, which can lead to hard-to-debug
489 non-determinism and segmentation faults, hence it is worth spending the
490 time to understand all the different options in the table above.
491
492.. note::
493
494 The next section on :ref:`call_policies` discusses *call policies* that can be
495 specified *in addition* to a return value policy from the list above. Call
496 policies indicate reference relationships that can involve both return values
497 and parameters of functions.
498
499.. note::
500
501 As an alternative to elaborate call policies and lifetime management logic,
502 consider using smart pointers (see the section on :ref:`smart_pointers` for
503 details). Smart pointers can tell whether an object is still referenced from
504 C++ or Python, which generally eliminates the kinds of inconsistencies that
505 can lead to crashes or undefined behavior. For functions returning smart
506 pointers, it is not necessary to specify a return value policy.
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100507
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200508.. _call_policies:
509
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100510Additional call policies
511========================
512
513In addition to the above return value policies, further `call policies` can be
514specified to indicate dependencies between parameters. There is currently just
515one policy named ``keep_alive<Nurse, Patient>``, which indicates that the
516argument with index ``Patient`` should be kept alive at least until the
517argument with index ``Nurse`` is freed by the garbage collector; argument
Wenzel Jakob8e93df82016-05-01 02:36:58 +0200518indices start at one, while zero refers to the return value. For methods, index
519one refers to the implicit ``this`` pointer, while regular arguments begin at
520index two. Arbitrarily many call policies can be specified.
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100521
Wenzel Jakob8e93df82016-05-01 02:36:58 +0200522Consider the following example: the binding code for a list append operation
523that ties the lifetime of the newly added element to the underlying container
524might be declared as follows:
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100525
526.. code-block:: cpp
527
528 py::class_<List>(m, "List")
529 .def("append", &List::append, py::keep_alive<1, 2>());
530
531.. note::
532
533 ``keep_alive`` is analogous to the ``with_custodian_and_ward`` (if Nurse,
534 Patient != 0) and ``with_custodian_and_ward_postcall`` (if Nurse/Patient ==
535 0) policies from Boost.Python.
536
Wenzel Jakob61587162016-01-18 22:38:52 +0100537.. seealso::
538
539 The file :file:`example/example13.cpp` contains a complete example that
540 demonstrates using :class:`keep_alive` in more detail.
541
Wenzel Jakob93296692015-10-13 23:21:54 +0200542Implicit type conversions
543=========================
544
545Suppose that instances of two types ``A`` and ``B`` are used in a project, and
Wenzel Jakob8e93df82016-05-01 02:36:58 +0200546that an ``A`` can easily be converted into an instance of type ``B`` (examples of this
Wenzel Jakob93296692015-10-13 23:21:54 +0200547could be a fixed and an arbitrary precision number type).
548
549.. code-block:: cpp
550
551 py::class_<A>(m, "A")
552 /// ... members ...
553
554 py::class_<B>(m, "B")
555 .def(py::init<A>())
556 /// ... members ...
557
558 m.def("func",
559 [](const B &) { /* .... */ }
560 );
561
562To invoke the function ``func`` using a variable ``a`` containing an ``A``
563instance, we'd have to write ``func(B(a))`` in Python. On the other hand, C++
564will automatically apply an implicit type conversion, which makes it possible
565to directly write ``func(a)``.
566
567In this situation (i.e. where ``B`` has a constructor that converts from
568``A``), the following statement enables similar implicit conversions on the
569Python side:
570
571.. code-block:: cpp
572
573 py::implicitly_convertible<A, B>();
574
Wenzel Jakob9f0dfce2016-04-06 17:38:18 +0200575Unique pointers
576===============
577
578Given a class ``Example`` with Python bindings, it's possible to return
579instances wrapped in C++11 unique pointers, like so
580
581.. code-block:: cpp
582
583 std::unique_ptr<Example> create_example() { return std::unique_ptr<Example>(new Example()); }
584
585.. code-block:: cpp
586
587 m.def("create_example", &create_example);
588
589In other words, there is nothing special that needs to be done. While returning
590unique pointers in this way is allowed, it is *illegal* to use them as function
591arguments. For instance, the following function signature cannot be processed
592by pybind11.
593
594.. code-block:: cpp
595
596 void do_something_with_example(std::unique_ptr<Example> ex) { ... }
597
598The above signature would imply that Python needs to give up ownership of an
599object that is passed to this function, which is generally not possible (for
600instance, the object might be referenced elsewhere).
601
Wenzel Jakobf7b58742016-04-25 23:04:27 +0200602.. _smart_pointers:
603
Wenzel Jakob93296692015-10-13 23:21:54 +0200604Smart pointers
605==============
606
Wenzel Jakob9f0dfce2016-04-06 17:38:18 +0200607This section explains how to pass values that are wrapped in "smart" pointer
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200608types with internal reference counting. For the simpler C++11 unique pointers,
609refer to the previous section.
Wenzel Jakob9f0dfce2016-04-06 17:38:18 +0200610
Wenzel Jakobe84f5572016-04-26 23:19:19 +0200611The binding generator for classes, :class:`class_`, takes an optional second
Wenzel Jakob93296692015-10-13 23:21:54 +0200612template type, which denotes a special *holder* type that is used to manage
613references to the object. When wrapping a type named ``Type``, the default
614value of this template parameter is ``std::unique_ptr<Type>``, which means that
615the object is deallocated when Python's reference count goes to zero.
616
Wenzel Jakob1853b652015-10-18 15:38:50 +0200617It is possible to switch to other types of reference counting wrappers or smart
618pointers, which is useful in codebases that rely on them. For instance, the
619following snippet causes ``std::shared_ptr`` to be used instead.
Wenzel Jakob93296692015-10-13 23:21:54 +0200620
621.. code-block:: cpp
622
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100623 py::class_<Example, std::shared_ptr<Example> /* <- holder type */> obj(m, "Example");
Wenzel Jakob5ef12192015-12-15 17:07:35 +0100624
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100625Note that any particular class can only be associated with a single holder type.
Wenzel Jakob93296692015-10-13 23:21:54 +0200626
Wenzel Jakob1853b652015-10-18 15:38:50 +0200627To enable transparent conversions for functions that take shared pointers as an
Wenzel Jakob5ef12192015-12-15 17:07:35 +0100628argument or that return them, a macro invocation similar to the following must
Wenzel Jakob1853b652015-10-18 15:38:50 +0200629be declared at the top level before any binding code:
630
631.. code-block:: cpp
632
Wenzel Jakobb1b71402015-10-18 16:48:30 +0200633 PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
Wenzel Jakob1853b652015-10-18 15:38:50 +0200634
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100635.. note::
Wenzel Jakob61d67f02015-12-14 12:53:06 +0100636
637 The first argument of :func:`PYBIND11_DECLARE_HOLDER_TYPE` should be a
638 placeholder name that is used as a template parameter of the second
639 argument. Thus, feel free to use any identifier, but use it consistently on
640 both sides; also, don't use the name of a type that already exists in your
641 codebase.
642
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100643One potential stumbling block when using holder types is that they need to be
644applied consistently. Can you guess what's broken about the following binding
645code?
Wenzel Jakob6e213c92015-11-24 23:05:58 +0100646
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100647.. code-block:: cpp
Wenzel Jakob6e213c92015-11-24 23:05:58 +0100648
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100649 class Child { };
Wenzel Jakob5ef12192015-12-15 17:07:35 +0100650
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100651 class Parent {
652 public:
653 Parent() : child(std::make_shared<Child>()) { }
654 Child *get_child() { return child.get(); } /* Hint: ** DON'T DO THIS ** */
655 private:
656 std::shared_ptr<Child> child;
657 };
Wenzel Jakob5ef12192015-12-15 17:07:35 +0100658
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100659 PYBIND11_PLUGIN(example) {
660 py::module m("example");
Wenzel Jakob5ef12192015-12-15 17:07:35 +0100661
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100662 py::class_<Child, std::shared_ptr<Child>>(m, "Child");
663
664 py::class_<Parent, std::shared_ptr<Parent>>(m, "Parent")
665 .def(py::init<>())
666 .def("get_child", &Parent::get_child);
667
668 return m.ptr();
669 }
670
671The following Python code will cause undefined behavior (and likely a
672segmentation fault).
673
674.. code-block:: python
675
676 from example import Parent
677 print(Parent().get_child())
678
679The problem is that ``Parent::get_child()`` returns a pointer to an instance of
680``Child``, but the fact that this instance is already managed by
681``std::shared_ptr<...>`` is lost when passing raw pointers. In this case,
682pybind11 will create a second independent ``std::shared_ptr<...>`` that also
683claims ownership of the pointer. In the end, the object will be freed **twice**
684since these shared pointers have no way of knowing about each other.
685
686There are two ways to resolve this issue:
687
6881. For types that are managed by a smart pointer class, never use raw pointers
689 in function arguments or return values. In other words: always consistently
690 wrap pointers into their designated holder types (such as
691 ``std::shared_ptr<...>``). In this case, the signature of ``get_child()``
692 should be modified as follows:
693
694.. code-block:: cpp
695
696 std::shared_ptr<Child> get_child() { return child; }
697
6982. Adjust the definition of ``Child`` by specifying
699 ``std::enable_shared_from_this<T>`` (see cppreference_ for details) as a
700 base class. This adds a small bit of information to ``Child`` that allows
701 pybind11 to realize that there is already an existing
702 ``std::shared_ptr<...>`` and communicate with it. In this case, the
703 declaration of ``Child`` should look as follows:
Wenzel Jakob5ef12192015-12-15 17:07:35 +0100704
Wenzel Jakob6e213c92015-11-24 23:05:58 +0100705.. _cppreference: http://en.cppreference.com/w/cpp/memory/enable_shared_from_this
706
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100707.. code-block:: cpp
708
709 class Child : public std::enable_shared_from_this<Child> { };
710
Wenzel Jakob5ef12192015-12-15 17:07:35 +0100711.. seealso::
712
713 The file :file:`example/example8.cpp` contains a complete example that
714 demonstrates how to work with custom reference-counting holder types in
715 more detail.
716
Wenzel Jakob93296692015-10-13 23:21:54 +0200717.. _custom_constructors:
718
719Custom constructors
720===================
721
722The syntax for binding constructors was previously introduced, but it only
723works when a constructor with the given parameters actually exists on the C++
724side. To extend this to more general cases, let's take a look at what actually
725happens under the hood: the following statement
726
727.. code-block:: cpp
728
729 py::class_<Example>(m, "Example")
730 .def(py::init<int>());
731
732is short hand notation for
733
734.. code-block:: cpp
735
736 py::class_<Example>(m, "Example")
737 .def("__init__",
738 [](Example &instance, int arg) {
739 new (&instance) Example(arg);
740 }
741 );
742
743In other words, :func:`init` creates an anonymous function that invokes an
744in-place constructor. Memory allocation etc. is already take care of beforehand
745within pybind11.
746
747Catching and throwing exceptions
748================================
749
750When C++ code invoked from Python throws an ``std::exception``, it is
751automatically converted into a Python ``Exception``. pybind11 defines multiple
752special exception classes that will map to different types of Python
753exceptions:
754
Wenzel Jakobf64feaf2016-04-28 14:33:45 +0200755.. tabularcolumns:: |p{0.5\textwidth}|p{0.45\textwidth}|
756
Wenzel Jakob978e3762016-04-07 18:00:41 +0200757+--------------------------------------+------------------------------+
758| C++ exception type | Python exception type |
759+======================================+==============================+
760| :class:`std::exception` | ``RuntimeError`` |
761+--------------------------------------+------------------------------+
762| :class:`std::bad_alloc` | ``MemoryError`` |
763+--------------------------------------+------------------------------+
764| :class:`std::domain_error` | ``ValueError`` |
765+--------------------------------------+------------------------------+
766| :class:`std::invalid_argument` | ``ValueError`` |
767+--------------------------------------+------------------------------+
768| :class:`std::length_error` | ``ValueError`` |
769+--------------------------------------+------------------------------+
770| :class:`std::out_of_range` | ``ValueError`` |
771+--------------------------------------+------------------------------+
772| :class:`std::range_error` | ``ValueError`` |
773+--------------------------------------+------------------------------+
774| :class:`pybind11::stop_iteration` | ``StopIteration`` (used to |
775| | implement custom iterators) |
776+--------------------------------------+------------------------------+
777| :class:`pybind11::index_error` | ``IndexError`` (used to |
778| | indicate out of bounds |
779| | accesses in ``__getitem__``, |
780| | ``__setitem__``, etc.) |
781+--------------------------------------+------------------------------+
Sergey Lyskova95bde12016-05-08 19:31:55 -0400782| :class:`pybind11::value_error` | ``ValueError`` (used to |
783| | indicate wrong value passed |
784| | in ``container.remove(...)`` |
785+--------------------------------------+------------------------------+
Wenzel Jakob978e3762016-04-07 18:00:41 +0200786| :class:`pybind11::error_already_set` | Indicates that the Python |
787| | exception flag has already |
788| | been initialized |
789+--------------------------------------+------------------------------+
Wenzel Jakob93296692015-10-13 23:21:54 +0200790
791When a Python function invoked from C++ throws an exception, it is converted
792into a C++ exception of type :class:`error_already_set` whose string payload
793contains a textual summary.
794
795There is also a special exception :class:`cast_error` that is thrown by
796:func:`handle::call` when the input arguments cannot be converted to Python
797objects.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200798
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200799.. _opaque:
800
801Treating STL data structures as opaque objects
802==============================================
803
804pybind11 heavily relies on a template matching mechanism to convert parameters
805and return values that are constructed from STL data types such as vectors,
806linked lists, hash tables, etc. This even works in a recursive manner, for
807instance to deal with lists of hash maps of pairs of elementary and custom
808types, etc.
809
810However, a fundamental limitation of this approach is that internal conversions
811between Python and C++ types involve a copy operation that prevents
812pass-by-reference semantics. What does this mean?
813
814Suppose we bind the following function
815
816.. code-block:: cpp
817
818 void append_1(std::vector<int> &v) {
819 v.push_back(1);
820 }
821
822and call it from Python, the following happens:
823
824.. code-block:: python
825
826 >>> v = [5, 6]
827 >>> append_1(v)
828 >>> print(v)
829 [5, 6]
830
831As you can see, when passing STL data structures by reference, modifications
832are not propagated back the Python side. A similar situation arises when
833exposing STL data structures using the ``def_readwrite`` or ``def_readonly``
834functions:
835
836.. code-block:: cpp
837
838 /* ... definition ... */
839
840 class MyClass {
841 std::vector<int> contents;
842 };
843
844 /* ... binding code ... */
845
846 py::class_<MyClass>(m, "MyClass")
847 .def(py::init<>)
848 .def_readwrite("contents", &MyClass::contents);
849
850In this case, properties can be read and written in their entirety. However, an
851``append`` operaton involving such a list type has no effect:
852
853.. code-block:: python
854
855 >>> m = MyClass()
856 >>> m.contents = [5, 6]
857 >>> print(m.contents)
858 [5, 6]
859 >>> m.contents.append(7)
860 >>> print(m.contents)
861 [5, 6]
862
863To deal with both of the above situations, pybind11 provides a macro named
864``PYBIND11_MAKE_OPAQUE(T)`` that disables the template-based conversion
865machinery of types, thus rendering them *opaque*. The contents of opaque
866objects are never inspected or extracted, hence they can be passed by
867reference. For instance, to turn ``std::vector<int>`` into an opaque type, add
868the declaration
869
870.. code-block:: cpp
871
872 PYBIND11_MAKE_OPAQUE(std::vector<int>);
873
874before any binding code (e.g. invocations to ``class_::def()``, etc.). This
875macro must be specified at the top level, since instantiates a partial template
876overload. If your binding code consists of multiple compilation units, it must
877be present in every file preceding any usage of ``std::vector<int>``. Opaque
878types must also have a corresponding ``class_`` declaration to associate them
879with a name in Python, and to define a set of available operations:
880
881.. code-block:: cpp
882
883 py::class_<std::vector<int>>(m, "IntVector")
884 .def(py::init<>())
885 .def("clear", &std::vector<int>::clear)
886 .def("pop_back", &std::vector<int>::pop_back)
887 .def("__len__", [](const std::vector<int> &v) { return v.size(); })
888 .def("__iter__", [](std::vector<int> &v) {
889 return py::make_iterator(v.begin(), v.end());
890 }, py::keep_alive<0, 1>()) /* Keep vector alive while iterator is used */
891 // ....
892
893
894.. seealso::
895
896 The file :file:`example/example14.cpp` contains a complete example that
897 demonstrates how to create and expose opaque types using pybind11 in more
898 detail.
899
900.. _eigen:
901
902Transparent conversion of dense and sparse Eigen data types
903===========================================================
904
905Eigen [#f1]_ is C++ header-based library for dense and sparse linear algebra. Due to
906its popularity and widespread adoption, pybind11 provides transparent
907conversion support between Eigen and Scientific Python linear algebra data types.
908
909Specifically, when including the optional header file :file:`pybind11/eigen.h`,
Wenzel Jakob178c8a82016-05-10 15:59:01 +0100910pybind11 will automatically and transparently convert
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200911
9121. Static and dynamic Eigen dense vectors and matrices to instances of
913 ``numpy.ndarray`` (and vice versa).
914
9151. Eigen sparse vectors and matrices to instances of
916 ``scipy.sparse.csr_matrix``/``scipy.sparse.csc_matrix`` (and vice versa).
917
918This makes it possible to bind most kinds of functions that rely on these types.
919One major caveat are functions that take Eigen matrices *by reference* and modify
920them somehow, in which case the information won't be propagated to the caller.
921
922.. code-block:: cpp
923
924 /* The Python bindings of this function won't replicate
925 the intended effect of modifying the function argument */
926 void scale_by_2(Eigen::Vector3f &v) {
927 v *= 2;
928 }
929
930To see why this is, refer to the section on :ref:`opaque` (although that
931section specifically covers STL data types, the underlying issue is the same).
932The next two sections discuss an efficient alternative for exposing the
933underlying native Eigen types as opaque objects in a way that still integrates
934with NumPy and SciPy.
935
936.. [#f1] http://eigen.tuxfamily.org
937
938.. seealso::
939
940 The file :file:`example/eigen.cpp` contains a complete example that
941 shows how to pass Eigen sparse and dense data types in more detail.
942
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200943Buffer protocol
944===============
945
946Python supports an extremely general and convenient approach for exchanging
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200947data between plugin libraries. Types can expose a buffer view [#f2]_, which
948provides fast direct access to the raw internal data representation. Suppose we
949want to bind the following simplistic Matrix class:
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200950
951.. code-block:: cpp
952
953 class Matrix {
954 public:
955 Matrix(size_t rows, size_t cols) : m_rows(rows), m_cols(cols) {
956 m_data = new float[rows*cols];
957 }
958 float *data() { return m_data; }
959 size_t rows() const { return m_rows; }
960 size_t cols() const { return m_cols; }
961 private:
962 size_t m_rows, m_cols;
963 float *m_data;
964 };
965
966The following binding code exposes the ``Matrix`` contents as a buffer object,
Wenzel Jakob8e93df82016-05-01 02:36:58 +0200967making it possible to cast Matrices into NumPy arrays. It is even possible to
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200968completely avoid copy operations with Python expressions like
969``np.array(matrix_instance, copy = False)``.
970
971.. code-block:: cpp
972
973 py::class_<Matrix>(m, "Matrix")
974 .def_buffer([](Matrix &m) -> py::buffer_info {
975 return py::buffer_info(
Wenzel Jakob876eeab2016-05-04 22:22:48 +0200976 m.data(), /* Pointer to buffer */
977 sizeof(float), /* Size of one scalar */
978 py::format_descriptor<float>::value, /* Python struct-style format descriptor */
979 2, /* Number of dimensions */
980 { m.rows(), m.cols() }, /* Buffer dimensions */
981 { sizeof(float) * m.rows(), /* Strides (in bytes) for each index */
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200982 sizeof(float) }
983 );
984 });
985
986The snippet above binds a lambda function, which can create ``py::buffer_info``
987description records on demand describing a given matrix. The contents of
988``py::buffer_info`` mirror the Python buffer protocol specification.
989
990.. code-block:: cpp
991
992 struct buffer_info {
993 void *ptr;
994 size_t itemsize;
995 std::string format;
996 int ndim;
997 std::vector<size_t> shape;
998 std::vector<size_t> strides;
999 };
1000
1001To create a C++ function that can take a Python buffer object as an argument,
1002simply use the type ``py::buffer`` as one of its arguments. Buffers can exist
1003in a great variety of configurations, hence some safety checks are usually
1004necessary in the function body. Below, you can see an basic example on how to
1005define a custom constructor for the Eigen double precision matrix
1006(``Eigen::MatrixXd``) type, which supports initialization from compatible
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001007buffer objects (e.g. a NumPy matrix).
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001008
1009.. code-block:: cpp
1010
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001011 /* Bind MatrixXd (or some other Eigen type) to Python */
1012 typedef Eigen::MatrixXd Matrix;
1013
1014 typedef Matrix::Scalar Scalar;
1015 constexpr bool rowMajor = Matrix::Flags & Eigen::RowMajorBit;
1016
1017 py::class_<Matrix>(m, "Matrix")
1018 .def("__init__", [](Matrix &m, py::buffer b) {
Wenzel Jakobe7628532016-05-05 10:04:44 +02001019 typedef Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic> Strides;
Wenzel Jakobe7628532016-05-05 10:04:44 +02001020
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001021 /* Request a buffer descriptor from Python */
1022 py::buffer_info info = b.request();
1023
1024 /* Some sanity checks ... */
Wenzel Jakobe7628532016-05-05 10:04:44 +02001025 if (info.format != py::format_descriptor<Scalar>::value)
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001026 throw std::runtime_error("Incompatible format: expected a double array!");
1027
1028 if (info.ndim != 2)
1029 throw std::runtime_error("Incompatible buffer dimension!");
1030
Wenzel Jakobe7628532016-05-05 10:04:44 +02001031 auto strides = Strides(
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001032 info.strides[rowMajor ? 0 : 1] / sizeof(Scalar),
1033 info.strides[rowMajor ? 1 : 0] / sizeof(Scalar));
Wenzel Jakobe7628532016-05-05 10:04:44 +02001034
1035 auto map = Eigen::Map<Matrix, 0, Strides>(
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001036 static_cat<Scalar *>(info.ptr), info.shape[0], info.shape[1], strides);
Wenzel Jakobe7628532016-05-05 10:04:44 +02001037
1038 new (&m) Matrix(map);
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001039 });
1040
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001041For reference, the ``def_buffer()`` call for this Eigen data type should look
1042as follows:
1043
1044.. code-block:: cpp
1045
1046 .def_buffer([](Matrix &m) -> py::buffer_info {
1047 return py::buffer_info(
1048 m.data(), /* Pointer to buffer */
1049 sizeof(Scalar), /* Size of one scalar */
1050 /* Python struct-style format descriptor */
1051 py::format_descriptor<Scalar>::value,
1052 /* Number of dimensions */
1053 2,
1054 /* Buffer dimensions */
1055 { (size_t) m.rows(),
1056 (size_t) m.cols() },
1057 /* Strides (in bytes) for each index */
1058 { sizeof(Scalar) * (rowMajor ? m.cols() : 1),
1059 sizeof(Scalar) * (rowMajor ? 1 : m.rows()) }
1060 );
1061 })
1062
1063For a much easier approach of binding Eigen types (although with some
1064limitations), refer to the section on :ref:`eigen`.
1065
Wenzel Jakob93296692015-10-13 23:21:54 +02001066.. seealso::
1067
1068 The file :file:`example/example7.cpp` contains a complete example that
1069 demonstrates using the buffer protocol with pybind11 in more detail.
1070
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001071.. [#f2] http://docs.python.org/3/c-api/buffer.html
Wenzel Jakob978e3762016-04-07 18:00:41 +02001072
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001073NumPy support
1074=============
1075
1076By exchanging ``py::buffer`` with ``py::array`` in the above snippet, we can
1077restrict the function so that it only accepts NumPy arrays (rather than any
Wenzel Jakob978e3762016-04-07 18:00:41 +02001078type of Python object satisfying the buffer protocol).
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001079
1080In many situations, we want to define a function which only accepts a NumPy
Wenzel Jakob93296692015-10-13 23:21:54 +02001081array of a certain data type. This is possible via the ``py::array_t<T>``
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001082template. For instance, the following function requires the argument to be a
Wenzel Jakobf1032df2016-05-05 10:00:00 +02001083NumPy array containing double precision values.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001084
1085.. code-block:: cpp
1086
Wenzel Jakob93296692015-10-13 23:21:54 +02001087 void f(py::array_t<double> array);
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001088
Wenzel Jakobf1032df2016-05-05 10:00:00 +02001089When it is invoked with a different type (e.g. an integer or a list of
1090integers), the binding code will attempt to cast the input into a NumPy array
1091of the requested type. Note that this feature requires the
1092:file:``pybind11/numpy.h`` header to be included.
1093
1094Data in NumPy arrays is not guaranteed to packed in a dense manner;
1095furthermore, entries can be separated by arbitrary column and row strides.
1096Sometimes, it can be useful to require a function to only accept dense arrays
1097using either the C (row-major) or Fortran (column-major) ordering. This can be
1098accomplished via a second template argument with values ``py::array::c_style``
1099or ``py::array::f_style``.
1100
1101.. code-block:: cpp
1102
1103 void f(py::array_t<double, py::array::c_style> array);
1104
1105As before, the implementation will attempt to convert non-conforming arguments
1106into an array satisfying the specified requirements.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001107
1108Vectorizing functions
1109=====================
1110
1111Suppose we want to bind a function with the following signature to Python so
1112that it can process arbitrary NumPy array arguments (vectors, matrices, general
1113N-D arrays) in addition to its normal arguments:
1114
1115.. code-block:: cpp
1116
1117 double my_func(int x, float y, double z);
1118
Wenzel Jakob8f4eb002015-10-15 18:13:33 +02001119After including the ``pybind11/numpy.h`` header, this is extremely simple:
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001120
1121.. code-block:: cpp
1122
1123 m.def("vectorized_func", py::vectorize(my_func));
1124
1125Invoking the function like below causes 4 calls to be made to ``my_func`` with
Wenzel Jakob8e93df82016-05-01 02:36:58 +02001126each of the array elements. The significant advantage of this compared to
Wenzel Jakob978e3762016-04-07 18:00:41 +02001127solutions like ``numpy.vectorize()`` is that the loop over the elements runs
1128entirely on the C++ side and can be crunched down into a tight, optimized loop
1129by the compiler. The result is returned as a NumPy array of type
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001130``numpy.dtype.float64``.
1131
1132.. code-block:: python
1133
1134 >>> x = np.array([[1, 3],[5, 7]])
1135 >>> y = np.array([[2, 4],[6, 8]])
1136 >>> z = 3
1137 >>> result = vectorized_func(x, y, z)
1138
1139The scalar argument ``z`` is transparently replicated 4 times. The input
1140arrays ``x`` and ``y`` are automatically converted into the right types (they
1141are of type ``numpy.dtype.int64`` but need to be ``numpy.dtype.int32`` and
1142``numpy.dtype.float32``, respectively)
1143
Wenzel Jakob8e93df82016-05-01 02:36:58 +02001144Sometimes we might want to explicitly exclude an argument from the vectorization
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001145because it makes little sense to wrap it in a NumPy array. For instance,
1146suppose the function signature was
1147
1148.. code-block:: cpp
1149
1150 double my_func(int x, float y, my_custom_type *z);
1151
1152This can be done with a stateful Lambda closure:
1153
1154.. code-block:: cpp
1155
1156 // Vectorize a lambda function with a capture object (e.g. to exclude some arguments from the vectorization)
1157 m.def("vectorized_func",
Wenzel Jakob93296692015-10-13 23:21:54 +02001158 [](py::array_t<int> x, py::array_t<float> y, my_custom_type *z) {
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001159 auto stateful_closure = [z](int x, float y) { return my_func(x, y, z); };
1160 return py::vectorize(stateful_closure)(x, y);
1161 }
1162 );
1163
Wenzel Jakob61587162016-01-18 22:38:52 +01001164In cases where the computation is too complicated to be reduced to
1165``vectorize``, it will be necessary to create and access the buffer contents
1166manually. The following snippet contains a complete example that shows how this
1167works (the code is somewhat contrived, since it could have been done more
1168simply using ``vectorize``).
1169
1170.. code-block:: cpp
1171
1172 #include <pybind11/pybind11.h>
1173 #include <pybind11/numpy.h>
1174
1175 namespace py = pybind11;
1176
1177 py::array_t<double> add_arrays(py::array_t<double> input1, py::array_t<double> input2) {
1178 auto buf1 = input1.request(), buf2 = input2.request();
1179
1180 if (buf1.ndim != 1 || buf2.ndim != 1)
1181 throw std::runtime_error("Number of dimensions must be one");
1182
1183 if (buf1.shape[0] != buf2.shape[0])
1184 throw std::runtime_error("Input shapes must match");
1185
1186 auto result = py::array(py::buffer_info(
1187 nullptr, /* Pointer to data (nullptr -> ask NumPy to allocate!) */
1188 sizeof(double), /* Size of one item */
Nils Wernerf7048f22016-05-19 11:17:17 +02001189 py::format_descriptor<double>::value(), /* Buffer format */
Wenzel Jakob61587162016-01-18 22:38:52 +01001190 buf1.ndim, /* How many dimensions? */
1191 { buf1.shape[0] }, /* Number of elements for each dimension */
1192 { sizeof(double) } /* Strides for each dimension */
1193 ));
1194
1195 auto buf3 = result.request();
1196
1197 double *ptr1 = (double *) buf1.ptr,
1198 *ptr2 = (double *) buf2.ptr,
1199 *ptr3 = (double *) buf3.ptr;
1200
1201 for (size_t idx = 0; idx < buf1.shape[0]; idx++)
1202 ptr3[idx] = ptr1[idx] + ptr2[idx];
1203
1204 return result;
1205 }
1206
1207 PYBIND11_PLUGIN(test) {
1208 py::module m("test");
1209 m.def("add_arrays", &add_arrays, "Add two NumPy arrays");
1210 return m.ptr();
1211 }
1212
Wenzel Jakob93296692015-10-13 23:21:54 +02001213.. seealso::
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001214
Wenzel Jakob93296692015-10-13 23:21:54 +02001215 The file :file:`example/example10.cpp` contains a complete example that
1216 demonstrates using :func:`vectorize` in more detail.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001217
Wenzel Jakob93296692015-10-13 23:21:54 +02001218Functions taking Python objects as arguments
1219============================================
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001220
Wenzel Jakob93296692015-10-13 23:21:54 +02001221pybind11 exposes all major Python types using thin C++ wrapper classes. These
1222wrapper classes can also be used as parameters of functions in bindings, which
1223makes it possible to directly work with native Python types on the C++ side.
1224For instance, the following statement iterates over a Python ``dict``:
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001225
Wenzel Jakob93296692015-10-13 23:21:54 +02001226.. code-block:: cpp
1227
1228 void print_dict(py::dict dict) {
1229 /* Easily interact with Python types */
1230 for (auto item : dict)
1231 std::cout << "key=" << item.first << ", "
1232 << "value=" << item.second << std::endl;
1233 }
1234
1235Available types include :class:`handle`, :class:`object`, :class:`bool_`,
Wenzel Jakob27e8e102016-01-17 22:36:37 +01001236:class:`int_`, :class:`float_`, :class:`str`, :class:`bytes`, :class:`tuple`,
Wenzel Jakobf64feaf2016-04-28 14:33:45 +02001237:class:`list`, :class:`dict`, :class:`slice`, :class:`none`, :class:`capsule`,
1238:class:`iterable`, :class:`iterator`, :class:`function`, :class:`buffer`,
1239:class:`array`, and :class:`array_t`.
Wenzel Jakob93296692015-10-13 23:21:54 +02001240
Wenzel Jakob436b7312015-10-20 01:04:30 +02001241In this kind of mixed code, it is often necessary to convert arbitrary C++
1242types to Python, which can be done using :func:`cast`:
1243
1244.. code-block:: cpp
1245
1246 MyClass *cls = ..;
1247 py::object obj = py::cast(cls);
1248
1249The reverse direction uses the following syntax:
1250
1251.. code-block:: cpp
1252
1253 py::object obj = ...;
1254 MyClass *cls = obj.cast<MyClass *>();
1255
1256When conversion fails, both directions throw the exception :class:`cast_error`.
Wenzel Jakob178c8a82016-05-10 15:59:01 +01001257It is also possible to call python functions via ``operator()``.
1258
1259.. code-block:: cpp
1260
1261 py::function f = <...>;
1262 py::object result_py = f(1234, "hello", some_instance);
1263 MyClass &result = result_py.cast<MyClass>();
1264
1265The special ``f(*args)`` and ``f(*args, **kwargs)`` syntax is also supported to
1266supply arbitrary argument and keyword lists, although these cannot be mixed
1267with other parameters.
1268
1269.. code-block:: cpp
1270
1271 py::function f = <...>;
1272 py::tuple args = py::make_tuple(1234);
1273 py::dict kwargs;
1274 kwargs["y"] = py::cast(5678);
1275 py::object result = f(*args, **kwargs);
Wenzel Jakob436b7312015-10-20 01:04:30 +02001276
Wenzel Jakob93296692015-10-13 23:21:54 +02001277.. seealso::
1278
1279 The file :file:`example/example2.cpp` contains a complete example that
Wenzel Jakob178c8a82016-05-10 15:59:01 +01001280 demonstrates passing native Python types in more detail. The file
1281 :file:`example/example11.cpp` discusses usage of ``args`` and ``kwargs``.
Wenzel Jakob2ac50442016-01-17 22:36:35 +01001282
1283Default arguments revisited
1284===========================
1285
1286The section on :ref:`default_args` previously discussed basic usage of default
1287arguments using pybind11. One noteworthy aspect of their implementation is that
1288default arguments are converted to Python objects right at declaration time.
1289Consider the following example:
1290
1291.. code-block:: cpp
1292
1293 py::class_<MyClass>("MyClass")
1294 .def("myFunction", py::arg("arg") = SomeType(123));
1295
1296In this case, pybind11 must already be set up to deal with values of the type
1297``SomeType`` (via a prior instantiation of ``py::class_<SomeType>``), or an
1298exception will be thrown.
1299
1300Another aspect worth highlighting is that the "preview" of the default argument
1301in the function signature is generated using the object's ``__repr__`` method.
1302If not available, the signature may not be very helpful, e.g.:
1303
1304.. code-block:: python
1305
1306 FUNCTIONS
1307 ...
1308 | myFunction(...)
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001309 | Signature : (MyClass, arg : SomeType = <SomeType object at 0x101b7b080>) -> NoneType
Wenzel Jakob2ac50442016-01-17 22:36:35 +01001310 ...
1311
1312The first way of addressing this is by defining ``SomeType.__repr__``.
1313Alternatively, it is possible to specify the human-readable preview of the
1314default argument manually using the ``arg_t`` notation:
1315
1316.. code-block:: cpp
1317
1318 py::class_<MyClass>("MyClass")
1319 .def("myFunction", py::arg_t<SomeType>("arg", SomeType(123), "SomeType(123)"));
1320
Wenzel Jakobc769fce2016-03-03 12:03:30 +01001321Sometimes it may be necessary to pass a null pointer value as a default
1322argument. In this case, remember to cast it to the underlying type in question,
1323like so:
1324
1325.. code-block:: cpp
1326
1327 py::class_<MyClass>("MyClass")
1328 .def("myFunction", py::arg("arg") = (SomeType *) nullptr);
1329
Wenzel Jakob178c8a82016-05-10 15:59:01 +01001330Binding functions that accept arbitrary numbers of arguments and keywords arguments
1331===================================================================================
1332
1333Python provides a useful mechanism to define functions that accept arbitrary
1334numbers of arguments and keyword arguments:
1335
1336.. code-block:: cpp
1337
1338 def generic(*args, **kwargs):
1339 # .. do something with args and kwargs
1340
1341Such functions can also be created using pybind11:
1342
1343.. code-block:: cpp
1344
1345 void generic(py::args args, py::kwargs kwargs) {
1346 /// .. do something with args
1347 if (kwargs)
1348 /// .. do something with kwargs
1349 }
1350
1351 /// Binding code
1352 m.def("generic", &generic);
1353
1354(See ``example/example11.cpp``). The class ``py::args`` derives from
1355``py::list`` and ``py::kwargs`` derives from ``py::dict`` Note that the
1356``kwargs`` argument is invalid if no keyword arguments were actually provided.
1357Please refer to the other examples for details on how to iterate over these,
1358and on how to cast their entries into C++ objects.
1359
Wenzel Jakob2dfbade2016-01-17 22:36:37 +01001360Partitioning code over multiple extension modules
1361=================================================
1362
Wenzel Jakob90d2f5e2016-04-11 14:30:11 +02001363It's straightforward to split binding code over multiple extension modules,
1364while referencing types that are declared elsewhere. Everything "just" works
1365without any special precautions. One exception to this rule occurs when
1366extending a type declared in another extension module. Recall the basic example
1367from Section :ref:`inheritance`.
Wenzel Jakob2dfbade2016-01-17 22:36:37 +01001368
1369.. code-block:: cpp
1370
1371 py::class_<Pet> pet(m, "Pet");
1372 pet.def(py::init<const std::string &>())
1373 .def_readwrite("name", &Pet::name);
1374
1375 py::class_<Dog>(m, "Dog", pet /* <- specify parent */)
1376 .def(py::init<const std::string &>())
1377 .def("bark", &Dog::bark);
1378
1379Suppose now that ``Pet`` bindings are defined in a module named ``basic``,
1380whereas the ``Dog`` bindings are defined somewhere else. The challenge is of
1381course that the variable ``pet`` is not available anymore though it is needed
1382to indicate the inheritance relationship to the constructor of ``class_<Dog>``.
1383However, it can be acquired as follows:
1384
1385.. code-block:: cpp
1386
1387 py::object pet = (py::object) py::module::import("basic").attr("Pet");
1388
1389 py::class_<Dog>(m, "Dog", pet)
1390 .def(py::init<const std::string &>())
1391 .def("bark", &Dog::bark);
1392
Wenzel Jakob8d862b32016-03-06 13:37:22 +01001393Alternatively, we can rely on the ``base`` tag, which performs an automated
1394lookup of the corresponding Python type. However, this also requires invoking
1395the ``import`` function once to ensure that the pybind11 binding code of the
1396module ``basic`` has been executed.
1397
Wenzel Jakob8d862b32016-03-06 13:37:22 +01001398.. code-block:: cpp
1399
1400 py::module::import("basic");
1401
1402 py::class_<Dog>(m, "Dog", py::base<Pet>())
1403 .def(py::init<const std::string &>())
1404 .def("bark", &Dog::bark);
Wenzel Jakobeda978e2016-03-15 15:05:40 +01001405
Wenzel Jakob978e3762016-04-07 18:00:41 +02001406Naturally, both methods will fail when there are cyclic dependencies.
1407
Wenzel Jakob90d2f5e2016-04-11 14:30:11 +02001408Note that compiling code which has its default symbol visibility set to
1409*hidden* (e.g. via the command line flag ``-fvisibility=hidden`` on GCC/Clang) can interfere with the
1410ability to access types defined in another extension module. Workarounds
1411include changing the global symbol visibility (not recommended, because it will
1412lead unnecessarily large binaries) or manually exporting types that are
1413accessed by multiple extension modules:
1414
1415.. code-block:: cpp
1416
1417 #ifdef _WIN32
1418 # define EXPORT_TYPE __declspec(dllexport)
1419 #else
1420 # define EXPORT_TYPE __attribute__ ((visibility("default")))
1421 #endif
1422
1423 class EXPORT_TYPE Dog : public Animal {
1424 ...
1425 };
1426
1427
Wenzel Jakob1c329aa2016-04-13 02:37:36 +02001428Pickling support
1429================
1430
1431Python's ``pickle`` module provides a powerful facility to serialize and
1432de-serialize a Python object graph into a binary data stream. To pickle and
Wenzel Jakob3d0e6ff2016-04-13 11:48:10 +02001433unpickle C++ classes using pybind11, two additional functions must be provided.
Wenzel Jakob1c329aa2016-04-13 02:37:36 +02001434Suppose the class in question has the following signature:
1435
1436.. code-block:: cpp
1437
1438 class Pickleable {
1439 public:
1440 Pickleable(const std::string &value) : m_value(value) { }
1441 const std::string &value() const { return m_value; }
1442
1443 void setExtra(int extra) { m_extra = extra; }
1444 int extra() const { return m_extra; }
1445 private:
1446 std::string m_value;
1447 int m_extra = 0;
1448 };
1449
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001450The binding code including the requisite ``__setstate__`` and ``__getstate__`` methods [#f3]_
Wenzel Jakob1c329aa2016-04-13 02:37:36 +02001451looks as follows:
1452
1453.. code-block:: cpp
1454
1455 py::class_<Pickleable>(m, "Pickleable")
1456 .def(py::init<std::string>())
1457 .def("value", &Pickleable::value)
1458 .def("extra", &Pickleable::extra)
1459 .def("setExtra", &Pickleable::setExtra)
1460 .def("__getstate__", [](const Pickleable &p) {
1461 /* Return a tuple that fully encodes the state of the object */
1462 return py::make_tuple(p.value(), p.extra());
1463 })
1464 .def("__setstate__", [](Pickleable &p, py::tuple t) {
1465 if (t.size() != 2)
1466 throw std::runtime_error("Invalid state!");
1467
Wenzel Jakobd40885a2016-04-13 13:30:05 +02001468 /* Invoke the in-place constructor. Note that this is needed even
1469 when the object just has a trivial default constructor */
Wenzel Jakob1c329aa2016-04-13 02:37:36 +02001470 new (&p) Pickleable(t[0].cast<std::string>());
1471
1472 /* Assign any additional state */
1473 p.setExtra(t[1].cast<int>());
1474 });
1475
1476An instance can now be pickled as follows:
1477
1478.. code-block:: python
1479
1480 try:
1481 import cPickle as pickle # Use cPickle on Python 2.7
1482 except ImportError:
1483 import pickle
1484
1485 p = Pickleable("test_value")
1486 p.setExtra(15)
Wenzel Jakob81e09752016-04-30 23:13:03 +02001487 data = pickle.dumps(p, 2)
Wenzel Jakob1c329aa2016-04-13 02:37:36 +02001488
Wenzel Jakob81e09752016-04-30 23:13:03 +02001489Note that only the cPickle module is supported on Python 2.7. The second
1490argument to ``dumps`` is also crucial: it selects the pickle protocol version
14912, since the older version 1 is not supported. Newer versions are also fine—for
1492instance, specify ``-1`` to always use the latest available version. Beware:
1493failure to follow these instructions will cause important pybind11 memory
1494allocation routines to be skipped during unpickling, which will likely lead to
1495memory corruption and/or segmentation faults.
Wenzel Jakob1c329aa2016-04-13 02:37:36 +02001496
1497.. seealso::
1498
1499 The file :file:`example/example15.cpp` contains a complete example that
1500 demonstrates how to pickle and unpickle types using pybind11 in more detail.
1501
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001502.. [#f3] http://docs.python.org/3/library/pickle.html#pickling-class-instances
Wenzel Jakobef7a9b92016-04-13 18:41:59 +02001503
1504Generating documentation using Sphinx
1505=====================================
1506
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001507Sphinx [#f4]_ has the ability to inspect the signatures and documentation
Wenzel Jakobef7a9b92016-04-13 18:41:59 +02001508strings in pybind11-based extension modules to automatically generate beautiful
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001509documentation in a variety formats. The pbtest repository [#f5]_ contains a
Wenzel Jakobef7a9b92016-04-13 18:41:59 +02001510simple example repository which uses this approach.
1511
1512There are two potential gotchas when using this approach: first, make sure that
1513the resulting strings do not contain any :kbd:`TAB` characters, which break the
1514docstring parsing routines. You may want to use C++11 raw string literals,
1515which are convenient for multi-line comments. Conveniently, any excess
1516indentation will be automatically be removed by Sphinx. However, for this to
1517work, it is important that all lines are indented consistently, i.e.:
1518
1519.. code-block:: cpp
1520
1521 // ok
1522 m.def("foo", &foo, R"mydelimiter(
1523 The foo function
1524
1525 Parameters
1526 ----------
1527 )mydelimiter");
1528
1529 // *not ok*
1530 m.def("foo", &foo, R"mydelimiter(The foo function
1531
1532 Parameters
1533 ----------
1534 )mydelimiter");
1535
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001536.. [#f4] http://www.sphinx-doc.org
1537.. [#f5] http://github.com/pybind/pbtest