From 6ff042bf9b7d2faca76b6740020991cef1feed63 Mon Sep 17 00:00:00 2001 From: Icxolu <10486322+Icxolu@users.noreply.github.com> Date: Thu, 29 Feb 2024 21:37:31 +0100 Subject: [PATCH] use `BoundRef` to defer ref cnt inc until after the error case --- pyo3-macros-backend/src/method.rs | 4 ++-- src/impl_/coroutine.rs | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pyo3-macros-backend/src/method.rs b/pyo3-macros-backend/src/method.rs index 357560aacba..42f22204601 100644 --- a/pyo3-macros-backend/src/method.rs +++ b/pyo3-macros-backend/src/method.rs @@ -513,7 +513,7 @@ impl<'a> FnSpec<'a> { holders.pop().unwrap(); // does not actually use holder created by `self_arg` quote! {{ - let __guard = _pyo3::impl_::coroutine::RefGuard::<#cls>::new(_pyo3::Bound::from_borrowed_ptr(py, _slf))?; + let __guard = _pyo3::impl_::coroutine::RefGuard::<#cls>::new(&_pyo3::impl_::pymethods::BoundRef::ref_from_ptr(py, &_slf))?; async move { function(&__guard, #(#args),*).await } }} } @@ -521,7 +521,7 @@ impl<'a> FnSpec<'a> { holders.pop().unwrap(); // does not actually use holder created by `self_arg` quote! {{ - let mut __guard = _pyo3::impl_::coroutine::RefMutGuard::<#cls>::new(_pyo3::Bound::from_borrowed_ptr(py, _slf))?; + let mut __guard = _pyo3::impl_::coroutine::RefMutGuard::<#cls>::new(&_pyo3::impl_::pymethods::BoundRef::ref_from_ptr(py, &_slf))?; async move { function(&mut __guard, #(#args),*).await } }} } diff --git a/src/impl_/coroutine.rs b/src/impl_/coroutine.rs index 199208f69d1..9be95ba3303 100644 --- a/src/impl_/coroutine.rs +++ b/src/impl_/coroutine.rs @@ -39,10 +39,10 @@ fn get_ptr(obj: &Py) -> *mut T { pub struct RefGuard(Py); impl RefGuard { - pub fn new(obj: Bound<'_, PyAny>) -> PyResult { - let owned = obj.downcast_into::()?; + pub fn new(obj: &Bound<'_, PyAny>) -> PyResult { + let owned = obj.downcast::()?; mem::forget(owned.try_borrow()?); - Ok(RefGuard(owned.unbind())) + Ok(RefGuard(owned.clone().unbind())) } } @@ -67,10 +67,10 @@ impl Drop for RefGuard { pub struct RefMutGuard>(Py); impl> RefMutGuard { - pub fn new(obj: Bound<'_, PyAny>) -> PyResult { - let owned = obj.downcast_into::()?; + pub fn new(obj: &Bound<'_, PyAny>) -> PyResult { + let owned = obj.downcast::()?; mem::forget(owned.try_borrow_mut()?); - Ok(RefMutGuard(owned.unbind())) + Ok(RefMutGuard(owned.clone().unbind())) } }