Skip to content

Commit

Permalink
improve serialization error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasmueboe committed Oct 28, 2024
1 parent 42b8ef7 commit abf98b7
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/gridcounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bound<'py, PyBytes>> {
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__(
Expand Down

0 comments on commit abf98b7

Please sign in to comment.