Capture mjs_attach error message in the Python bindings.

PiperOrigin-RevId: 682409212
Change-Id: I391ee48626e7dcd985f65a231370ef5ec16124d7
This commit is contained in:
Alessio Quaglino
2024-10-04 12:18:36 -07:00
committed by Copybara-Service
parent 3091665c16
commit 6e66cafe76
8 changed files with 68 additions and 3 deletions
+13 -2
View File
@@ -563,7 +563,12 @@ PYBIND11_MODULE(_specs, m) {
"attach_frame",
[](raw::MjsBody& self, raw::MjsFrame& frame, std::string& prefix,
std::string& suffix) -> raw::MjsFrame* {
return mjs_attachFrame(&self, &frame, prefix.c_str(), suffix.c_str());
auto new_frame =
mjs_attachFrame(&self, &frame, prefix.c_str(), suffix.c_str());
if (!new_frame) {
throw pybind11::value_error(mjs_getError(mjs_getSpec(&self)));
}
return new_frame;
},
py::return_value_policy::reference_internal);
@@ -578,7 +583,13 @@ PYBIND11_MODULE(_specs, m) {
"attach_body",
[](raw::MjsFrame& self, raw::MjsBody& body, std::string& prefix,
std::string& suffix) -> raw::MjsBody* {
return mjs_attachBody(&self, &body, prefix.c_str(), suffix.c_str());
auto new_body =
mjs_attachBody(&self, &body, prefix.c_str(), suffix.c_str());
if (!new_body) {
throw pybind11::value_error(
mjs_getError(mjs_getSpecFromFrame(&self)));
}
return new_body;
},
py::return_value_policy::reference_internal);
+15
View File
@@ -846,5 +846,20 @@ class SpecsTest(absltest.TestCase):
with self.assertRaises(IndexError):
material.textures[-1] = 'x'
def test_attach_error(self):
child = mujoco.MjSpec()
parent = mujoco.MjSpec()
parent.degree = not child.degree
body = parent.worldbody.add_body()
frame = child.worldbody.add_frame()
with self.assertRaises(ValueError) as cm:
body.attach_frame(frame, '', '')
self.assertEqual(
str(cm.exception),
'Error: cannot attach mjSpecs with incompatible compiler/angle'
' attribute',
)
if __name__ == '__main__':
absltest.main()