From f0c2f3f7a3e991af15b9ec177116aba485e5eb14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannis=20Sch=C3=B6nleber?= Date: Mon, 2 Dec 2024 19:59:32 +0100 Subject: [PATCH] chore(pickle): handle unwraps with error message --- src/python_bindings/mod.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/python_bindings/mod.rs b/src/python_bindings/mod.rs index 086fdb60..55d979d1 100644 --- a/src/python_bindings/mod.rs +++ b/src/python_bindings/mod.rs @@ -94,7 +94,10 @@ impl PyIndex { Python::with_gil(|py| { let cls = PyModule::import_bound(py, "outlines_core.fsm.outlines_core_rs")? .getattr("Index")?; - let binary_data: Vec = bincode::encode_to_vec(&self.0, config::standard()).unwrap(); + let binary_data: Vec = bincode::encode_to_vec(&self.0, config::standard()) + .map_err(|e| { + PyErr::new::(format!("Serialization of Index failed: {}", e)) + })?; Ok((cls.getattr("from_binary")?.to_object(py), (binary_data,))) }) } @@ -102,7 +105,9 @@ impl PyIndex { #[staticmethod] fn from_binary(binary_data: Vec) -> PyResult { let (index, _): (Index, usize) = - bincode::decode_from_slice(&binary_data[..], config::standard()).unwrap(); + bincode::decode_from_slice(&binary_data[..], config::standard()).map_err(|e| { + PyErr::new::(format!("Deserialization of Index failed: {}", e)) + })?; Ok(PyIndex(index)) }