-
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
WIP Add arg().disown() (superseding the consume call_policy) #1226
Conversation
FWIW, even in its current wonky form, this has proven to be "working for me". At least for now. |
I'd say yes, this PR is still quite relevant (and I'm actually still looking to have it land in some form, as it in it's own state has been useful to us :) ). It looks like you're providing a much more generic mechanism to transfer ownership, which is awesome!
In (a slightly more refined version of) my WIP branch from #1146, I type-erase mechanisms that deal with holders, store those in I believe that ultimately, you'd need this sort of mechanism to properly mix holder types (which my implementation misses, since I don't store Feel free to steal bits from my PR if you're able to understand them and find them useful. And naiive question: How does this pass compilation with |
auto value_and_holder = values_and_holders(inst).begin(); | ||
|
||
|
||
using holder_type = std::unique_ptr<void*>; // how do I get the proper one? |
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.
@torokati44 After seeing your stuff, and reworking my PR, I believe this function should be what you need for type-erased holder transfer:
https://github.com/EricCousineau-TRI/pybind11/blob/379afa73c7fedded57e4fedc9c3b077cb7292643/include/pybind11/cast.h#L482
You could use it to something like the tune of:
if (!reclaim_existing_if_needed(inst, tinfo, existing_holder)) {
throw cast_error("Cannot release ownership of this object"); // If you have an incompatible holder type, like `shared_ptr`.
}
The caveat is, you'll need to figure out what the holder type is for the given argument. I believe that should be elsewhere in this area of code.
(Also, ensure that you get tinfo
from the argument type and NOT the holder, as holder->type
may be null!)
I was actually asking if my PRs were relevant, seeing that yours' are progressing. :) |
Hup, sorry about that! (Was in technical mode) That being said, this is based on my usage of it, which looks like this: Can I ask if you have a code snippet or open-source project that you could point to, using your code? |
I'm trying to create Python bindings for the OMNeT++ API. (I don't think I can share it just yet.) It has ownership transfer of objects between the simulation kernel and the user code, in both directions, passed as raw pointer function arguments. If As the API above is pretty much set in stone, I can't just switch to Ownership transfer (or the lack thereof) via return value (again, as raw pointers) also only works one way (AFAIK; C++ -> Python), but one issue at a time... :) |
Ah, I see. In that case, it sounds like you'd be interested @jagerman's proposal for adding a |
I have been able to achieve (almost) what I wanted with current vanilla I'm sure it violates a bunch of programming principles, and the names could be better, etc., but it gets the job done (somehow). At least until a better solution (either a call policy, an The only inconvenience is that wherever this kind of ownership transfer happens with a function argument or return value, I have to bind a tiny wrapper lambda instead of the real function, that calls I had silently hoped that someone with more experience with this kind of template-heavy code will finish this instead of me, or provide a better alternative, but so far this hasn't happened. Oh well. I don't think I'll ever finish this, rather just go ahead with my custom holder. Thanks for the help anyway, @EricCousineau-TRI! |
I finally found some time to work a little bit on #971. This PR supersedes it. (Should I close it?) I reworked it, taking some of @jagerman's advice. However, it is still not nearly complete.
py::arg
, instead of as a call policy.holder_helper
static method.owned
tovalue_and_holder
is not yet done.holder_helper::release
does not give a meaningful error when there is noholder_type::release()
holder_type
is still assumed to bestd::unique_ptr<void*>
For the last one: I have no idea how to get the proper
holder_type
for the args. I tried to look in the direction ofclass_<arg>::holder_type
, but that would have to be forward declared, and I'm no even sure what to supply as thetype
template parameter.Is this in the right place in
dispatcher()
?Also, I saw that #1146 referenced #971, is this PR still relevant despite that one?
Any comments are still much appreciated.