misc.rst 13.8 KB
Newer Older
Dean Moldovan's avatar
Dean Moldovan committed
1
2
3
4
5
6
7
8
9
Miscellaneous
#############

.. _macro_notes:

General notes regarding convenience macros
==========================================

pybind11 provides a few convenience macros such as
10
:func:`PYBIND11_DECLARE_HOLDER_TYPE` and ``PYBIND11_OVERRIDE_*``. Since these
11
12
13
14
15
16
are "just" macros that are evaluated in the preprocessor (which has no concept
of types), they *will* get confused by commas in a template argument; for
example, consider:

.. code-block:: cpp

17
    PYBIND11_OVERRIDE(MyReturnType<T1, T2>, Class<T3, T4>, func)
18
19
20
21
22
23
24
25
26
27
28

The limitation of the C preprocessor interprets this as five arguments (with new
arguments beginning after each comma) rather than three.  To get around this,
there are two alternatives: you can use a type alias, or you can wrap the type
using the ``PYBIND11_TYPE`` macro:

.. code-block:: cpp

    // Version 1: using a type alias
    using ReturnType = MyReturnType<T1, T2>;
    using ClassType = Class<T3, T4>;
29
    PYBIND11_OVERRIDE(ReturnType, ClassType, func);
30
31

    // Version 2: using the PYBIND11_TYPE macro:
32
    PYBIND11_OVERRIDE(PYBIND11_TYPE(MyReturnType<T1, T2>),
33
34
35
                      PYBIND11_TYPE(Class<T3, T4>), func)

The ``PYBIND11_MAKE_OPAQUE`` macro does *not* require the above workarounds.
Dean Moldovan's avatar
Dean Moldovan committed
36

Dean Moldovan's avatar
Dean Moldovan committed
37
.. _gil:
Dean Moldovan's avatar
Dean Moldovan committed
38
39
40
41

Global Interpreter Lock (GIL)
=============================

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
The Python C API dictates that the Global Interpreter Lock (GIL) must always
be held by the current thread to safely access Python objects. As a result,
when Python calls into C++ via pybind11 the GIL must be held, and pybind11
will never implicitly release the GIL.

.. code-block:: cpp

    void my_function() {
        /* GIL is held when this function is called from Python */
    }

    PYBIND11_MODULE(example, m) {
        m.def("my_function", &my_function);
    }

pybind11 will ensure that the GIL is held when it knows that it is calling
Python code. For example, if a Python callback is passed to C++ code via
``std::function``, when C++ code calls the function the built-in wrapper
will acquire the GIL before calling the Python callback. Similarly, the
``PYBIND11_OVERRIDE`` family of macros will acquire the GIL before calling
back into Python.

When writing C++ code that is called from other C++ code, if that code accesses
Python state, it must explicitly acquire and release the GIL.

Dean Moldovan's avatar
Dean Moldovan committed
67
68
69
The classes :class:`gil_scoped_release` and :class:`gil_scoped_acquire` can be
used to acquire and release the global interpreter lock in the body of a C++
function call. In this way, long-running C++ code can be parallelized using
70
71
72
73
multiple Python threads, **but great care must be taken** when any
:class:`gil_scoped_release` appear: if there is any way that the C++ code
can access Python objects, :class:`gil_scoped_acquire` should be used to
reacquire the GIL. Taking :ref:`overriding_virtuals` as an example, this
Dean Moldovan's avatar
Dean Moldovan committed
74
75
76
could be realized as follows (important changes highlighted):

.. code-block:: cpp
77
    :emphasize-lines: 8,30,31
Dean Moldovan's avatar
Dean Moldovan committed
78
79
80
81
82
83
84
85

    class PyAnimal : public Animal {
    public:
        /* Inherit the constructors */
        using Animal::Animal;

        /* Trampoline (need one for each virtual function) */
        std::string go(int n_times) {
86
            /* PYBIND11_OVERRIDE_PURE will acquire the GIL before accessing Python state */
87
            PYBIND11_OVERRIDE_PURE(
Dean Moldovan's avatar
Dean Moldovan committed
88
89
90
91
92
93
94
95
                std::string, /* Return type */
                Animal,      /* Parent class */
                go,          /* Name of function */
                n_times      /* Argument(s) */
            );
        }
    };

96
    PYBIND11_MODULE(example, m) {
Dean Moldovan's avatar
Dean Moldovan committed
97
98
99
100
101
102
103
104
105
        py::class_<Animal, PyAnimal> animal(m, "Animal");
        animal
            .def(py::init<>())
            .def("go", &Animal::go);

        py::class_<Dog>(m, "Dog", animal)
            .def(py::init<>());

        m.def("call_go", [](Animal *animal) -> std::string {
106
107
            // GIL is held when called from Python code. Release GIL before
            // calling into (potentially long-running) C++ code
Dean Moldovan's avatar
Dean Moldovan committed
108
109
110
111
112
            py::gil_scoped_release release;
            return call_go(animal);
        });
    }

113
The ``call_go`` wrapper can also be simplified using the ``call_guard`` policy
Dean Moldovan's avatar
Dean Moldovan committed
114
115
116
117
118
119
(see :ref:`call_policies`) which yields the same result:

.. code-block:: cpp

    m.def("call_go", &call_go, py::call_guard<py::gil_scoped_release>());

Dean Moldovan's avatar
Dean Moldovan committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

Binding sequence data types, iterators, the slicing protocol, etc.
==================================================================

Please refer to the supplemental example for details.

.. seealso::

    The file :file:`tests/test_sequences_and_iterators.cpp` contains a
    complete example that shows how to bind a sequence data type, including
    length queries (``__len__``), iterators (``__iter__``), the slicing
    protocol and other kinds of useful operations.


Partitioning code over multiple extension modules
=================================================

It's straightforward to split binding code over multiple extension modules,
while referencing types that are declared elsewhere. Everything "just" works
without any special precautions. One exception to this rule occurs when
extending a type declared in another extension module. Recall the basic example
from Section :ref:`inheritance`.

.. code-block:: cpp

    py::class_<Pet> pet(m, "Pet");
    pet.def(py::init<const std::string &>())
       .def_readwrite("name", &Pet::name);

    py::class_<Dog>(m, "Dog", pet /* <- specify parent */)
        .def(py::init<const std::string &>())
        .def("bark", &Dog::bark);

Suppose now that ``Pet`` bindings are defined in a module named ``basic``,
whereas the ``Dog`` bindings are defined somewhere else. The challenge is of
course that the variable ``pet`` is not available anymore though it is needed
to indicate the inheritance relationship to the constructor of ``class_<Dog>``.
However, it can be acquired as follows:

.. code-block:: cpp

161
    py::object pet = (py::object) py::module_::import("basic").attr("Pet");
Dean Moldovan's avatar
Dean Moldovan committed
162
163
164
165
166
167
168
169
170
171
172
173
174

    py::class_<Dog>(m, "Dog", pet)
        .def(py::init<const std::string &>())
        .def("bark", &Dog::bark);

Alternatively, you can specify the base class as a template parameter option to
``class_``, which performs an automated lookup of the corresponding Python
type. Like the above code, however, this also requires invoking the ``import``
function once to ensure that the pybind11 binding code of the module ``basic``
has been executed:

.. code-block:: cpp

175
    py::module_::import("basic");
Dean Moldovan's avatar
Dean Moldovan committed
176
177
178
179
180
181
182

    py::class_<Dog, Pet>(m, "Dog")
        .def(py::init<const std::string &>())
        .def("bark", &Dog::bark);

Naturally, both methods will fail when there are cyclic dependencies.

183
184
Note that pybind11 code compiled with hidden-by-default symbol visibility (e.g.
via the command line flag ``-fvisibility=hidden`` on GCC/Clang), which is
Patrik Huber's avatar
Patrik Huber committed
185
required for proper pybind11 functionality, can interfere with the ability to
186
187
188
access types defined in another extension module.  Working around this requires
manually exporting types that are accessed by multiple extension modules;
pybind11 provides a macro to do just this:
Dean Moldovan's avatar
Dean Moldovan committed
189
190
191

.. code-block:: cpp

192
    class PYBIND11_EXPORT Dog : public Animal {
Dean Moldovan's avatar
Dean Moldovan committed
193
194
195
        ...
    };

Ivan Smirnov's avatar
Ivan Smirnov committed
196
197
198
199
200
201
202
203
204
Note also that it is possible (although would rarely be required) to share arbitrary
C++ objects between extension modules at runtime. Internal library data is shared
between modules using capsule machinery [#f6]_ which can be also utilized for
storing, modifying and accessing user-defined data. Note that an extension module
will "see" other extensions' data if and only if they were built with the same
pybind11 version. Consider the following example:

.. code-block:: cpp

205
    auto data = reinterpret_cast<MyData *>(py::get_shared_data("mydata"));
Ivan Smirnov's avatar
Ivan Smirnov committed
206
    if (!data)
207
        data = static_cast<MyData *>(py::set_shared_data("mydata", new MyData(42)));
Ivan Smirnov's avatar
Ivan Smirnov committed
208
209
210
211
212
213
214

If the above snippet was used in several separately compiled extension modules,
the first one to be imported would create a ``MyData`` instance and associate
a ``"mydata"`` key with a pointer to it. Extensions that are imported later
would be then able to access the data behind the same pointer.

.. [#f6] https://docs.python.org/3/extending/extending.html#using-capsules
Dean Moldovan's avatar
Dean Moldovan committed
215

216
217
218
219
220
Module Destructors
==================

pybind11 does not provide an explicit mechanism to invoke cleanup code at
module destruction time. In rare cases where such functionality is required, it
221
222
is possible to emulate it using Python capsules or weak references with a
destruction callback.
223
224
225
226
227
228
229
230

.. code-block:: cpp

    auto cleanup_callback = []() {
        // perform cleanup here -- this function is called with the GIL held
    };

    m.add_object("_cleanup", py::capsule(cleanup_callback));
Dean Moldovan's avatar
Dean Moldovan committed
231

232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
This approach has the potential downside that instances of classes exposed
within the module may still be alive when the cleanup callback is invoked
(whether this is acceptable will generally depend on the application).

Alternatively, the capsule may also be stashed within a type object, which
ensures that it not called before all instances of that type have been
collected:

.. code-block:: cpp

    auto cleanup_callback = []() { /* ... */ };
    m.attr("BaseClass").attr("_cleanup") = py::capsule(cleanup_callback);

Both approaches also expose a potentially dangerous ``_cleanup`` attribute in
Python, which may be undesirable from an API standpoint (a premature explicit
247
call from Python might lead to undefined behavior). Yet another approach that
248
249
250
251
avoids this issue involves weak reference with a cleanup callback:

.. code-block:: cpp

252
    // Register a callback function that is invoked when the BaseClass object is collected
253
254
255
256
257
258
259
260
261
262
263
    py::cpp_function cleanup_callback(
        [](py::handle weakref) {
            // perform cleanup here -- this function is called with the GIL held

            weakref.dec_ref(); // release weak reference
        }
    );

    // Create a weak reference with a cleanup callback and initially leak it
    (void) py::weakref(m.attr("BaseClass"), cleanup_callback).release();

264
265
.. note::

266
267
268
    PyPy does not garbage collect objects when the interpreter exits. An alternative
    approach (which also works on CPython) is to use the :py:mod:`atexit` module [#f7]_,
    for example:
269
270
271

    .. code-block:: cpp

272
        auto atexit = py::module_::import("atexit");
273
274
275
276
277
278
        atexit.attr("register")(py::cpp_function([]() {
            // perform cleanup here -- this function is called with the GIL held
        }));

    .. [#f7] https://docs.python.org/3/library/atexit.html

279

Dean Moldovan's avatar
Dean Moldovan committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
Generating documentation using Sphinx
=====================================

Sphinx [#f4]_ has the ability to inspect the signatures and documentation
strings in pybind11-based extension modules to automatically generate beautiful
documentation in a variety formats. The python_example repository [#f5]_ contains a
simple example repository which uses this approach.

There are two potential gotchas when using this approach: first, make sure that
the resulting strings do not contain any :kbd:`TAB` characters, which break the
docstring parsing routines. You may want to use C++11 raw string literals,
which are convenient for multi-line comments. Conveniently, any excess
indentation will be automatically be removed by Sphinx. However, for this to
work, it is important that all lines are indented consistently, i.e.:

.. code-block:: cpp

    // ok
    m.def("foo", &foo, R"mydelimiter(
        The foo function

        Parameters
        ----------
    )mydelimiter");

    // *not ok*
    m.def("foo", &foo, R"mydelimiter(The foo function

        Parameters
        ----------
    )mydelimiter");

312
By default, pybind11 automatically generates and prepends a signature to the docstring of a function
313
registered with ``module_::def()`` and ``class_::def()``. Sometimes this
314
behavior is not desirable, because you want to provide your own signature or remove
315
316
317
318
319
the docstring completely to exclude the function from the Sphinx documentation.
The class ``options`` allows you to selectively suppress auto-generated signatures:

.. code-block:: cpp

320
    PYBIND11_MODULE(example, m) {
321
322
        py::options options;
        options.disable_function_signatures();
323

324
325
326
        m.def("add", [](int a, int b) { return a + b; }, "A function which adds two numbers");
    }

327
328
329
330
331
332
333
334
335
pybind11 also appends all members of an enum to the resulting enum docstring.
This default behavior can be disabled by using the ``disable_enum_members_docstring()``
function of the ``options`` class.

With ``disable_user_defined_docstrings()`` all user defined docstrings of
``module_::def()``, ``class_::def()`` and ``enum_()`` are disabled, but the
function signatures and enum members are included in the docstring, unless they
are disabled separately.

336
337
Note that changes to the settings affect only function bindings created during the
lifetime of the ``options`` instance. When it goes out of scope at the end of the module's init function,
338
339
the default settings are restored to prevent unwanted side effects.

Dean Moldovan's avatar
Dean Moldovan committed
340
341
.. [#f4] http://www.sphinx-doc.org
.. [#f5] http://github.com/pybind/python_example
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372

.. _avoiding-cpp-types-in-docstrings:

Avoiding C++ types in docstrings
================================

Docstrings are generated at the time of the declaration, e.g. when ``.def(...)`` is called.
At this point parameter and return types should be known to pybind11.
If a custom type is not exposed yet through a ``py::class_`` constructor or a custom type caster,
its C++ type name will be used instead to generate the signature in the docstring:

.. code-block:: text

     |  __init__(...)
     |      __init__(self: example.Foo, arg0: ns::Bar) -> None
                                              ^^^^^^^


This limitation can be circumvented by ensuring that C++ classes are registered with pybind11
before they are used as a parameter or return type of a function:

.. code-block:: cpp

    PYBIND11_MODULE(example, m) {

        auto pyFoo = py::class_<ns::Foo>(m, "Foo");
        auto pyBar = py::class_<ns::Bar>(m, "Bar");

        pyFoo.def(py::init<const ns::Bar&>());
        pyBar.def(py::init<const ns::Foo&>());
    }