1818#include < string_view>
1919#include < vector>
2020
21+ #include < pybind11/pybind11.h>
22+ #include < pybind11_abseil/status_caster.h>
23+ #include < pybind11_abseil/statusor_caster.h>
24+
2125#include " eglt/data/eg_structs.h"
2226#include " eglt/data/serialization.h"
2327#include " eglt/pybind11_headers.h"
28+ #include " eglt/util/status_macros.h"
2429#include " eglt/util/utils_pybind11.h"
2530
2631namespace eglt ::pybindings {
@@ -187,20 +192,24 @@ void BindSerializerRegistry(py::handle scope, std::string_view name) {
187192 py::class_<SerializerRegistry, std::shared_ptr<SerializerRegistry>>(
188193 scope, std::string (name).c_str ());
189194 registry.def (MakeSameObjectRefConstructor<SerializerRegistry>());
195+
190196 registry.def (
191197 py::init ([]() { return std::make_shared<SerializerRegistry>(); }));
198+
192199 registry.def_property_readonly (
193200 " _mimetype_to_type" , [](const std::shared_ptr<SerializerRegistry>& self) {
194201 return GetMimetypeToTypeDict (self.get ());
195202 });
203+
196204 registry.def_property_readonly (
197205 " _type_to_mimetype" , [](const std::shared_ptr<SerializerRegistry>& self) {
198206 return GetTypeToMimetypeDict (self.get ());
199207 });
208+
200209 registry.def (
201210 " serialize" ,
202211 [](const std::shared_ptr<SerializerRegistry>& self, py::handle value,
203- std::string_view mimetype) -> py::bytes {
212+ std::string_view mimetype) -> absl::StatusOr< py::bytes> {
204213 auto mimetype_str = std::string (mimetype);
205214 if (mimetype_str.empty ()) {
206215 auto type_to_mimetype = GetTypeToMimetypeDict (self.get ());
@@ -218,38 +227,28 @@ void BindSerializerRegistry(py::handle scope, std::string_view name) {
218227 py::gil_scoped_release release_gil;
219228 serialized = self->Serialize (value, mimetype_str);
220229 if (!serialized.ok ()) {
221- auto cpp_any = CastPyObjectToAny (std::move (value), mimetype_str);
222- if (!cpp_any.ok ()) {
223- throw py::value_error (
224- absl::StrCat (" Failed to convert value to std::any: " ,
225- cpp_any.status ().message ()));
226- }
227-
228- serialized = self->Serialize (*std::move (cpp_any), mimetype_str);
230+ ASSIGN_OR_RETURN (std::any cpp_any,
231+ CastPyObjectToAny (std::move (value), mimetype_str));
232+ serialized = self->Serialize (std::move (cpp_any), mimetype_str);
229233 }
230234 }
231- if (!serialized.ok ()) {
232- throw py::value_error (absl::StrCat (" Failed to serialize value: " ,
233- serialized.status ().message ()));
234- }
235+ RETURN_IF_ERROR (serialized.status ());
235236 return py::bytes (std::move (*serialized));
236237 },
237238 py::arg (" value" ), py::arg_v (" mimetype" , " " ));
239+
238240 registry.def (
239241 " deserialize" ,
240242 [](const std::shared_ptr<SerializerRegistry>& self, py::bytes data,
241- std::string_view mimetype) -> py::object {
243+ std::string_view mimetype) -> absl::StatusOr< py::object> {
242244 absl::StatusOr<std::any> deserialized;
243245 {
244246 py::gil_scoped_release release_gil;
245247 deserialized = self->Deserialize (std::move (data), mimetype);
246248 }
247- if (!deserialized.ok ()) {
248- throw py::value_error (absl::StrCat (" Failed to deserialize data: " ,
249- deserialized.status ().message ()));
250- }
249+ RETURN_IF_ERROR (deserialized.status ());
251250 if (std::any_cast<py::object>(&*deserialized) == nullptr ) {
252- throw py::type_error (
251+ return absl::InvalidArgumentError (
253252 absl::StrCat (" Deserialized object is not a py::object, but a " ,
254253 deserialized->type ().name (),
255254 " . Cannot convert to py::object because it's not "
@@ -259,14 +258,15 @@ void BindSerializerRegistry(py::handle scope, std::string_view name) {
259258 return std::any_cast<py::object>(*std::move (deserialized));
260259 },
261260 py::arg (" data" ), py::arg_v (" mimetype" , " " ));
261+
262262 registry.def (
263263 " register_serializer" ,
264264 [](const std::shared_ptr<SerializerRegistry>& self,
265265 std::string_view mimetype, const py::function& serializer,
266266 const py::object& obj_type = py::none ()) {
267267 if (!obj_type.is_none ()) {
268268 if (!py::isinstance<py::type>(obj_type)) {
269- throw py::type_error (
269+ return absl::InvalidArgumentError (
270270 " obj_type must be a type, not an instance or other object." );
271271 }
272272 // Register the mimetype with the type.
@@ -276,6 +276,7 @@ void BindSerializerRegistry(py::handle scope, std::string_view name) {
276276 }
277277 self->RegisterSerializer (std::string (mimetype),
278278 PySerializerToCppSerializer (serializer));
279+ return absl::OkStatus ();
279280 },
280281 py::arg (" mimetype" ), py::arg (" serializer" ),
281282 py::arg_v (" obj_type" , py::none ()), py::keep_alive<1 , 3 >());
@@ -286,9 +287,8 @@ void BindSerializerRegistry(py::handle scope, std::string_view name) {
286287 const py::object& obj_type = py::none ()) {
287288 if (!obj_type.is_none ()) {
288289 if (!py::isinstance<py::type>(obj_type)) {
289- throw py::type_error (
290- " obj_type must be a type, not an instance or other "
291- " object." );
290+ return absl::InvalidArgumentError (
291+ " obj_type must be a type, not an instance or other object." );
292292 }
293293 // Register the mimetype with the type.
294294 auto mimetype_str = std::string (mimetype);
@@ -298,6 +298,7 @@ void BindSerializerRegistry(py::handle scope, std::string_view name) {
298298 self->RegisterDeserializer (
299299 std::string (mimetype),
300300 PyDeserializerToCppDeserializer (deserializer));
301+ return absl::OkStatus ();
301302 },
302303 py::keep_alive<1 , 3 >());
303304 registry.def (" __del__" , [](const std::shared_ptr<SerializerRegistry>& self) {
@@ -326,25 +327,27 @@ py::module_ MakeDataModule(py::module_ scope, std::string_view module_name) {
326327 data.def (
327328 " to_bytes" ,
328329 [](py::handle obj, std::string_view mimetype = " " ,
329- SerializerRegistry* registry = nullptr ) -> py::bytes {
330- return pybindings::PyToChunk (std::move (obj), mimetype, registry).data ;
330+ SerializerRegistry* registry = nullptr ) -> absl::StatusOr<py::bytes> {
331+ ASSIGN_OR_RETURN (Chunk chunk, pybindings::PyToChunk (
332+ std::move (obj), mimetype, registry));
333+ return std::move (chunk.data );
331334 },
332335 py::arg (" obj" ), py::arg_v (" mimetype" , " " ),
333336 py::arg_v (" registry" , nullptr ));
334337
335338 data.def (
336339 " to_chunk" ,
337340 [](py::handle obj, std::string_view mimetype = " " ,
338- SerializerRegistry* registry = nullptr ) {
339- return pybindings::PyToChunk (std::move ( obj) , mimetype, registry);
341+ SerializerRegistry* registry = nullptr ) -> absl::StatusOr<Chunk> {
342+ return pybindings::PyToChunk (obj, mimetype, registry);
340343 },
341344 py::arg (" obj" ), py::arg_v (" mimetype" , " " ),
342345 py::arg_v (" registry" , nullptr ));
343346
344347 data.def (
345348 " from_chunk" ,
346349 [](Chunk chunk, std::string_view mimetype = " " ,
347- const SerializerRegistry* registry = nullptr ) -> py::object {
350+ const SerializerRegistry* registry = nullptr ) {
348351 return pybindings::PyFromChunk (std::move (chunk), mimetype, registry);
349352 },
350353 py::arg (" chunk" ), py::arg_v (" mimetype" , " " ),
0 commit comments