From 6af1d2fbeac287ced0b757681a86568f93b46be4 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Fri, 16 May 2025 10:00:57 -0700 Subject: [PATCH] remove unwrap in pyo3 module --- python/tskit_glue/src/lib.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) 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.