From abf98b7083cd23e5179906511b3fe055c5508ccc Mon Sep 17 00:00:00 2001 From: niklasmueboe Date: Mon, 28 Oct 2024 15:26:20 +0100 Subject: [PATCH] improve serialization error handling --- src/gridcounts.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/gridcounts.rs b/src/gridcounts.rs index d24a20d..3eb8a6e 100644 --- a/src/gridcounts.rs +++ b/src/gridcounts.rs @@ -254,19 +254,24 @@ impl GridCounts { } fn __setstate__(&mut self, state: Bound<'_, PyBytes>) -> PyResult<()> { - let (counts, shape, resolution, n_threads) = deserialize(state.as_bytes()).unwrap(); - self.counts = counts; - self.shape = shape; - self.resolution = resolution; - self.set_n_threads(Some(n_threads))?; + match deserialize(state.as_bytes()) { + Ok((counts, shape, resolution, n_threads)) => { + self.counts = counts; + self.shape = shape; + self.resolution = resolution; + self.set_n_threads(Some(n_threads))?; - Ok(()) + Ok(()) + } + Err(e) => Err(PyValueError::new_err(e.to_string())), + } } fn __getstate__<'py>(&self, py: Python<'py>) -> PyResult> { - let to_bytes = (&self.counts, self.shape, self.resolution, self.n_threads); - - Ok(PyBytes::new_bound(py, &serialize(&to_bytes).unwrap())) + match serialize(&(&self.counts, self.shape, self.resolution, self.n_threads)) { + Ok(bytes) => Ok(PyBytes::new_bound(py, &bytes)), + Err(e) => Err(PyRuntimeError::new_err(e.to_string())), + } } fn __getnewargs_ex__(