-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Proposed wording for extended module destructor documentation #1031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,7 +173,8 @@ 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 | ||
is possible to emulate it using Python capsules with a destruction callback. | ||
is possible to emulate it using Python capsules or weak references with a | ||
destruction callback. | ||
|
||
.. code-block:: cpp | ||
|
||
|
@@ -183,6 +184,39 @@ is possible to emulate it using Python capsules with a destruction callback. | |
|
||
m.add_object("_cleanup", py::capsule(cleanup_callback)); | ||
|
||
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 | ||
call from Python might lead to undefined behavior). Yet another approach that | ||
avoids this issue involves weak reference with a cleanup callback: | ||
|
||
.. code-block:: cpp | ||
|
||
// Register a callback function that is invoked when the BaseClass object is colelcted | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. colelcted -> collected |
||
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(); | ||
|
||
|
||
Generating documentation using Sphinx | ||
===================================== | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it not -> it is not