-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Support ownership transfer for unique_ptr<T>
(move-only holders) from Python to C++ (and back)
#1132
Comments
Worked out an example for containers. That, on top of the oddity with weak references, I'm thinking of taking a MATLAB-style approach: potentially keeping track of the C++ object lifetime, and flip a switch if the object is destroyed, and check this switch in EDIT: Forgot this won't apply well for pure C++ objects, as there is no signaling mechanism. Options are to just let the segfault happen (may be fine by me) or to keep the existing design. I'm not liking this existing design for the above mentioned pain points. |
I had reviewed some of the prior questions on GitHub, StackOverflow, etc, and found that accepting a
unique_ptr<T>
from Python is not presently possible (ref: one of the GitHub issues).I think that this can be done, and have made a (rough, but not too rough) prototype of doing so that I believe is generally comprehensive for most of the simple edge cases.
branch | head commit (as of writing)
Demo Usage
Code sampled from this pseudo-test case.
Should print something like:
Functionality Overview
move_only_holder_caster
toload
values. If a Python value is loaded into aunique_ptr<T>
, the object is "released" to C++ (see here):pybind11::move<>
).pybind11
can yank the instance out without disturbing the arguments (possible implementation).pybind11::move<>()
for it to work (see commented-out code).type_caster_generic::load_impl<>()
), use the type's "release" mechanism):pybind
instance into a non-owned instance, and let it destruct once its reference count depletes at the end of the caster.pybind11::trampoline
in the prototype).pybind11::object
reference to it in the trampoline wrapper.type_caster_generic::cast()
could have a new clause:Caveats / Limitations
trampoline
wrapper affects destructor order if the object is destroyed in C++. See note here with a workaround.keep_alive
to the weak reference (and this tie can be broken if ownership is transferred back).weak_ref
workflows.default_holder
case. Unless someone actually has their own custom move-only holder, I'd prefer to keep it this way for simplicity's sake.internals
andinstance
. These could be reduced some, if need be, but there'd need to be at least one field.Followup
I would really like to have this feature, so please do let me know if this seems like viable feature option. I am more than happy to submit a PR and go through the review process if so desired (which would be a much more polished, properly unit-tested version of the prototype branch).
\cc @jagerman
The text was updated successfully, but these errors were encountered: