diff --git a/src/class/basic.rs b/src/class/basic.rs index cb920aee..d6be61f2 100644 --- a/src/class/basic.rs +++ b/src/class/basic.rs @@ -507,19 +507,21 @@ where let slf = py.from_borrowed_ptr::>(slf); let arg = py.from_borrowed_ptr::(arg); - let res = match extract_op(op) { - Ok(op) => match arg.extract() { - Ok(arg) => match slf.try_borrow_unguarded() { - Ok(borrow) => borrow.__richcmp__(arg, op).into(), - Err(e) => Err(e.into()), - }, - Err(e) => Err(e), - }, - Err(e) => Err(e), - }; - match res { - Ok(val) => val.into_py(py).into_ptr(), - Err(e) => e.restore_and_null(py), + match slf.try_borrow() { + Ok(borrowed_slf) => { + let res = match extract_op(op) { + Ok(op) => match arg.extract() { + Ok(arg) => borrowed_slf.__richcmp__(arg, op).into(), + Err(e) => Err(e), + }, + Err(e) => Err(e), + }; + match res { + Ok(val) => val.into_py(py).into_ptr(), + Err(e) => e.restore_and_null(py), + } + } + Err(e) => PyErr::from(e).restore_and_null(py), } } Some(wrap::) diff --git a/src/class/sequence.rs b/src/class/sequence.rs index 79716e84..4798a24e 100644 --- a/src/class/sequence.rs +++ b/src/class/sequence.rs @@ -264,25 +264,27 @@ mod sq_ass_item_impl { let _pool = crate::GILPool::new(py); let slf = py.from_borrowed_ptr::>(slf); - let result = if value.is_null() { - Err(PyErr::new::(format!( + if value.is_null() { + return PyErr::new::(format!( "Item deletion is not supported by {:?}", stringify!(T) - ))) - } else { - let value = py.from_borrowed_ptr::(value); - match value.extract() { - Ok(value) => match slf.try_borrow_mut_unguarded() { - Ok(slf_) => slf_.__setitem__(key.into(), value).into(), - Err(e) => e.into(), - }, - Err(e) => Err(e), - } - }; + )) + .restore_and_minus1(py); + } - match result { - Ok(_) => 0, - Err(e) => e.restore_and_minus1(py), + match slf.try_borrow_mut() { + Ok(mut slf) => { + let value = py.from_borrowed_ptr::(value); + let result = match value.extract() { + Ok(value) => slf.__setitem__(key.into(), value).into(), + Err(e) => e.into(), + }; + match result { + Ok(_) => 0, + Err(e) => e.restore_and_minus1(py), + } + } + Err(e) => PyErr::from(e).restore_and_minus1(py), } } Some(wrap::) @@ -371,12 +373,12 @@ mod sq_ass_item_impl { call_mut!(slf, __delitem__; key.into()) } else { let value = py.from_borrowed_ptr::(value); - match value.extract() { - Ok(value) => match slf.try_borrow_mut_unguarded() { - Ok(slf_) => slf_.__setitem__(key.into(), value).into(), - Err(e) => e.into(), + match slf.try_borrow_mut() { + Ok(mut slf_) => match value.extract() { + Ok(value) => slf_.__setitem__(key.into(), value).into(), + Err(e) => Err(e), }, - Err(e) => Err(e), + Err(e) => Err(e.into()), } }; match result { diff --git a/src/conversion.rs b/src/conversion.rs index 3c10e6a2..469eb749 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -259,8 +259,7 @@ where { fn extract(obj: &'a PyAny) -> PyResult { let cell: &PyCell = PyTryFrom::try_from(obj)?; - let ref_ = unsafe { cell.try_borrow_unguarded()? }; - Ok(ref_.clone()) + Ok(unsafe { cell.try_borrow_unguarded()?.clone() }) } }