Skip to content

Commit

Permalink
Add error handling to reflection_table.from_msgpack.
Browse files Browse the repository at this point in the history
  • Loading branch information
toastisme committed Oct 10, 2024
1 parent 7df425c commit 45423c1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions newsfragments/XXX.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add error handling to `reflection_table.from_msgpack`.
28 changes: 23 additions & 5 deletions src/dials/array_family/boost_python/flex_reflection_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -785,13 +785,31 @@ namespace dials { namespace af { namespace boost_python {
* @returns The reflection table
*/
reflection_table reflection_table_from_msgpack(boost::python::object packed) {
if (!PyBytes_Check(packed.ptr())) {
PyErr_SetString(PyExc_TypeError, "Input is not a valid bytes-like object");
boost::python::throw_error_already_set();
}

const char *data = PyBytes_AsString(packed.ptr());
std::size_t size = PyBytes_Size(packed.ptr());
msgpack::unpacked result;
std::size_t off = 0;
msgpack::unpack(result, data, size, off, reflection_table_reference_func);
reflection_table r = result.get().as<reflection_table>();
return r;

if (data == NULL || size == 0) {
PyErr_SetString(PyExc_ValueError, "Input bytes are invalid or empty");
boost::python::throw_error_already_set();
}

try {
msgpack::unpacked result;
std::size_t off = 0;
msgpack::unpack(result, data, size, off, reflection_table_reference_func);

reflection_table r = result.get().as<reflection_table>();
return r;
} catch (const std::exception &e) {
PyErr_SetString(PyExc_RuntimeError, e.what());
boost::python::throw_error_already_set();
}
return reflection_table();
}

/*
Expand Down

0 comments on commit 45423c1

Please sign in to comment.