Skip to content
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

pybind11 fork: Ensure failed overload with unique_ptr returns ownership #8160

Closed
EricCousineau-TRI opened this issue Feb 23, 2018 · 0 comments
Assignees

Comments

@EricCousineau-TRI
Copy link
Contributor

@apojomovsky ran into a situation where a set of ovlerloads look like something to the effect of:

py::class_<Container>(m, "Container")
  .def(py::init<unique_ptr<A>, FirstT>())
  .def(py::init<unique_ptr<A>, SecondT>());

When Python calls something like:

c = Container(A(), SecondT())

this will most likely failure with the following error:

RuntimeError: C++ object must be owned by pybind11 when attempting to release to C++

This most likely happens since pybind11::detail::argument_loader does not have a "transaction" mechanism of sort; the cast of unique_ptr<A> succeeds when it tries to match the first overload, but when it is unable to load the second argument, it will just exit out, and not try to reverse the ownership transfer:
https://github.com/RobotLocomotion/pybind11/blob/060f8eb/include/pybind11/cast.h#L2138

The solution is to introduce some sort of "transaction" mechanism.

From what it looks like, I detect ownership transfer from the move_only_holder_caster; if the caster is destructed without having transferred the object, then it can reverse the ownership transfer (simply calling cast to release ownership back to Python).

@EricCousineau-TRI EricCousineau-TRI self-assigned this Feb 23, 2018
EricCousineau-TRI added a commit to EricCousineau-TRI/pybind11 that referenced this issue Feb 23, 2018
EricCousineau-TRI added a commit to EricCousineau-TRI/pybind11 that referenced this issue Feb 23, 2018
EricCousineau-TRI added a commit to EricCousineau-TRI/pybind11 that referenced this issue Feb 23, 2018
EricCousineau-TRI added a commit to EricCousineau-TRI/pybind11 that referenced this issue Feb 23, 2018
EricCousineau-TRI added a commit to EricCousineau-TRI/pybind11 that referenced this issue Feb 23, 2018
EricCousineau-TRI added a commit to EricCousineau-TRI/pybind11 that referenced this issue Feb 23, 2018
EricCousineau-TRI added a commit to RobotLocomotion/pybind11 that referenced this issue Feb 26, 2018
unique_ptr: Fix ownership issues for failed overloads (RobotLocomotion/drake#8160)
EricCousineau-TRI added a commit to EricCousineau-TRI/pybind11 that referenced this issue Mar 22, 2018
EricCousineau-TRI added a commit to EricCousineau-TRI/pybind11 that referenced this issue Mar 22, 2018
EricCousineau-TRI added a commit to EricCousineau-TRI/pybind11 that referenced this issue Apr 23, 2018
EricCousineau-TRI added a commit to EricCousineau-TRI/pybind11 that referenced this issue Apr 23, 2018
EricCousineau-TRI added a commit to EricCousineau-TRI/pybind11 that referenced this issue May 23, 2018
EricCousineau-TRI added a commit to EricCousineau-TRI/pybind11 that referenced this issue May 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant