Skip to content

Commit

Permalink
review: LilyFoote, Icxolu feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Feb 28, 2024
1 parent 108f1e7 commit 101563c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyo3-macros-backend/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl SelfType {
holders.push(quote_spanned! { *span =>
#[allow(clippy::let_unit_value)]
let mut #holder = _pyo3::impl_::extract_argument::FunctionArgumentHolder::INIT;
let mut #slf = _pyo3::Borrowed::from_ptr(#py, #slf);
let mut #slf = _pyo3::impl_::pymethods::BoundRef::ref_from_ptr(#py, &#slf);
});
error_mode.handle_error(quote_spanned! { *span =>
_pyo3::impl_::extract_argument::#method::<#cls>(
Expand Down
4 changes: 2 additions & 2 deletions pyo3-macros-backend/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ pub fn impl_arg_params(
.collect::<Result<_>>()?;
return Ok((
quote! {
let _args = _pyo3::Borrowed::from_ptr(py, _args);
let _kwargs = _pyo3::Borrowed::from_ptr_or_opt(py, _kwargs);
let _args = _pyo3::impl_::pymethods::BoundRef::ref_from_ptr(py, &_args);
let _kwargs = _pyo3::impl_::pymethods::BoundRef::ref_from_ptr_or_opt(py, &_kwargs);
},
arg_convert,
));
Expand Down
2 changes: 1 addition & 1 deletion pyo3-macros-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ fn extract_object(
});
extract_error_mode.handle_error(quote! {
_pyo3::impl_::extract_argument::extract_argument(
&_pyo3::Borrowed::from_ptr(py, #source_ptr),
&_pyo3::impl_::pymethods::BoundRef::ref_from_ptr(py, &#source_ptr),
&mut #holder,
#name
)
Expand Down
8 changes: 8 additions & 0 deletions src/impl_/pymethods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,11 @@ impl<T> From<BoundRef<'_, '_, T>> for Py<T> {
bound.0.clone().unbind()
}
}

impl<'py, T> std::ops::Deref for BoundRef<'_, 'py, T> {
type Target = Bound<'py, T>;
#[inline]
fn deref(&self) -> &Self::Target {
self.0
}
}
4 changes: 4 additions & 0 deletions src/types/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,11 +692,15 @@ mod borrowed_iter {
let mut key: *mut ffi::PyObject = std::ptr::null_mut();
let mut value: *mut ffi::PyObject = std::ptr::null_mut();

// Safety: self.dict lives sufficiently long that the pointer is not dangling
if unsafe { ffi::PyDict_Next(self.dict.as_ptr(), &mut self.ppos, &mut key, &mut value) }
!= 0
{
let py = self.dict.py();
self.len -= 1;
// Safety:
// - PyDict_Next returns borrowed values
// - we have already checked that `PyDict_Next` succeeded, so we can assume these to be non-null
Some(unsafe { (key.assume_borrowed(py), value.assume_borrowed(py)) })
} else {
None
Expand Down

0 comments on commit 101563c

Please sign in to comment.