Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 1 | .. _advanced: |
| 2 | |
| 3 | Advanced topics |
| 4 | ############### |
| 5 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 6 | For brevity, the rest of this chapter assumes that the following two lines are |
| 7 | present: |
| 8 | |
| 9 | .. code-block:: cpp |
| 10 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 11 | #include <pybind11/pybind11.h> |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 12 | |
Wenzel Jakob | 10e62e1 | 2015-10-15 22:46:07 +0200 | [diff] [blame] | 13 | namespace py = pybind11; |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 14 | |
Wenzel Jakob | de3ad07 | 2016-02-02 11:38:21 +0100 | [diff] [blame] | 15 | Exporting constants and mutable objects |
| 16 | ======================================= |
| 17 | |
| 18 | To expose a C++ constant, use the ``attr`` function to register it in a module |
| 19 | as shown below. The ``int_`` class is one of many small wrapper objects defined |
| 20 | in ``pybind11/pytypes.h``. General objects (including integers) can also be |
| 21 | converted 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 Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 31 | Operator overloading |
| 32 | ==================== |
| 33 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 34 | Suppose that we're given the following ``Vector2`` class with a vector addition |
| 35 | and scalar multiplication operation, all implemented using overloaded operators |
| 36 | in C++. |
| 37 | |
| 38 | .. code-block:: cpp |
| 39 | |
| 40 | class Vector2 { |
| 41 | public: |
| 42 | Vector2(float x, float y) : x(x), y(y) { } |
| 43 | |
| 44 | std::string toString() const { return "[" + std::to_string(x) + ", " + std::to_string(y) + "]"; } |
| 45 | |
| 46 | Vector2 operator+(const Vector2 &v) const { return Vector2(x + v.x, y + v.y); } |
| 47 | Vector2 operator*(float value) const { return Vector2(x * value, y * value); } |
| 48 | Vector2& operator+=(const Vector2 &v) { x += v.x; y += v.y; return *this; } |
| 49 | Vector2& operator*=(float v) { x *= v; y *= v; return *this; } |
| 50 | |
| 51 | friend Vector2 operator*(float f, const Vector2 &v) { return Vector2(f * v.x, f * v.y); } |
| 52 | |
| 53 | private: |
| 54 | float x, y; |
| 55 | }; |
| 56 | |
| 57 | The following snippet shows how the above operators can be conveniently exposed |
| 58 | to Python. |
| 59 | |
| 60 | .. code-block:: cpp |
| 61 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 62 | #include <pybind11/operators.h> |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 63 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 64 | PYBIND11_PLUGIN(example) { |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 65 | py::module m("example", "pybind11 example plugin"); |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 66 | |
| 67 | py::class_<Vector2>(m, "Vector2") |
| 68 | .def(py::init<float, float>()) |
| 69 | .def(py::self + py::self) |
| 70 | .def(py::self += py::self) |
| 71 | .def(py::self *= float()) |
| 72 | .def(float() * py::self) |
| 73 | .def("__repr__", &Vector2::toString); |
| 74 | |
| 75 | return m.ptr(); |
| 76 | } |
| 77 | |
| 78 | Note that a line like |
| 79 | |
| 80 | .. code-block:: cpp |
| 81 | |
| 82 | .def(py::self * float()) |
| 83 | |
| 84 | is really just short hand notation for |
| 85 | |
| 86 | .. code-block:: cpp |
| 87 | |
| 88 | .def("__mul__", [](const Vector2 &a, float b) { |
| 89 | return a * b; |
| 90 | }) |
| 91 | |
| 92 | This can be useful for exposing additional operators that don't exist on the |
| 93 | C++ side, or to perform other types of customization. |
| 94 | |
| 95 | .. note:: |
| 96 | |
| 97 | To use the more convenient ``py::self`` notation, the additional |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 98 | header file :file:`pybind11/operators.h` must be included. |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 99 | |
| 100 | .. seealso:: |
| 101 | |
| 102 | The file :file:`example/example3.cpp` contains a complete example that |
| 103 | demonstrates how to work with overloaded operators in more detail. |
| 104 | |
| 105 | Callbacks and passing anonymous functions |
| 106 | ========================================= |
| 107 | |
| 108 | The C++11 standard brought lambda functions and the generic polymorphic |
| 109 | function wrapper ``std::function<>`` to the C++ programming language, which |
| 110 | enable powerful new ways of working with functions. Lambda functions come in |
| 111 | two flavors: stateless lambda function resemble classic function pointers that |
| 112 | link to an anonymous piece of code, while stateful lambda functions |
| 113 | additionally depend on captured variables that are stored in an anonymous |
| 114 | *lambda closure object*. |
| 115 | |
| 116 | Here is a simple example of a C++ function that takes an arbitrary function |
| 117 | (stateful or stateless) with signature ``int -> int`` as an argument and runs |
| 118 | it with the value 10. |
| 119 | |
| 120 | .. code-block:: cpp |
| 121 | |
| 122 | int func_arg(const std::function<int(int)> &f) { |
| 123 | return f(10); |
| 124 | } |
| 125 | |
| 126 | The example below is more involved: it takes a function of signature ``int -> int`` |
| 127 | and returns another function of the same kind. The return value is a stateful |
| 128 | lambda function, which stores the value ``f`` in the capture object and adds 1 to |
| 129 | its return value upon execution. |
| 130 | |
| 131 | .. code-block:: cpp |
| 132 | |
| 133 | std::function<int(int)> func_ret(const std::function<int(int)> &f) { |
| 134 | return [f](int i) { |
| 135 | return f(i) + 1; |
| 136 | }; |
| 137 | } |
| 138 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 139 | After including the extra header file :file:`pybind11/functional.h`, it is almost |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 140 | trivial to generate binding code for both of these functions. |
| 141 | |
| 142 | .. code-block:: cpp |
| 143 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 144 | #include <pybind11/functional.h> |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 145 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 146 | PYBIND11_PLUGIN(example) { |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 147 | py::module m("example", "pybind11 example plugin"); |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 148 | |
| 149 | m.def("func_arg", &func_arg); |
| 150 | m.def("func_ret", &func_ret); |
| 151 | |
| 152 | return m.ptr(); |
| 153 | } |
| 154 | |
| 155 | The following interactive session shows how to call them from Python. |
| 156 | |
| 157 | .. code-block:: python |
| 158 | |
| 159 | $ python |
| 160 | >>> import example |
| 161 | >>> def square(i): |
| 162 | ... return i * i |
| 163 | ... |
| 164 | >>> example.func_arg(square) |
| 165 | 100L |
| 166 | >>> square_plus_1 = example.func_ret(square) |
| 167 | >>> square_plus_1(4) |
| 168 | 17L |
| 169 | >>> |
| 170 | |
| 171 | .. note:: |
| 172 | |
| 173 | This functionality is very useful when generating bindings for callbacks in |
| 174 | C++ libraries (e.g. a graphical user interface library). |
| 175 | |
| 176 | The file :file:`example/example5.cpp` contains a complete example that |
| 177 | demonstrates how to work with callbacks and anonymous functions in more detail. |
| 178 | |
Wenzel Jakob | a4175d6 | 2015-11-17 08:30:34 +0100 | [diff] [blame] | 179 | .. warning:: |
| 180 | |
| 181 | Keep in mind that passing a function from C++ to Python (or vice versa) |
| 182 | will instantiate a piece of wrapper code that translates function |
| 183 | invocations between the two languages. Copying the same function back and |
| 184 | forth between Python and C++ many times in a row will cause these wrappers |
| 185 | to accumulate, which can decrease performance. |
| 186 | |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 187 | Overriding virtual functions in Python |
| 188 | ====================================== |
| 189 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 190 | Suppose that a C++ class or interface has a virtual function that we'd like to |
| 191 | to override from within Python (we'll focus on the class ``Animal``; ``Dog`` is |
| 192 | given as a specific example of how one would do this with traditional C++ |
| 193 | code). |
| 194 | |
| 195 | .. code-block:: cpp |
| 196 | |
| 197 | class Animal { |
| 198 | public: |
| 199 | virtual ~Animal() { } |
| 200 | virtual std::string go(int n_times) = 0; |
| 201 | }; |
| 202 | |
| 203 | class Dog : public Animal { |
| 204 | public: |
| 205 | std::string go(int n_times) { |
| 206 | std::string result; |
| 207 | for (int i=0; i<n_times; ++i) |
| 208 | result += "woof! "; |
| 209 | return result; |
| 210 | } |
| 211 | }; |
| 212 | |
| 213 | Let's also suppose that we are given a plain function which calls the |
| 214 | function ``go()`` on an arbitrary ``Animal`` instance. |
| 215 | |
| 216 | .. code-block:: cpp |
| 217 | |
| 218 | std::string call_go(Animal *animal) { |
| 219 | return animal->go(3); |
| 220 | } |
| 221 | |
| 222 | Normally, the binding code for these classes would look as follows: |
| 223 | |
| 224 | .. code-block:: cpp |
| 225 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 226 | PYBIND11_PLUGIN(example) { |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 227 | py::module m("example", "pybind11 example plugin"); |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 228 | |
| 229 | py::class_<Animal> animal(m, "Animal"); |
| 230 | animal |
| 231 | .def("go", &Animal::go); |
| 232 | |
| 233 | py::class_<Dog>(m, "Dog", animal) |
| 234 | .def(py::init<>()); |
| 235 | |
| 236 | m.def("call_go", &call_go); |
| 237 | |
| 238 | return m.ptr(); |
| 239 | } |
| 240 | |
| 241 | However, these bindings are impossible to extend: ``Animal`` is not |
| 242 | constructible, and we clearly require some kind of "trampoline" that |
| 243 | redirects virtual calls back to Python. |
| 244 | |
| 245 | Defining a new type of ``Animal`` from within Python is possible but requires a |
| 246 | helper class that is defined as follows: |
| 247 | |
| 248 | .. code-block:: cpp |
| 249 | |
| 250 | class PyAnimal : public Animal { |
| 251 | public: |
| 252 | /* Inherit the constructors */ |
| 253 | using Animal::Animal; |
| 254 | |
| 255 | /* Trampoline (need one for each virtual function) */ |
| 256 | std::string go(int n_times) { |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 257 | PYBIND11_OVERLOAD_PURE( |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 258 | std::string, /* Return type */ |
| 259 | Animal, /* Parent class */ |
| 260 | go, /* Name of function */ |
| 261 | n_times /* Argument(s) */ |
| 262 | ); |
| 263 | } |
| 264 | }; |
| 265 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 266 | The macro :func:`PYBIND11_OVERLOAD_PURE` should be used for pure virtual |
| 267 | functions, and :func:`PYBIND11_OVERLOAD` should be used for functions which have |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 268 | a default implementation. The binding code also needs a few minor adaptations |
| 269 | (highlighted): |
| 270 | |
| 271 | .. code-block:: cpp |
| 272 | :emphasize-lines: 4,6,7 |
| 273 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 274 | PYBIND11_PLUGIN(example) { |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 275 | py::module m("example", "pybind11 example plugin"); |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 276 | |
| 277 | py::class_<PyAnimal> animal(m, "Animal"); |
| 278 | animal |
| 279 | .alias<Animal>() |
| 280 | .def(py::init<>()) |
| 281 | .def("go", &Animal::go); |
| 282 | |
| 283 | py::class_<Dog>(m, "Dog", animal) |
| 284 | .def(py::init<>()); |
| 285 | |
| 286 | m.def("call_go", &call_go); |
| 287 | |
| 288 | return m.ptr(); |
| 289 | } |
| 290 | |
| 291 | Importantly, the trampoline helper class is used as the template argument to |
| 292 | :class:`class_`, and a call to :func:`class_::alias` informs the binding |
| 293 | generator that this is merely an alias for the underlying type ``Animal``. |
| 294 | Following this, we are able to define a constructor as usual. |
| 295 | |
| 296 | The Python session below shows how to override ``Animal::go`` and invoke it via |
| 297 | a virtual method call. |
| 298 | |
Wenzel Jakob | de3ad07 | 2016-02-02 11:38:21 +0100 | [diff] [blame] | 299 | .. code-block:: python |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 300 | |
| 301 | >>> from example import * |
| 302 | >>> d = Dog() |
| 303 | >>> call_go(d) |
| 304 | u'woof! woof! woof! ' |
| 305 | >>> class Cat(Animal): |
| 306 | ... def go(self, n_times): |
| 307 | ... return "meow! " * n_times |
| 308 | ... |
| 309 | >>> c = Cat() |
| 310 | >>> call_go(c) |
| 311 | u'meow! meow! meow! ' |
| 312 | |
| 313 | .. seealso:: |
| 314 | |
| 315 | The file :file:`example/example12.cpp` contains a complete example that |
| 316 | demonstrates how to override virtual functions using pybind11 in more |
| 317 | detail. |
| 318 | |
Wenzel Jakob | ecdd868 | 2015-12-07 18:17:58 +0100 | [diff] [blame] | 319 | |
| 320 | Global Interpreter Lock (GIL) |
| 321 | ============================= |
| 322 | |
| 323 | The classes :class:`gil_scoped_release` and :class:`gil_scoped_acquire` can be |
| 324 | used to acquire and release the global interpreter lock in the body of a C++ |
| 325 | function call. In this way, long-running C++ code can be parallelized using |
| 326 | multiple Python threads. Taking the previous section as an example, this could |
| 327 | be realized as follows (important changes highlighted): |
| 328 | |
| 329 | .. code-block:: cpp |
| 330 | :emphasize-lines: 8,9,33,34 |
| 331 | |
| 332 | class PyAnimal : public Animal { |
| 333 | public: |
| 334 | /* Inherit the constructors */ |
| 335 | using Animal::Animal; |
| 336 | |
| 337 | /* Trampoline (need one for each virtual function) */ |
| 338 | std::string go(int n_times) { |
| 339 | /* Acquire GIL before calling Python code */ |
Wenzel Jakob | a4caa85 | 2015-12-14 12:39:02 +0100 | [diff] [blame] | 340 | py::gil_scoped_acquire acquire; |
Wenzel Jakob | ecdd868 | 2015-12-07 18:17:58 +0100 | [diff] [blame] | 341 | |
| 342 | PYBIND11_OVERLOAD_PURE( |
| 343 | std::string, /* Return type */ |
| 344 | Animal, /* Parent class */ |
| 345 | go, /* Name of function */ |
| 346 | n_times /* Argument(s) */ |
| 347 | ); |
| 348 | } |
| 349 | }; |
| 350 | |
| 351 | PYBIND11_PLUGIN(example) { |
| 352 | py::module m("example", "pybind11 example plugin"); |
| 353 | |
| 354 | py::class_<PyAnimal> animal(m, "Animal"); |
| 355 | animal |
| 356 | .alias<Animal>() |
| 357 | .def(py::init<>()) |
| 358 | .def("go", &Animal::go); |
| 359 | |
| 360 | py::class_<Dog>(m, "Dog", animal) |
| 361 | .def(py::init<>()); |
| 362 | |
| 363 | m.def("call_go", [](Animal *animal) -> std::string { |
| 364 | /* Release GIL before calling into (potentially long-running) C++ code */ |
Wenzel Jakob | a4caa85 | 2015-12-14 12:39:02 +0100 | [diff] [blame] | 365 | py::gil_scoped_release release; |
Wenzel Jakob | ecdd868 | 2015-12-07 18:17:58 +0100 | [diff] [blame] | 366 | return call_go(animal); |
| 367 | }); |
| 368 | |
| 369 | return m.ptr(); |
| 370 | } |
| 371 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 372 | Passing STL data structures |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 373 | =========================== |
| 374 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 375 | When including the additional header file :file:`pybind11/stl.h`, conversions |
Wenzel Jakob | 978e376 | 2016-04-07 18:00:41 +0200 | [diff] [blame] | 376 | between ``std::vector<>``, ``std::list<>``, ``std::set<>``, and ``std::map<>`` |
| 377 | and the Python ``list``, ``set`` and ``dict`` data structures are automatically |
| 378 | enabled. The types ``std::pair<>`` and ``std::tuple<>`` are already supported |
| 379 | out of the box with just the core :file:`pybind11/pybind11.h` header. |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 380 | |
| 381 | .. note:: |
| 382 | |
Wenzel Jakob | 44db04f | 2015-12-14 12:40:45 +0100 | [diff] [blame] | 383 | Arbitrary nesting of any of these types is supported. |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 384 | |
| 385 | .. seealso:: |
| 386 | |
| 387 | The file :file:`example/example2.cpp` contains a complete example that |
| 388 | demonstrates how to pass STL data types in more detail. |
| 389 | |
Wenzel Jakob | b282595 | 2016-04-13 23:33:00 +0200 | [diff] [blame] | 390 | Binding sequence data types, iterators, the slicing protocol, etc. |
| 391 | ================================================================== |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 392 | |
| 393 | Please refer to the supplemental example for details. |
| 394 | |
| 395 | .. seealso:: |
| 396 | |
| 397 | The file :file:`example/example6.cpp` contains a complete example that |
| 398 | shows how to bind a sequence data type, including length queries |
| 399 | (``__len__``), iterators (``__iter__``), the slicing protocol and other |
| 400 | kinds of useful operations. |
| 401 | |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 402 | Return value policies |
| 403 | ===================== |
| 404 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 405 | Python and C++ use wildly different ways of managing the memory and lifetime of |
| 406 | objects managed by them. This can lead to issues when creating bindings for |
| 407 | functions that return a non-trivial type. Just by looking at the type |
| 408 | information, it is not clear whether Python should take charge of the returned |
| 409 | value and eventually free its resources, or if this is handled on the C++ side. |
| 410 | For this reason, pybind11 provides a several `return value policy` annotations |
| 411 | that can be passed to the :func:`module::def` and :func:`class_::def` |
Wenzel Jakob | 61d67f0 | 2015-12-14 12:53:06 +0100 | [diff] [blame] | 412 | functions. The default policy is :enum:`return_value_policy::automatic`. |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 413 | |
Wenzel Jakob | f7b5874 | 2016-04-25 23:04:27 +0200 | [diff] [blame^] | 414 | +--------------------------------------------------+----------------------------------------------------------------------------+ |
| 415 | | Return value policy | Description | |
| 416 | +==================================================+============================================================================+ |
| 417 | | :enum:`return_value_policy::automatic` | This is the default return value policy, which falls back to the policy | |
| 418 | | | :enum:`return_value_policy::take_ownership` when the return value is a | |
| 419 | | | pointer. Otherwise, it uses :enum::`return_value::move` or | |
| 420 | | | :enum::`return_value::copy` for rvalue and lvalue references, respectively.| |
| 421 | | | See below for a description of what all of these different policies do. | |
| 422 | +--------------------------------------------------+----------------------------------------------------------------------------+ |
| 423 | | :enum:`return_value_policy::automatic_reference` | As above, but use policy :enum:`return_value_policy::reference` when the | |
| 424 | | | return value is a pointer. | |
| 425 | +--------------------------------------------------+----------------------------------------------------------------------------+ |
| 426 | | :enum:`return_value_policy::take_ownership` | Reference an existing object (i.e. do not create a new copy) and take | |
| 427 | | | ownership. Python will call the destructor and delete operator when the | |
| 428 | | | object's reference count reaches zero. Undefined behavior ensues when the | |
| 429 | | | C++ side does the same.. | |
| 430 | +--------------------------------------------------+----------------------------------------------------------------------------+ |
| 431 | | :enum:`return_value_policy::copy` | Create a new copy of the returned object, which will be owned by Python. | |
| 432 | | | This policy is comparably safe because the lifetimes of the two instances | |
| 433 | | | are decoupled. | |
| 434 | +--------------------------------------------------+----------------------------------------------------------------------------+ |
| 435 | | :enum:`return_value_policy::move` | Use ``std::move`` to move the return value contents into a new instance | |
| 436 | | | that will be owned by Python. This policy is comparably safe because the | |
| 437 | | | lifetimes of the two instances (move source and destination) are decoupled.| |
| 438 | +--------------------------------------------------+----------------------------------------------------------------------------+ |
| 439 | | :enum:`return_value_policy::reference` | Reference an existing object, but do not take ownership. The C++ side is | |
| 440 | | | responsible for managing the object's lifetime and deallocating it when | |
| 441 | | | it is no longer used. Warning: undefined behavior will ensue when the C++ | |
| 442 | | | side deletes an object that is still referenced by Python. | |
| 443 | +--------------------------------------------------+----------------------------------------------------------------------------+ |
| 444 | | :enum:`return_value_policy::reference_internal` | Reference the object, but do not take ownership. The object is considered | |
| 445 | | | be owned by the C++ instance whose method or property returned it. The | |
| 446 | | | Python object will increase the reference count of this 'parent' by 1 | |
| 447 | | | to ensure that it won't be deallocated while Python is using the 'child' | |
| 448 | +--------------------------------------------------+----------------------------------------------------------------------------+ |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 449 | |
| 450 | .. warning:: |
| 451 | |
Wenzel Jakob | f7b5874 | 2016-04-25 23:04:27 +0200 | [diff] [blame^] | 452 | Code with invalid call policies might access unitialized memory or free |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 453 | data structures multiple times, which can lead to hard-to-debug |
| 454 | non-determinism and segmentation faults, hence it is worth spending the |
| 455 | time to understand all the different options above. |
| 456 | |
Wenzel Jakob | f7b5874 | 2016-04-25 23:04:27 +0200 | [diff] [blame^] | 457 | .. note:: |
| 458 | |
| 459 | The next section on :ref:`call_policies` discusses *call policies* that can be |
| 460 | specified *in addition* to a return value policy from the list above. Call |
| 461 | policies indicate reference relationships that can involve both return values |
| 462 | and parameters of functions. |
| 463 | |
| 464 | .. note:: |
| 465 | |
| 466 | As an alternative to elaborate call policies and lifetime management logic, |
| 467 | consider using smart pointers (see :ref:`smart_pointers` for details) that |
| 468 | can be used to share reference count information between C++ and Python. |
| 469 | |
| 470 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 471 | See below for an example that uses the |
| 472 | :enum:`return_value_policy::reference_internal` policy. |
| 473 | |
| 474 | .. code-block:: cpp |
| 475 | |
| 476 | class Example { |
| 477 | public: |
| 478 | Internal &get_internal() { return internal; } |
| 479 | private: |
| 480 | Internal internal; |
| 481 | }; |
| 482 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 483 | PYBIND11_PLUGIN(example) { |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 484 | py::module m("example", "pybind11 example plugin"); |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 485 | |
| 486 | py::class_<Example>(m, "Example") |
| 487 | .def(py::init<>()) |
Wenzel Jakob | 978e376 | 2016-04-07 18:00:41 +0200 | [diff] [blame] | 488 | .def("get_internal", &Example::get_internal, "Return the internal data", py::return_value_policy::reference_internal); |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 489 | |
| 490 | return m.ptr(); |
| 491 | } |
| 492 | |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 493 | |
Wenzel Jakob | f7b5874 | 2016-04-25 23:04:27 +0200 | [diff] [blame^] | 494 | .. _call_policies: |
| 495 | |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 496 | Additional call policies |
| 497 | ======================== |
| 498 | |
| 499 | In addition to the above return value policies, further `call policies` can be |
| 500 | specified to indicate dependencies between parameters. There is currently just |
| 501 | one policy named ``keep_alive<Nurse, Patient>``, which indicates that the |
| 502 | argument with index ``Patient`` should be kept alive at least until the |
| 503 | argument with index ``Nurse`` is freed by the garbage collector; argument |
| 504 | indices start at one, while zero refers to the return value. Arbitrarily many |
| 505 | call policies can be specified. |
| 506 | |
| 507 | For instance, binding code for a a list append operation that ties the lifetime |
| 508 | of the newly added element to the underlying container might be declared as |
| 509 | follows: |
| 510 | |
| 511 | .. code-block:: cpp |
| 512 | |
| 513 | py::class_<List>(m, "List") |
| 514 | .def("append", &List::append, py::keep_alive<1, 2>()); |
| 515 | |
| 516 | .. note:: |
| 517 | |
| 518 | ``keep_alive`` is analogous to the ``with_custodian_and_ward`` (if Nurse, |
| 519 | Patient != 0) and ``with_custodian_and_ward_postcall`` (if Nurse/Patient == |
| 520 | 0) policies from Boost.Python. |
| 521 | |
Wenzel Jakob | 6158716 | 2016-01-18 22:38:52 +0100 | [diff] [blame] | 522 | .. seealso:: |
| 523 | |
| 524 | The file :file:`example/example13.cpp` contains a complete example that |
| 525 | demonstrates using :class:`keep_alive` in more detail. |
| 526 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 527 | Implicit type conversions |
| 528 | ========================= |
| 529 | |
| 530 | Suppose that instances of two types ``A`` and ``B`` are used in a project, and |
| 531 | that an ``A`` can easily be converted into a an instance of type ``B`` (examples of this |
| 532 | could be a fixed and an arbitrary precision number type). |
| 533 | |
| 534 | .. code-block:: cpp |
| 535 | |
| 536 | py::class_<A>(m, "A") |
| 537 | /// ... members ... |
| 538 | |
| 539 | py::class_<B>(m, "B") |
| 540 | .def(py::init<A>()) |
| 541 | /// ... members ... |
| 542 | |
| 543 | m.def("func", |
| 544 | [](const B &) { /* .... */ } |
| 545 | ); |
| 546 | |
| 547 | To invoke the function ``func`` using a variable ``a`` containing an ``A`` |
| 548 | instance, we'd have to write ``func(B(a))`` in Python. On the other hand, C++ |
| 549 | will automatically apply an implicit type conversion, which makes it possible |
| 550 | to directly write ``func(a)``. |
| 551 | |
| 552 | In this situation (i.e. where ``B`` has a constructor that converts from |
| 553 | ``A``), the following statement enables similar implicit conversions on the |
| 554 | Python side: |
| 555 | |
| 556 | .. code-block:: cpp |
| 557 | |
| 558 | py::implicitly_convertible<A, B>(); |
| 559 | |
Wenzel Jakob | 9f0dfce | 2016-04-06 17:38:18 +0200 | [diff] [blame] | 560 | Unique pointers |
| 561 | =============== |
| 562 | |
| 563 | Given a class ``Example`` with Python bindings, it's possible to return |
| 564 | instances wrapped in C++11 unique pointers, like so |
| 565 | |
| 566 | .. code-block:: cpp |
| 567 | |
| 568 | std::unique_ptr<Example> create_example() { return std::unique_ptr<Example>(new Example()); } |
| 569 | |
| 570 | .. code-block:: cpp |
| 571 | |
| 572 | m.def("create_example", &create_example); |
| 573 | |
| 574 | In other words, there is nothing special that needs to be done. While returning |
| 575 | unique pointers in this way is allowed, it is *illegal* to use them as function |
| 576 | arguments. For instance, the following function signature cannot be processed |
| 577 | by pybind11. |
| 578 | |
| 579 | .. code-block:: cpp |
| 580 | |
| 581 | void do_something_with_example(std::unique_ptr<Example> ex) { ... } |
| 582 | |
| 583 | The above signature would imply that Python needs to give up ownership of an |
| 584 | object that is passed to this function, which is generally not possible (for |
| 585 | instance, the object might be referenced elsewhere). |
| 586 | |
Wenzel Jakob | f7b5874 | 2016-04-25 23:04:27 +0200 | [diff] [blame^] | 587 | .. _smart_pointers: |
| 588 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 589 | Smart pointers |
| 590 | ============== |
| 591 | |
Wenzel Jakob | 9f0dfce | 2016-04-06 17:38:18 +0200 | [diff] [blame] | 592 | This section explains how to pass values that are wrapped in "smart" pointer |
| 593 | types with internal reference counting. For simpler C++11 unique pointers, |
| 594 | please refer to the previous section. |
| 595 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 596 | The binding generator for classes (:class:`class_`) takes an optional second |
| 597 | template type, which denotes a special *holder* type that is used to manage |
| 598 | references to the object. When wrapping a type named ``Type``, the default |
| 599 | value of this template parameter is ``std::unique_ptr<Type>``, which means that |
| 600 | the object is deallocated when Python's reference count goes to zero. |
| 601 | |
Wenzel Jakob | 1853b65 | 2015-10-18 15:38:50 +0200 | [diff] [blame] | 602 | It is possible to switch to other types of reference counting wrappers or smart |
| 603 | pointers, which is useful in codebases that rely on them. For instance, the |
| 604 | following snippet causes ``std::shared_ptr`` to be used instead. |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 605 | |
| 606 | .. code-block:: cpp |
| 607 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 608 | py::class_<Example, std::shared_ptr<Example> /* <- holder type */> obj(m, "Example"); |
Wenzel Jakob | 5ef1219 | 2015-12-15 17:07:35 +0100 | [diff] [blame] | 609 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 610 | Note that any particular class can only be associated with a single holder type. |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 611 | |
Wenzel Jakob | 1853b65 | 2015-10-18 15:38:50 +0200 | [diff] [blame] | 612 | To enable transparent conversions for functions that take shared pointers as an |
Wenzel Jakob | 5ef1219 | 2015-12-15 17:07:35 +0100 | [diff] [blame] | 613 | argument or that return them, a macro invocation similar to the following must |
Wenzel Jakob | 1853b65 | 2015-10-18 15:38:50 +0200 | [diff] [blame] | 614 | be declared at the top level before any binding code: |
| 615 | |
| 616 | .. code-block:: cpp |
| 617 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 618 | PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>); |
Wenzel Jakob | 1853b65 | 2015-10-18 15:38:50 +0200 | [diff] [blame] | 619 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 620 | .. note:: |
Wenzel Jakob | 61d67f0 | 2015-12-14 12:53:06 +0100 | [diff] [blame] | 621 | |
| 622 | The first argument of :func:`PYBIND11_DECLARE_HOLDER_TYPE` should be a |
| 623 | placeholder name that is used as a template parameter of the second |
| 624 | argument. Thus, feel free to use any identifier, but use it consistently on |
| 625 | both sides; also, don't use the name of a type that already exists in your |
| 626 | codebase. |
| 627 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 628 | One potential stumbling block when using holder types is that they need to be |
| 629 | applied consistently. Can you guess what's broken about the following binding |
| 630 | code? |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 631 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 632 | .. code-block:: cpp |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 633 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 634 | class Child { }; |
Wenzel Jakob | 5ef1219 | 2015-12-15 17:07:35 +0100 | [diff] [blame] | 635 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 636 | class Parent { |
| 637 | public: |
| 638 | Parent() : child(std::make_shared<Child>()) { } |
| 639 | Child *get_child() { return child.get(); } /* Hint: ** DON'T DO THIS ** */ |
| 640 | private: |
| 641 | std::shared_ptr<Child> child; |
| 642 | }; |
Wenzel Jakob | 5ef1219 | 2015-12-15 17:07:35 +0100 | [diff] [blame] | 643 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 644 | PYBIND11_PLUGIN(example) { |
| 645 | py::module m("example"); |
Wenzel Jakob | 5ef1219 | 2015-12-15 17:07:35 +0100 | [diff] [blame] | 646 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 647 | py::class_<Child, std::shared_ptr<Child>>(m, "Child"); |
| 648 | |
| 649 | py::class_<Parent, std::shared_ptr<Parent>>(m, "Parent") |
| 650 | .def(py::init<>()) |
| 651 | .def("get_child", &Parent::get_child); |
| 652 | |
| 653 | return m.ptr(); |
| 654 | } |
| 655 | |
| 656 | The following Python code will cause undefined behavior (and likely a |
| 657 | segmentation fault). |
| 658 | |
| 659 | .. code-block:: python |
| 660 | |
| 661 | from example import Parent |
| 662 | print(Parent().get_child()) |
| 663 | |
| 664 | The problem is that ``Parent::get_child()`` returns a pointer to an instance of |
| 665 | ``Child``, but the fact that this instance is already managed by |
| 666 | ``std::shared_ptr<...>`` is lost when passing raw pointers. In this case, |
| 667 | pybind11 will create a second independent ``std::shared_ptr<...>`` that also |
| 668 | claims ownership of the pointer. In the end, the object will be freed **twice** |
| 669 | since these shared pointers have no way of knowing about each other. |
| 670 | |
| 671 | There are two ways to resolve this issue: |
| 672 | |
| 673 | 1. For types that are managed by a smart pointer class, never use raw pointers |
| 674 | in function arguments or return values. In other words: always consistently |
| 675 | wrap pointers into their designated holder types (such as |
| 676 | ``std::shared_ptr<...>``). In this case, the signature of ``get_child()`` |
| 677 | should be modified as follows: |
| 678 | |
| 679 | .. code-block:: cpp |
| 680 | |
| 681 | std::shared_ptr<Child> get_child() { return child; } |
| 682 | |
| 683 | 2. Adjust the definition of ``Child`` by specifying |
| 684 | ``std::enable_shared_from_this<T>`` (see cppreference_ for details) as a |
| 685 | base class. This adds a small bit of information to ``Child`` that allows |
| 686 | pybind11 to realize that there is already an existing |
| 687 | ``std::shared_ptr<...>`` and communicate with it. In this case, the |
| 688 | declaration of ``Child`` should look as follows: |
Wenzel Jakob | 5ef1219 | 2015-12-15 17:07:35 +0100 | [diff] [blame] | 689 | |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 690 | .. _cppreference: http://en.cppreference.com/w/cpp/memory/enable_shared_from_this |
| 691 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 692 | .. code-block:: cpp |
| 693 | |
| 694 | class Child : public std::enable_shared_from_this<Child> { }; |
| 695 | |
Wenzel Jakob | 5ef1219 | 2015-12-15 17:07:35 +0100 | [diff] [blame] | 696 | .. seealso:: |
| 697 | |
| 698 | The file :file:`example/example8.cpp` contains a complete example that |
| 699 | demonstrates how to work with custom reference-counting holder types in |
| 700 | more detail. |
| 701 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 702 | .. _custom_constructors: |
| 703 | |
| 704 | Custom constructors |
| 705 | =================== |
| 706 | |
| 707 | The syntax for binding constructors was previously introduced, but it only |
| 708 | works when a constructor with the given parameters actually exists on the C++ |
| 709 | side. To extend this to more general cases, let's take a look at what actually |
| 710 | happens under the hood: the following statement |
| 711 | |
| 712 | .. code-block:: cpp |
| 713 | |
| 714 | py::class_<Example>(m, "Example") |
| 715 | .def(py::init<int>()); |
| 716 | |
| 717 | is short hand notation for |
| 718 | |
| 719 | .. code-block:: cpp |
| 720 | |
| 721 | py::class_<Example>(m, "Example") |
| 722 | .def("__init__", |
| 723 | [](Example &instance, int arg) { |
| 724 | new (&instance) Example(arg); |
| 725 | } |
| 726 | ); |
| 727 | |
| 728 | In other words, :func:`init` creates an anonymous function that invokes an |
| 729 | in-place constructor. Memory allocation etc. is already take care of beforehand |
| 730 | within pybind11. |
| 731 | |
| 732 | Catching and throwing exceptions |
| 733 | ================================ |
| 734 | |
| 735 | When C++ code invoked from Python throws an ``std::exception``, it is |
| 736 | automatically converted into a Python ``Exception``. pybind11 defines multiple |
| 737 | special exception classes that will map to different types of Python |
| 738 | exceptions: |
| 739 | |
Wenzel Jakob | 978e376 | 2016-04-07 18:00:41 +0200 | [diff] [blame] | 740 | +--------------------------------------+------------------------------+ |
| 741 | | C++ exception type | Python exception type | |
| 742 | +======================================+==============================+ |
| 743 | | :class:`std::exception` | ``RuntimeError`` | |
| 744 | +--------------------------------------+------------------------------+ |
| 745 | | :class:`std::bad_alloc` | ``MemoryError`` | |
| 746 | +--------------------------------------+------------------------------+ |
| 747 | | :class:`std::domain_error` | ``ValueError`` | |
| 748 | +--------------------------------------+------------------------------+ |
| 749 | | :class:`std::invalid_argument` | ``ValueError`` | |
| 750 | +--------------------------------------+------------------------------+ |
| 751 | | :class:`std::length_error` | ``ValueError`` | |
| 752 | +--------------------------------------+------------------------------+ |
| 753 | | :class:`std::out_of_range` | ``ValueError`` | |
| 754 | +--------------------------------------+------------------------------+ |
| 755 | | :class:`std::range_error` | ``ValueError`` | |
| 756 | +--------------------------------------+------------------------------+ |
| 757 | | :class:`pybind11::stop_iteration` | ``StopIteration`` (used to | |
| 758 | | | implement custom iterators) | |
| 759 | +--------------------------------------+------------------------------+ |
| 760 | | :class:`pybind11::index_error` | ``IndexError`` (used to | |
| 761 | | | indicate out of bounds | |
| 762 | | | accesses in ``__getitem__``, | |
| 763 | | | ``__setitem__``, etc.) | |
| 764 | +--------------------------------------+------------------------------+ |
| 765 | | :class:`pybind11::error_already_set` | Indicates that the Python | |
| 766 | | | exception flag has already | |
| 767 | | | been initialized | |
| 768 | +--------------------------------------+------------------------------+ |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 769 | |
| 770 | When a Python function invoked from C++ throws an exception, it is converted |
| 771 | into a C++ exception of type :class:`error_already_set` whose string payload |
| 772 | contains a textual summary. |
| 773 | |
| 774 | There is also a special exception :class:`cast_error` that is thrown by |
| 775 | :func:`handle::call` when the input arguments cannot be converted to Python |
| 776 | objects. |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 777 | |
| 778 | Buffer protocol |
| 779 | =============== |
| 780 | |
| 781 | Python supports an extremely general and convenient approach for exchanging |
Wenzel Jakob | 978e376 | 2016-04-07 18:00:41 +0200 | [diff] [blame] | 782 | data between plugin libraries. Types can expose a buffer view [#f1]_, |
| 783 | which provides fast direct access to the raw internal representation. Suppose |
| 784 | we want to bind the following simplistic Matrix class: |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 785 | |
| 786 | .. code-block:: cpp |
| 787 | |
| 788 | class Matrix { |
| 789 | public: |
| 790 | Matrix(size_t rows, size_t cols) : m_rows(rows), m_cols(cols) { |
| 791 | m_data = new float[rows*cols]; |
| 792 | } |
| 793 | float *data() { return m_data; } |
| 794 | size_t rows() const { return m_rows; } |
| 795 | size_t cols() const { return m_cols; } |
| 796 | private: |
| 797 | size_t m_rows, m_cols; |
| 798 | float *m_data; |
| 799 | }; |
| 800 | |
| 801 | The following binding code exposes the ``Matrix`` contents as a buffer object, |
| 802 | making it possible to cast Matrixes into NumPy arrays. It is even possible to |
| 803 | completely avoid copy operations with Python expressions like |
| 804 | ``np.array(matrix_instance, copy = False)``. |
| 805 | |
| 806 | .. code-block:: cpp |
| 807 | |
| 808 | py::class_<Matrix>(m, "Matrix") |
| 809 | .def_buffer([](Matrix &m) -> py::buffer_info { |
| 810 | return py::buffer_info( |
| 811 | m.data(), /* Pointer to buffer */ |
| 812 | sizeof(float), /* Size of one scalar */ |
| 813 | py::format_descriptor<float>::value(), /* Python struct-style format descriptor */ |
| 814 | 2, /* Number of dimensions */ |
| 815 | { m.rows(), m.cols() }, /* Buffer dimensions */ |
| 816 | { sizeof(float) * m.rows(), /* Strides (in bytes) for each index */ |
| 817 | sizeof(float) } |
| 818 | ); |
| 819 | }); |
| 820 | |
| 821 | The snippet above binds a lambda function, which can create ``py::buffer_info`` |
| 822 | description records on demand describing a given matrix. The contents of |
| 823 | ``py::buffer_info`` mirror the Python buffer protocol specification. |
| 824 | |
| 825 | .. code-block:: cpp |
| 826 | |
| 827 | struct buffer_info { |
| 828 | void *ptr; |
| 829 | size_t itemsize; |
| 830 | std::string format; |
| 831 | int ndim; |
| 832 | std::vector<size_t> shape; |
| 833 | std::vector<size_t> strides; |
| 834 | }; |
| 835 | |
| 836 | To create a C++ function that can take a Python buffer object as an argument, |
| 837 | simply use the type ``py::buffer`` as one of its arguments. Buffers can exist |
| 838 | in a great variety of configurations, hence some safety checks are usually |
| 839 | necessary in the function body. Below, you can see an basic example on how to |
| 840 | define a custom constructor for the Eigen double precision matrix |
| 841 | (``Eigen::MatrixXd``) type, which supports initialization from compatible |
| 842 | buffer |
| 843 | objects (e.g. a NumPy matrix). |
| 844 | |
| 845 | .. code-block:: cpp |
| 846 | |
| 847 | py::class_<Eigen::MatrixXd>(m, "MatrixXd") |
| 848 | .def("__init__", [](Eigen::MatrixXd &m, py::buffer b) { |
| 849 | /* Request a buffer descriptor from Python */ |
| 850 | py::buffer_info info = b.request(); |
| 851 | |
| 852 | /* Some sanity checks ... */ |
| 853 | if (info.format != py::format_descriptor<double>::value()) |
| 854 | throw std::runtime_error("Incompatible format: expected a double array!"); |
| 855 | |
| 856 | if (info.ndim != 2) |
| 857 | throw std::runtime_error("Incompatible buffer dimension!"); |
| 858 | |
| 859 | if (info.strides[0] == sizeof(double)) { |
| 860 | /* Buffer has the right layout -- directly copy. */ |
| 861 | new (&m) Eigen::MatrixXd(info.shape[0], info.shape[1]); |
| 862 | memcpy(m.data(), info.ptr, sizeof(double) * m.size()); |
| 863 | } else { |
| 864 | /* Oops -- the buffer is transposed */ |
| 865 | new (&m) Eigen::MatrixXd(info.shape[1], info.shape[0]); |
| 866 | memcpy(m.data(), info.ptr, sizeof(double) * m.size()); |
| 867 | m.transposeInPlace(); |
| 868 | } |
| 869 | }); |
| 870 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 871 | .. seealso:: |
| 872 | |
| 873 | The file :file:`example/example7.cpp` contains a complete example that |
| 874 | demonstrates using the buffer protocol with pybind11 in more detail. |
| 875 | |
Wenzel Jakob | 1c329aa | 2016-04-13 02:37:36 +0200 | [diff] [blame] | 876 | .. [#f1] http://docs.python.org/3/c-api/buffer.html |
Wenzel Jakob | 978e376 | 2016-04-07 18:00:41 +0200 | [diff] [blame] | 877 | |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 878 | NumPy support |
| 879 | ============= |
| 880 | |
| 881 | By exchanging ``py::buffer`` with ``py::array`` in the above snippet, we can |
| 882 | restrict the function so that it only accepts NumPy arrays (rather than any |
Wenzel Jakob | 978e376 | 2016-04-07 18:00:41 +0200 | [diff] [blame] | 883 | type of Python object satisfying the buffer protocol). |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 884 | |
| 885 | In many situations, we want to define a function which only accepts a NumPy |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 886 | array of a certain data type. This is possible via the ``py::array_t<T>`` |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 887 | template. For instance, the following function requires the argument to be a |
| 888 | dense array of doubles in C-style ordering. |
| 889 | |
| 890 | .. code-block:: cpp |
| 891 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 892 | void f(py::array_t<double> array); |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 893 | |
| 894 | When it is invoked with a different type (e.g. an integer), the binding code |
Wenzel Jakob | 978e376 | 2016-04-07 18:00:41 +0200 | [diff] [blame] | 895 | will attempt to cast the input into a NumPy array of the requested type. Note |
| 896 | that this feature requires the :file:``pybind11/numpy.h`` header to be |
| 897 | included. |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 898 | |
| 899 | Vectorizing functions |
| 900 | ===================== |
| 901 | |
| 902 | Suppose we want to bind a function with the following signature to Python so |
| 903 | that it can process arbitrary NumPy array arguments (vectors, matrices, general |
| 904 | N-D arrays) in addition to its normal arguments: |
| 905 | |
| 906 | .. code-block:: cpp |
| 907 | |
| 908 | double my_func(int x, float y, double z); |
| 909 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 910 | After including the ``pybind11/numpy.h`` header, this is extremely simple: |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 911 | |
| 912 | .. code-block:: cpp |
| 913 | |
| 914 | m.def("vectorized_func", py::vectorize(my_func)); |
| 915 | |
| 916 | Invoking the function like below causes 4 calls to be made to ``my_func`` with |
Wenzel Jakob | 978e376 | 2016-04-07 18:00:41 +0200 | [diff] [blame] | 917 | each of the the array elements. The significant advantage of this compared to |
| 918 | solutions like ``numpy.vectorize()`` is that the loop over the elements runs |
| 919 | entirely on the C++ side and can be crunched down into a tight, optimized loop |
| 920 | by the compiler. The result is returned as a NumPy array of type |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 921 | ``numpy.dtype.float64``. |
| 922 | |
| 923 | .. code-block:: python |
| 924 | |
| 925 | >>> x = np.array([[1, 3],[5, 7]]) |
| 926 | >>> y = np.array([[2, 4],[6, 8]]) |
| 927 | >>> z = 3 |
| 928 | >>> result = vectorized_func(x, y, z) |
| 929 | |
| 930 | The scalar argument ``z`` is transparently replicated 4 times. The input |
| 931 | arrays ``x`` and ``y`` are automatically converted into the right types (they |
| 932 | are of type ``numpy.dtype.int64`` but need to be ``numpy.dtype.int32`` and |
| 933 | ``numpy.dtype.float32``, respectively) |
| 934 | |
| 935 | Sometimes we might want to explitly exclude an argument from the vectorization |
| 936 | because it makes little sense to wrap it in a NumPy array. For instance, |
| 937 | suppose the function signature was |
| 938 | |
| 939 | .. code-block:: cpp |
| 940 | |
| 941 | double my_func(int x, float y, my_custom_type *z); |
| 942 | |
| 943 | This can be done with a stateful Lambda closure: |
| 944 | |
| 945 | .. code-block:: cpp |
| 946 | |
| 947 | // Vectorize a lambda function with a capture object (e.g. to exclude some arguments from the vectorization) |
| 948 | m.def("vectorized_func", |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 949 | [](py::array_t<int> x, py::array_t<float> y, my_custom_type *z) { |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 950 | auto stateful_closure = [z](int x, float y) { return my_func(x, y, z); }; |
| 951 | return py::vectorize(stateful_closure)(x, y); |
| 952 | } |
| 953 | ); |
| 954 | |
Wenzel Jakob | 6158716 | 2016-01-18 22:38:52 +0100 | [diff] [blame] | 955 | In cases where the computation is too complicated to be reduced to |
| 956 | ``vectorize``, it will be necessary to create and access the buffer contents |
| 957 | manually. The following snippet contains a complete example that shows how this |
| 958 | works (the code is somewhat contrived, since it could have been done more |
| 959 | simply using ``vectorize``). |
| 960 | |
| 961 | .. code-block:: cpp |
| 962 | |
| 963 | #include <pybind11/pybind11.h> |
| 964 | #include <pybind11/numpy.h> |
| 965 | |
| 966 | namespace py = pybind11; |
| 967 | |
| 968 | py::array_t<double> add_arrays(py::array_t<double> input1, py::array_t<double> input2) { |
| 969 | auto buf1 = input1.request(), buf2 = input2.request(); |
| 970 | |
| 971 | if (buf1.ndim != 1 || buf2.ndim != 1) |
| 972 | throw std::runtime_error("Number of dimensions must be one"); |
| 973 | |
| 974 | if (buf1.shape[0] != buf2.shape[0]) |
| 975 | throw std::runtime_error("Input shapes must match"); |
| 976 | |
| 977 | auto result = py::array(py::buffer_info( |
| 978 | nullptr, /* Pointer to data (nullptr -> ask NumPy to allocate!) */ |
| 979 | sizeof(double), /* Size of one item */ |
| 980 | py::format_descriptor<double>::value(), /* Buffer format */ |
| 981 | buf1.ndim, /* How many dimensions? */ |
| 982 | { buf1.shape[0] }, /* Number of elements for each dimension */ |
| 983 | { sizeof(double) } /* Strides for each dimension */ |
| 984 | )); |
| 985 | |
| 986 | auto buf3 = result.request(); |
| 987 | |
| 988 | double *ptr1 = (double *) buf1.ptr, |
| 989 | *ptr2 = (double *) buf2.ptr, |
| 990 | *ptr3 = (double *) buf3.ptr; |
| 991 | |
| 992 | for (size_t idx = 0; idx < buf1.shape[0]; idx++) |
| 993 | ptr3[idx] = ptr1[idx] + ptr2[idx]; |
| 994 | |
| 995 | return result; |
| 996 | } |
| 997 | |
| 998 | PYBIND11_PLUGIN(test) { |
| 999 | py::module m("test"); |
| 1000 | m.def("add_arrays", &add_arrays, "Add two NumPy arrays"); |
| 1001 | return m.ptr(); |
| 1002 | } |
| 1003 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 1004 | .. seealso:: |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 1005 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 1006 | The file :file:`example/example10.cpp` contains a complete example that |
| 1007 | demonstrates using :func:`vectorize` in more detail. |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 1008 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 1009 | Functions taking Python objects as arguments |
| 1010 | ============================================ |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 1011 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 1012 | pybind11 exposes all major Python types using thin C++ wrapper classes. These |
| 1013 | wrapper classes can also be used as parameters of functions in bindings, which |
| 1014 | makes it possible to directly work with native Python types on the C++ side. |
| 1015 | For instance, the following statement iterates over a Python ``dict``: |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 1016 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 1017 | .. code-block:: cpp |
| 1018 | |
| 1019 | void print_dict(py::dict dict) { |
| 1020 | /* Easily interact with Python types */ |
| 1021 | for (auto item : dict) |
| 1022 | std::cout << "key=" << item.first << ", " |
| 1023 | << "value=" << item.second << std::endl; |
| 1024 | } |
| 1025 | |
| 1026 | Available types include :class:`handle`, :class:`object`, :class:`bool_`, |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 1027 | :class:`int_`, :class:`float_`, :class:`str`, :class:`bytes`, :class:`tuple`, |
| 1028 | :class:`list`, :class:`dict`, :class:`slice`, :class:`capsule`, |
| 1029 | :class:`function`, :class:`buffer`, :class:`array`, and :class:`array_t`. |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 1030 | |
Wenzel Jakob | 436b731 | 2015-10-20 01:04:30 +0200 | [diff] [blame] | 1031 | In this kind of mixed code, it is often necessary to convert arbitrary C++ |
| 1032 | types to Python, which can be done using :func:`cast`: |
| 1033 | |
| 1034 | .. code-block:: cpp |
| 1035 | |
| 1036 | MyClass *cls = ..; |
| 1037 | py::object obj = py::cast(cls); |
| 1038 | |
| 1039 | The reverse direction uses the following syntax: |
| 1040 | |
| 1041 | .. code-block:: cpp |
| 1042 | |
| 1043 | py::object obj = ...; |
| 1044 | MyClass *cls = obj.cast<MyClass *>(); |
| 1045 | |
| 1046 | When conversion fails, both directions throw the exception :class:`cast_error`. |
| 1047 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 1048 | .. seealso:: |
| 1049 | |
| 1050 | The file :file:`example/example2.cpp` contains a complete example that |
| 1051 | demonstrates passing native Python types in more detail. |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 1052 | |
| 1053 | Default arguments revisited |
| 1054 | =========================== |
| 1055 | |
| 1056 | The section on :ref:`default_args` previously discussed basic usage of default |
| 1057 | arguments using pybind11. One noteworthy aspect of their implementation is that |
| 1058 | default arguments are converted to Python objects right at declaration time. |
| 1059 | Consider the following example: |
| 1060 | |
| 1061 | .. code-block:: cpp |
| 1062 | |
| 1063 | py::class_<MyClass>("MyClass") |
| 1064 | .def("myFunction", py::arg("arg") = SomeType(123)); |
| 1065 | |
| 1066 | In this case, pybind11 must already be set up to deal with values of the type |
| 1067 | ``SomeType`` (via a prior instantiation of ``py::class_<SomeType>``), or an |
| 1068 | exception will be thrown. |
| 1069 | |
| 1070 | Another aspect worth highlighting is that the "preview" of the default argument |
| 1071 | in the function signature is generated using the object's ``__repr__`` method. |
| 1072 | If not available, the signature may not be very helpful, e.g.: |
| 1073 | |
| 1074 | .. code-block:: python |
| 1075 | |
| 1076 | FUNCTIONS |
| 1077 | ... |
| 1078 | | myFunction(...) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 1079 | | Signature : (MyClass, arg : SomeType = <SomeType object at 0x101b7b080>) -> NoneType |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 1080 | ... |
| 1081 | |
| 1082 | The first way of addressing this is by defining ``SomeType.__repr__``. |
| 1083 | Alternatively, it is possible to specify the human-readable preview of the |
| 1084 | default argument manually using the ``arg_t`` notation: |
| 1085 | |
| 1086 | .. code-block:: cpp |
| 1087 | |
| 1088 | py::class_<MyClass>("MyClass") |
| 1089 | .def("myFunction", py::arg_t<SomeType>("arg", SomeType(123), "SomeType(123)")); |
| 1090 | |
Wenzel Jakob | c769fce | 2016-03-03 12:03:30 +0100 | [diff] [blame] | 1091 | Sometimes it may be necessary to pass a null pointer value as a default |
| 1092 | argument. In this case, remember to cast it to the underlying type in question, |
| 1093 | like so: |
| 1094 | |
| 1095 | .. code-block:: cpp |
| 1096 | |
| 1097 | py::class_<MyClass>("MyClass") |
| 1098 | .def("myFunction", py::arg("arg") = (SomeType *) nullptr); |
| 1099 | |
Wenzel Jakob | 2dfbade | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 1100 | Partitioning code over multiple extension modules |
| 1101 | ================================================= |
| 1102 | |
Wenzel Jakob | 90d2f5e | 2016-04-11 14:30:11 +0200 | [diff] [blame] | 1103 | It's straightforward to split binding code over multiple extension modules, |
| 1104 | while referencing types that are declared elsewhere. Everything "just" works |
| 1105 | without any special precautions. One exception to this rule occurs when |
| 1106 | extending a type declared in another extension module. Recall the basic example |
| 1107 | from Section :ref:`inheritance`. |
Wenzel Jakob | 2dfbade | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 1108 | |
| 1109 | .. code-block:: cpp |
| 1110 | |
| 1111 | py::class_<Pet> pet(m, "Pet"); |
| 1112 | pet.def(py::init<const std::string &>()) |
| 1113 | .def_readwrite("name", &Pet::name); |
| 1114 | |
| 1115 | py::class_<Dog>(m, "Dog", pet /* <- specify parent */) |
| 1116 | .def(py::init<const std::string &>()) |
| 1117 | .def("bark", &Dog::bark); |
| 1118 | |
| 1119 | Suppose now that ``Pet`` bindings are defined in a module named ``basic``, |
| 1120 | whereas the ``Dog`` bindings are defined somewhere else. The challenge is of |
| 1121 | course that the variable ``pet`` is not available anymore though it is needed |
| 1122 | to indicate the inheritance relationship to the constructor of ``class_<Dog>``. |
| 1123 | However, it can be acquired as follows: |
| 1124 | |
| 1125 | .. code-block:: cpp |
| 1126 | |
| 1127 | py::object pet = (py::object) py::module::import("basic").attr("Pet"); |
| 1128 | |
| 1129 | py::class_<Dog>(m, "Dog", pet) |
| 1130 | .def(py::init<const std::string &>()) |
| 1131 | .def("bark", &Dog::bark); |
| 1132 | |
Wenzel Jakob | 8d862b3 | 2016-03-06 13:37:22 +0100 | [diff] [blame] | 1133 | Alternatively, we can rely on the ``base`` tag, which performs an automated |
| 1134 | lookup of the corresponding Python type. However, this also requires invoking |
| 1135 | the ``import`` function once to ensure that the pybind11 binding code of the |
| 1136 | module ``basic`` has been executed. |
| 1137 | |
Wenzel Jakob | 8d862b3 | 2016-03-06 13:37:22 +0100 | [diff] [blame] | 1138 | .. code-block:: cpp |
| 1139 | |
| 1140 | py::module::import("basic"); |
| 1141 | |
| 1142 | py::class_<Dog>(m, "Dog", py::base<Pet>()) |
| 1143 | .def(py::init<const std::string &>()) |
| 1144 | .def("bark", &Dog::bark); |
Wenzel Jakob | eda978e | 2016-03-15 15:05:40 +0100 | [diff] [blame] | 1145 | |
Wenzel Jakob | 978e376 | 2016-04-07 18:00:41 +0200 | [diff] [blame] | 1146 | Naturally, both methods will fail when there are cyclic dependencies. |
| 1147 | |
Wenzel Jakob | 90d2f5e | 2016-04-11 14:30:11 +0200 | [diff] [blame] | 1148 | Note that compiling code which has its default symbol visibility set to |
| 1149 | *hidden* (e.g. via the command line flag ``-fvisibility=hidden`` on GCC/Clang) can interfere with the |
| 1150 | ability to access types defined in another extension module. Workarounds |
| 1151 | include changing the global symbol visibility (not recommended, because it will |
| 1152 | lead unnecessarily large binaries) or manually exporting types that are |
| 1153 | accessed by multiple extension modules: |
| 1154 | |
| 1155 | .. code-block:: cpp |
| 1156 | |
| 1157 | #ifdef _WIN32 |
| 1158 | # define EXPORT_TYPE __declspec(dllexport) |
| 1159 | #else |
| 1160 | # define EXPORT_TYPE __attribute__ ((visibility("default"))) |
| 1161 | #endif |
| 1162 | |
| 1163 | class EXPORT_TYPE Dog : public Animal { |
| 1164 | ... |
| 1165 | }; |
| 1166 | |
| 1167 | |
Wenzel Jakob | eda978e | 2016-03-15 15:05:40 +0100 | [diff] [blame] | 1168 | Treating STL data structures as opaque objects |
| 1169 | ============================================== |
| 1170 | |
| 1171 | pybind11 heavily relies on a template matching mechanism to convert parameters |
| 1172 | and return values that are constructed from STL data types such as vectors, |
| 1173 | linked lists, hash tables, etc. This even works in a recursive manner, for |
| 1174 | instance to deal with lists of hash maps of pairs of elementary and custom |
| 1175 | types, etc. |
| 1176 | |
Wenzel Jakob | 0871228 | 2016-04-22 16:52:15 +0200 | [diff] [blame] | 1177 | However, a fundamental limitation of this approach is that internal conversions |
| 1178 | between Python and C++ types involve a copy operation that prevents |
Wenzel Jakob | 978e376 | 2016-04-07 18:00:41 +0200 | [diff] [blame] | 1179 | pass-by-reference semantics. What does this mean? |
Wenzel Jakob | eda978e | 2016-03-15 15:05:40 +0100 | [diff] [blame] | 1180 | |
| 1181 | Suppose we bind the following function |
| 1182 | |
| 1183 | .. code-block:: cpp |
| 1184 | |
| 1185 | void append_1(std::vector<int> &v) { |
| 1186 | v.push_back(1); |
| 1187 | } |
| 1188 | |
| 1189 | and call it as follows from Python: |
| 1190 | |
| 1191 | .. code-block:: python |
| 1192 | |
| 1193 | >>> v = [5, 6] |
| 1194 | >>> append_1(v) |
| 1195 | >>> print(v) |
| 1196 | [5, 6] |
| 1197 | |
| 1198 | As you can see, when passing STL data structures by reference, modifications |
Wenzel Jakob | 0871228 | 2016-04-22 16:52:15 +0200 | [diff] [blame] | 1199 | are not propagated back the Python side. A similar situation arises when |
| 1200 | exposing STL data structures using the ``def_readwrite`` or ``def_readonly`` |
| 1201 | functions: |
Wenzel Jakob | eda978e | 2016-03-15 15:05:40 +0100 | [diff] [blame] | 1202 | |
Wenzel Jakob | 0871228 | 2016-04-22 16:52:15 +0200 | [diff] [blame] | 1203 | .. code-block:: cpp |
| 1204 | |
| 1205 | /* ... definition ... */ |
| 1206 | |
| 1207 | class MyClass { |
| 1208 | std::vector<int> contents; |
| 1209 | }; |
| 1210 | |
| 1211 | /* ... binding code ... */ |
| 1212 | |
| 1213 | py::class_<MyClass>(m, "MyClass") |
| 1214 | .def(py::init<>) |
| 1215 | .def_readwrite("contents", &MyClass::contents); |
| 1216 | |
| 1217 | In this case, properties can be read and written in their entirety. However, an |
| 1218 | ``append`` operaton involving such a list type has no effect: |
| 1219 | |
| 1220 | .. code-block:: python |
| 1221 | |
| 1222 | >>> m = MyClass() |
| 1223 | >>> m.contents = [5, 6] |
| 1224 | >>> print(m.contents) |
| 1225 | [5, 6] |
| 1226 | >>> m.contents.append(7) |
| 1227 | >>> print(m.contents) |
| 1228 | [5, 6] |
| 1229 | |
| 1230 | To deal with both of the above situations, pybind11 contains a simple template |
| 1231 | wrapper class named ``opaque<T>``. |
| 1232 | |
| 1233 | ``opaque<T>`` disables pybind11's template-based conversion machinery for |
Wenzel Jakob | eda978e | 2016-03-15 15:05:40 +0100 | [diff] [blame] | 1234 | ``T`` and can be used to treat STL types as opaque objects, whose contents are |
| 1235 | never inspected or extracted (thus, they can be passed by reference). |
| 1236 | The downside of this approach is that it the binding code becomes a bit more |
| 1237 | wordy. The above function can be bound using the following wrapper code: |
| 1238 | |
| 1239 | .. code-block:: cpp |
| 1240 | |
| 1241 | m.def("append_1", [](py::opaque<std::vector<int>> &v) { append_1(v); }); |
| 1242 | |
| 1243 | Opaque types must also have a dedicated ``class_`` declaration to define a |
| 1244 | set of admissible operations. |
| 1245 | |
| 1246 | .. seealso:: |
| 1247 | |
| 1248 | The file :file:`example/example14.cpp` contains a complete example that |
Wenzel Jakob | 0871228 | 2016-04-22 16:52:15 +0200 | [diff] [blame] | 1249 | demonstrates how to create and expose opaque types using pybind11 in more |
| 1250 | detail. |
Wenzel Jakob | 1c329aa | 2016-04-13 02:37:36 +0200 | [diff] [blame] | 1251 | |
| 1252 | Pickling support |
| 1253 | ================ |
| 1254 | |
| 1255 | Python's ``pickle`` module provides a powerful facility to serialize and |
| 1256 | de-serialize a Python object graph into a binary data stream. To pickle and |
Wenzel Jakob | 3d0e6ff | 2016-04-13 11:48:10 +0200 | [diff] [blame] | 1257 | unpickle C++ classes using pybind11, two additional functions must be provided. |
Wenzel Jakob | 1c329aa | 2016-04-13 02:37:36 +0200 | [diff] [blame] | 1258 | Suppose the class in question has the following signature: |
| 1259 | |
| 1260 | .. code-block:: cpp |
| 1261 | |
| 1262 | class Pickleable { |
| 1263 | public: |
| 1264 | Pickleable(const std::string &value) : m_value(value) { } |
| 1265 | const std::string &value() const { return m_value; } |
| 1266 | |
| 1267 | void setExtra(int extra) { m_extra = extra; } |
| 1268 | int extra() const { return m_extra; } |
| 1269 | private: |
| 1270 | std::string m_value; |
| 1271 | int m_extra = 0; |
| 1272 | }; |
| 1273 | |
| 1274 | The binding code including the requisite ``__setstate__`` and ``__getstate__`` methods [#f2]_ |
| 1275 | looks as follows: |
| 1276 | |
| 1277 | .. code-block:: cpp |
| 1278 | |
| 1279 | py::class_<Pickleable>(m, "Pickleable") |
| 1280 | .def(py::init<std::string>()) |
| 1281 | .def("value", &Pickleable::value) |
| 1282 | .def("extra", &Pickleable::extra) |
| 1283 | .def("setExtra", &Pickleable::setExtra) |
| 1284 | .def("__getstate__", [](const Pickleable &p) { |
| 1285 | /* Return a tuple that fully encodes the state of the object */ |
| 1286 | return py::make_tuple(p.value(), p.extra()); |
| 1287 | }) |
| 1288 | .def("__setstate__", [](Pickleable &p, py::tuple t) { |
| 1289 | if (t.size() != 2) |
| 1290 | throw std::runtime_error("Invalid state!"); |
| 1291 | |
Wenzel Jakob | d40885a | 2016-04-13 13:30:05 +0200 | [diff] [blame] | 1292 | /* Invoke the in-place constructor. Note that this is needed even |
| 1293 | when the object just has a trivial default constructor */ |
Wenzel Jakob | 1c329aa | 2016-04-13 02:37:36 +0200 | [diff] [blame] | 1294 | new (&p) Pickleable(t[0].cast<std::string>()); |
| 1295 | |
| 1296 | /* Assign any additional state */ |
| 1297 | p.setExtra(t[1].cast<int>()); |
| 1298 | }); |
| 1299 | |
| 1300 | An instance can now be pickled as follows: |
| 1301 | |
| 1302 | .. code-block:: python |
| 1303 | |
| 1304 | try: |
| 1305 | import cPickle as pickle # Use cPickle on Python 2.7 |
| 1306 | except ImportError: |
| 1307 | import pickle |
| 1308 | |
| 1309 | p = Pickleable("test_value") |
| 1310 | p.setExtra(15) |
Wenzel Jakob | 3d0e6ff | 2016-04-13 11:48:10 +0200 | [diff] [blame] | 1311 | data = pickle.dumps(p, -1) |
Wenzel Jakob | 1c329aa | 2016-04-13 02:37:36 +0200 | [diff] [blame] | 1312 | |
| 1313 | Note that only the cPickle module is supported on Python 2.7. It is also |
| 1314 | important to request usage of the highest protocol version using the ``-1`` |
Wenzel Jakob | d40885a | 2016-04-13 13:30:05 +0200 | [diff] [blame] | 1315 | argument to ``dumps``. Failure to follow these two steps will lead to important |
| 1316 | pybind11 memory allocation routines to be skipped during unpickling, which will |
| 1317 | likely cause memory corruption and/or segmentation faults. |
Wenzel Jakob | 1c329aa | 2016-04-13 02:37:36 +0200 | [diff] [blame] | 1318 | |
| 1319 | .. seealso:: |
| 1320 | |
| 1321 | The file :file:`example/example15.cpp` contains a complete example that |
| 1322 | demonstrates how to pickle and unpickle types using pybind11 in more detail. |
| 1323 | |
| 1324 | .. [#f2] http://docs.python.org/3/library/pickle.html#pickling-class-instances |
Wenzel Jakob | ef7a9b9 | 2016-04-13 18:41:59 +0200 | [diff] [blame] | 1325 | |
| 1326 | Generating documentation using Sphinx |
| 1327 | ===================================== |
| 1328 | |
| 1329 | Sphinx [#f3]_ has the ability to inspect the signatures and documentation |
| 1330 | strings in pybind11-based extension modules to automatically generate beautiful |
| 1331 | documentation in a variety formats. The pbtest repository [#f4]_ contains a |
| 1332 | simple example repository which uses this approach. |
| 1333 | |
| 1334 | There are two potential gotchas when using this approach: first, make sure that |
| 1335 | the resulting strings do not contain any :kbd:`TAB` characters, which break the |
| 1336 | docstring parsing routines. You may want to use C++11 raw string literals, |
| 1337 | which are convenient for multi-line comments. Conveniently, any excess |
| 1338 | indentation will be automatically be removed by Sphinx. However, for this to |
| 1339 | work, it is important that all lines are indented consistently, i.e.: |
| 1340 | |
| 1341 | .. code-block:: cpp |
| 1342 | |
| 1343 | // ok |
| 1344 | m.def("foo", &foo, R"mydelimiter( |
| 1345 | The foo function |
| 1346 | |
| 1347 | Parameters |
| 1348 | ---------- |
| 1349 | )mydelimiter"); |
| 1350 | |
| 1351 | // *not ok* |
| 1352 | m.def("foo", &foo, R"mydelimiter(The foo function |
| 1353 | |
| 1354 | Parameters |
| 1355 | ---------- |
| 1356 | )mydelimiter"); |
| 1357 | |
| 1358 | .. [#f3] http://www.sphinx-doc.org |
| 1359 | .. [#f4] http://github.com/pybind/pbtest |
| 1360 | |