Patchwork [08,of,11] rust-dirstate: handle invalid length of p1/p2 parameters

login
register
mail settings
Submitter Yuya Nishihara
Date Aug. 17, 2019, 12:12 p.m.
Message ID <5f76ca22fb8fb29ec0a6.1566043929@mimosa>
Download mbox | patch
Permalink /patch/41330/
State Accepted
Headers show

Comments

Yuya Nishihara - Aug. 17, 2019, 12:12 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1566032768 -32400
#      Sat Aug 17 18:06:08 2019 +0900
# Node ID 5f76ca22fb8fb29ec0a60ec0af88cb328f7a0c9e
# Parent  d496a0a5d5f8ff45afde5a0a90b0917e507cbbbb
rust-dirstate: handle invalid length of p1/p2 parameters

It uses match syntax since map_err() failed to deduce the argument type.

Patch

diff --git a/rust/hg-cpython/src/dirstate/dirstate_map.rs b/rust/hg-cpython/src/dirstate/dirstate_map.rs
--- a/rust/hg-cpython/src/dirstate/dirstate_map.rs
+++ b/rust/hg-cpython/src/dirstate/dirstate_map.rs
@@ -24,9 +24,8 @@  use crate::{
     ref_sharing::PySharedState,
 };
 use hg::{
-    DirsIterable, DirsMultiset, DirstateEntry,
-    DirstateMap as RustDirstateMap, DirstateParents, DirstateParseError,
-    EntryState,
+    DirsIterable, DirsMultiset, DirstateEntry, DirstateMap as RustDirstateMap,
+    DirstateParents, DirstateParseError, EntryState, PARENT_SIZE,
 };
 
 // TODO
@@ -239,9 +238,8 @@  py_class!(pub class DirstateMap |py| {
     }
 
     def setparents(&self, p1: PyObject, p2: PyObject) -> PyResult<PyObject> {
-        // TODO: don't panic; raise Python exception instead.
-        let p1 = p1.extract::<PyBytes>(py)?.data(py).try_into().unwrap();
-        let p2 = p2.extract::<PyBytes>(py)?.data(py).try_into().unwrap();
+        let p1 = extract_node_id(py, &p1)?;
+        let p2 = extract_node_id(py, &p2)?;
 
         self.inner(py)
             .borrow_mut()
@@ -275,9 +273,8 @@  py_class!(pub class DirstateMap |py| {
     ) -> PyResult<PyBytes> {
         let now = Duration::new(now.extract(py)?, 0);
         let parents = DirstateParents {
-            // TODO: don't panic; raise Python exception instead.
-            p1: p1.extract::<PyBytes>(py)?.data(py).try_into().unwrap(),
-            p2: p2.extract::<PyBytes>(py)?.data(py).try_into().unwrap(),
+            p1: extract_node_id(py, &p1)?,
+            p2: extract_node_id(py, &p2)?,
         };
 
         match self.borrow_mut(py)?.pack(parents, now) {
@@ -508,3 +505,11 @@  py_shared_mapping_iterator!(
     DirstateMap::translate_key_value,
     Option<(PyBytes, PyObject)>
 );
+
+fn extract_node_id(py: Python, obj: &PyObject) -> PyResult<[u8; PARENT_SIZE]> {
+    let bytes = obj.extract::<PyBytes>(py)?;
+    match bytes.data(py).try_into() {
+        Ok(s) => Ok(s),
+        Err(e) => Err(PyErr::new::<exc::ValueError, _>(py, e.to_string())),
+    }
+}