Skip to content

Commit

Permalink
use BoundRef to defer ref cnt inc until after the error case
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Feb 29, 2024
1 parent 43e4fde commit 3c532d1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions pyo3-macros-backend/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,15 +513,15 @@ 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 }
}}
}
FnType::Fn(SelfType::Receiver { mutable: true, .. }) => {
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 }
}}
}
Expand Down
12 changes: 6 additions & 6 deletions src/impl_/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ fn get_ptr<T: PyClass>(obj: &Py<T>) -> *mut T {
pub struct RefGuard<T: PyClass>(Py<T>);

impl<T: PyClass> RefGuard<T> {
pub fn new(obj: Bound<'_, PyAny>) -> PyResult<Self> {
let owned = obj.downcast_into::<T>()?;
pub fn new(obj: &Bound<'_, PyAny>) -> PyResult<Self> {
let owned = obj.downcast::<T>()?;
mem::forget(owned.try_borrow()?);
Ok(RefGuard(owned.unbind()))
Ok(RefGuard(owned.clone().unbind()))
}
}

Expand All @@ -63,10 +63,10 @@ impl<T: PyClass> Drop for RefGuard<T> {
pub struct RefMutGuard<T: PyClass<Frozen = False>>(Py<T>);

impl<T: PyClass<Frozen = False>> RefMutGuard<T> {
pub fn new(obj: Bound<'_, PyAny>) -> PyResult<Self> {
let owned = obj.downcast_into::<T>()?;
pub fn new(obj: &Bound<'_, PyAny>) -> PyResult<Self> {
let owned = obj.downcast::<T>()?;
mem::forget(owned.try_borrow_mut()?);
Ok(RefMutGuard(owned.unbind()))
Ok(RefMutGuard(owned.clone().unbind()))
}
}

Expand Down

0 comments on commit 3c532d1

Please sign in to comment.