-
Notifications
You must be signed in to change notification settings - Fork 2.2k
type_caster_generic: add set_foreign_holder method for subclasses to implement #5862
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
Show all changes
8 commits
Select commit
Hold shift + click to select a range
17a1ff9
type_caster_generic: add set_foreign_holder method for subclasses to …
oremanj f971060
style: pre-commit fixes
pre-commit-ci[bot] fa66511
Rename try_shared_from_this -> set_via_shared_from_this to avoid conf…
oremanj 8c760c4
Add comment explaining the limits of the test
oremanj 524d54d
CI
oremanj 68c8f3a
style: pre-commit fixes
pre-commit-ci[bot] 1a90708
Fixes from code review
oremanj 1ba6d64
style: pre-commit fixes
pre-commit-ci[bot] 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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| pybind11/detail/holder_caster_foreign_helpers.h: Logic to implement | ||
| set_foreign_holder() in copyable_ and movable_holder_caster. | ||
|
|
||
| Copyright (c) 2025 Hudson River Trading LLC <opensource@hudson-trading.com> | ||
|
|
||
| All rights reserved. Use of this source code is governed by a | ||
| BSD-style license that can be found in the LICENSE file. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <pybind11/gil.h> | ||
|
|
||
| #include "common.h" | ||
|
|
||
| PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) | ||
| PYBIND11_NAMESPACE_BEGIN(detail) | ||
|
|
||
| struct holder_caster_foreign_helpers { | ||
| struct py_deleter { | ||
| void operator()(const void *) const noexcept { | ||
| // Don't run the deleter if the interpreter has been shut down | ||
| if (Py_IsInitialized() == 0) { | ||
| return; | ||
| } | ||
| gil_scoped_acquire guard; | ||
| Py_DECREF(o); | ||
| } | ||
|
|
||
| PyObject *o; | ||
| }; | ||
|
|
||
| template <typename type> | ||
| static auto set_via_shared_from_this(type *value, std::shared_ptr<type> *holder_out) | ||
| -> decltype(value->shared_from_this(), bool()) { | ||
| // object derives from enable_shared_from_this; | ||
| // try to reuse an existing shared_ptr if one is known | ||
| if (auto existing = try_get_shared_from_this(value)) { | ||
| *holder_out = std::static_pointer_cast<type>(existing); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| template <typename type> | ||
| static bool set_via_shared_from_this(void *, std::shared_ptr<type> *) { | ||
| return false; | ||
| } | ||
|
|
||
| template <typename type> | ||
| static bool set_foreign_holder(handle src, type *value, std::shared_ptr<type> *holder_out) { | ||
| // We only support using std::shared_ptr<T> for foreign T, and | ||
| // it's done by creating a new shared_ptr control block that | ||
| // owns a reference to the original Python object. | ||
| if (value == nullptr) { | ||
| *holder_out = {}; | ||
| return true; | ||
| } | ||
| if (set_via_shared_from_this(value, holder_out)) { | ||
| return true; | ||
| } | ||
| *holder_out = std::shared_ptr<type>(value, py_deleter{src.inc_ref().ptr()}); | ||
| return true; | ||
| } | ||
|
|
||
| template <typename type> | ||
| static bool | ||
| set_foreign_holder(handle src, const type *value, std::shared_ptr<const type> *holder_out) { | ||
| std::shared_ptr<type> holder_mut; | ||
| if (set_foreign_holder(src, const_cast<type *>(value), &holder_mut)) { | ||
| *holder_out = holder_mut; | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| template <typename type> | ||
| static bool set_foreign_holder(handle, type *, ...) { | ||
| throw cast_error("Unable to cast foreign type to held instance -- " | ||
| "only std::shared_ptr<T> is supported in this case"); | ||
| } | ||
| }; | ||
|
|
||
| PYBIND11_NAMESPACE_END(detail) | ||
| PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) |
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
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
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
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
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
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
Oops, something went wrong.
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.
@msimacek I just looked:
Do you think we could enable more test coverage for GraalPy by adopting Joshua's approach here more widely?
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.
The problem of gc.collect() not reliably GCing things is broader than the problem of not having refcounts. PyPy doesn't have refcounts but its gc.collect() works (if you call it a few times).