Fix return policies for some member functions#422
Conversation
Member functions that return a const ref should either use the default return policy (which makes a copy) or `py::return_value_policy::reference_internal`. Since we're dealing with small objects, making a copy seems the simplest and safest approach. For member functions that return a mutable reference, should use almost always use `py::return_value_policy::reference_internal`. Signed-off-by: Addisu Z. Taddese <addisu@openrobotics.org>
Codecov Report
@@ Coverage Diff @@
## ign-math6 #422 +/- ##
==========================================
Coverage 99.83% 99.83%
==========================================
Files 41 41
Lines 4138 4138
==========================================
Hits 4131 4131
Misses 7 7 Continue to review full report at Codecov.
|
|
are there tests we are missing that would confirm the intended behavior? we don't need to add it now, but I'm wondering what those tests would look like or if some of the classes already have them |
I don't know how to test this behavior. It has to do with object lifetime and the python garbage collector, so I'm not sure if there's an easy way to test it. |
ok, I don't fully understand the issue so I won't approve this, but I think it's fine to merge since it has one approval |
I can demonstrate the issue from sdformat import sdformat as sdf
from ignition.math import Inertiald, MassMatrix3d, Vector3d, Pose3d
link = sdf.Link()
mass = 10.0
inertial = Inertiald(MassMatrix3d(mass, Vector3d(2, 3, 4), Vector3d.ZERO),
Pose3d())
link.set_inertial(inertial)
print("mass from inertial:", inertial.mass_matrix().mass())
print("mass from link:", link.inertial().mass_matrix().mass())will print but we expect both to be 10. This PR fixes that. Edit: You have forward port this to |
Signed-off-by: Steve Peters <scpeters@openrobotics.org>
|
ok, I think I see what's happening. I still think it would be best to have tests that confirm what we think the API should be doing. Here is a branch where I've added some test expectations to https://github.com/gazebosim/gz-math/compare/scpeters/python_inertial_tests?expand=1
this more closely matches the c++ API; hopefully it is pythonic enough |
1 similar comment
|
ok, I think I see what's happening. I still think it would be best to have tests that confirm what we think the API should be doing. Here is a branch where I've added some test expectations to https://github.com/gazebosim/gz-math/compare/scpeters/python_inertial_tests?expand=1
this more closely matches the c++ API; hopefully it is pythonic enough |
|
The C++ member function |
🦟 Bug fix
Summary
We encountered some issues with the python bindings while working on gazebosim/gz-mujoco#5 where calling
link.inertia().mass_matrix()results in a newly constructedMassMatrix3dobject instead of returning the object inside the link'sInertialdobject.To fix this, generally, member functions that return a const ref should either use the default return policy (which makes a copy) or
py::return_value_policy::reference_internal. Since we're dealing with small objects, making a copy seems the simplest and safest approach.For member functions that return a mutable reference, we should use almost always use
py::return_value_policy::reference_internal. In such instances, if there are const and non-const overloads, the non-const overload should be picked for binding to python.Checklist
codecheckpassed (See contributing)Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining
Signed-off-bymessages.