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

1299 lines
46 KiB

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