diff --git a/python/tskit_glue/src/lib.rs b/python/tskit_glue/src/lib.rs index 476ad5426..0adbc1b7f 100644 --- a/python/tskit_glue/src/lib.rs +++ b/python/tskit_glue/src/lib.rs @@ -54,20 +54,16 @@ impl IndividualMetadata { /// Decode mutation metadata generated in rust via the `bincode` crate. #[pyfunction] -fn decode_bincode_mutation_metadata(md: Vec) -> MutationMetadata { - // NOTE: the unwrap here is not correct for production code - // and a failure will crash the Python interpreter! - let md = bincode::deserialize_from(md.as_slice()).unwrap(); - md +fn decode_bincode_mutation_metadata(md: Vec) -> PyResult { + bincode::deserialize_from(md.as_slice()) + .map_err(|_| pyo3::exceptions::PyValueError::new_err("error decoding mutation metadata")) } /// Decode individual metadata generated in rust via the `bincode` crate. #[pyfunction] -fn decode_bincode_individual_metadata(md: Vec) -> IndividualMetadata { - // NOTE: the unwrap here is not correct for production code - // and a failure will crash the Python interpreter! - let md = bincode::deserialize_from(md.as_slice()).unwrap(); - md +fn decode_bincode_individual_metadata(md: Vec) -> PyResult { + bincode::deserialize_from(md.as_slice()) + .map_err(|_| pyo3::exceptions::PyValueError::new_err("error decoding individual metadata")) } /// A Python module implemented in Rust.