Skip to content

Commit 1227ac8

Browse files
authored
remove unwrap in pyo3 module (#751)
1 parent b74b303 commit 1227ac8

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

python/tskit_glue/src/lib.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,16 @@ impl IndividualMetadata {
5454

5555
/// Decode mutation metadata generated in rust via the `bincode` crate.
5656
#[pyfunction]
57-
fn decode_bincode_mutation_metadata(md: Vec<u8>) -> MutationMetadata {
58-
// NOTE: the unwrap here is not correct for production code
59-
// and a failure will crash the Python interpreter!
60-
let md = bincode::deserialize_from(md.as_slice()).unwrap();
61-
md
57+
fn decode_bincode_mutation_metadata(md: Vec<u8>) -> PyResult<MutationMetadata> {
58+
bincode::deserialize_from(md.as_slice())
59+
.map_err(|_| pyo3::exceptions::PyValueError::new_err("error decoding mutation metadata"))
6260
}
6361

6462
/// Decode individual metadata generated in rust via the `bincode` crate.
6563
#[pyfunction]
66-
fn decode_bincode_individual_metadata(md: Vec<u8>) -> IndividualMetadata {
67-
// NOTE: the unwrap here is not correct for production code
68-
// and a failure will crash the Python interpreter!
69-
let md = bincode::deserialize_from(md.as_slice()).unwrap();
70-
md
64+
fn decode_bincode_individual_metadata(md: Vec<u8>) -> PyResult<IndividualMetadata> {
65+
bincode::deserialize_from(md.as_slice())
66+
.map_err(|_| pyo3::exceptions::PyValueError::new_err("error decoding individual metadata"))
7167
}
7268

7369
/// A Python module implemented in Rust.

0 commit comments

Comments
 (0)