misc.rst (11986:c12e4625ab56) misc.rst (12037:d28054ac6ec9)
1Miscellaneous
2#############
3
4.. _macro_notes:
5
6General notes regarding convenience macros
7==========================================
8

--- 5 unchanged lines hidden (view full) ---

14T2>, myFunc)``. In this case, the preprocessor assumes that the comma indicates
15the beginning of the next parameter. Use a ``typedef`` to bind the template to
16another name and use it in the macro to avoid this problem.
17
18
19Global Interpreter Lock (GIL)
20=============================
21
1Miscellaneous
2#############
3
4.. _macro_notes:
5
6General notes regarding convenience macros
7==========================================
8

--- 5 unchanged lines hidden (view full) ---

14T2>, myFunc)``. In this case, the preprocessor assumes that the comma indicates
15the beginning of the next parameter. Use a ``typedef`` to bind the template to
16another name and use it in the macro to avoid this problem.
17
18
19Global Interpreter Lock (GIL)
20=============================
21
22When calling a C++ function from Python, the GIL is always held.
22The classes :class:`gil_scoped_release` and :class:`gil_scoped_acquire` can be
23used to acquire and release the global interpreter lock in the body of a C++
24function call. In this way, long-running C++ code can be parallelized using
25multiple Python threads. Taking :ref:`overriding_virtuals` as an example, this
26could be realized as follows (important changes highlighted):
27
28.. code-block:: cpp
29 :emphasize-lines: 8,9,33,34

--- 134 unchanged lines hidden (view full) ---

164
165If the above snippet was used in several separately compiled extension modules,
166the first one to be imported would create a ``MyData`` instance and associate
167a ``"mydata"`` key with a pointer to it. Extensions that are imported later
168would be then able to access the data behind the same pointer.
169
170.. [#f6] https://docs.python.org/3/extending/extending.html#using-capsules
171
23The classes :class:`gil_scoped_release` and :class:`gil_scoped_acquire` can be
24used to acquire and release the global interpreter lock in the body of a C++
25function call. In this way, long-running C++ code can be parallelized using
26multiple Python threads. Taking :ref:`overriding_virtuals` as an example, this
27could be realized as follows (important changes highlighted):
28
29.. code-block:: cpp
30 :emphasize-lines: 8,9,33,34

--- 134 unchanged lines hidden (view full) ---

165
166If the above snippet was used in several separately compiled extension modules,
167the first one to be imported would create a ``MyData`` instance and associate
168a ``"mydata"`` key with a pointer to it. Extensions that are imported later
169would be then able to access the data behind the same pointer.
170
171.. [#f6] https://docs.python.org/3/extending/extending.html#using-capsules
172
173Module Destructors
174==================
172
175
176pybind11 does not provide an explicit mechanism to invoke cleanup code at
177module destruction time. In rare cases where such functionality is required, it
178is possible to emulate it using Python capsules with a destruction callback.
179
180.. code-block:: cpp
181
182 auto cleanup_callback = []() {
183 // perform cleanup here -- this function is called with the GIL held
184 };
185
186 m.add_object("_cleanup", py::capsule(cleanup_callback));
187
173Generating documentation using Sphinx
174=====================================
175
176Sphinx [#f4]_ has the ability to inspect the signatures and documentation
177strings in pybind11-based extension modules to automatically generate beautiful
178documentation in a variety formats. The python_example repository [#f5]_ contains a
179simple example repository which uses this approach.
180

--- 49 unchanged lines hidden ---
188Generating documentation using Sphinx
189=====================================
190
191Sphinx [#f4]_ has the ability to inspect the signatures and documentation
192strings in pybind11-based extension modules to automatically generate beautiful
193documentation in a variety formats. The python_example repository [#f5]_ contains a
194simple example repository which uses this approach.
195

--- 49 unchanged lines hidden ---