Skip to content

Commit c053462

Browse files
committed
added documentation for module destructors (fixes #752)
1 parent 0c99402 commit c053462

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

docs/advanced/misc.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,20 @@ would be then able to access the data behind the same pointer.
170170

171171
.. [#f6] https://docs.python.org/3/extending/extending.html#using-capsules
172172
173+
Module Destructors
174+
==================
175+
176+
pybind11 does not provide an explicit mechanism to invoke cleanup code at
177+
module destruction time. In rare cases where such functionality is required, it
178+
is 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));
173187
174188
Generating documentation using Sphinx
175189
=====================================

0 commit comments

Comments
 (0)