Skip to content

Commit

Permalink
Merge pull request #3680 from davidhewitt/list2
Browse files Browse the repository at this point in the history
implement `PyListMethods`
  • Loading branch information
davidhewitt committed Dec 21, 2023
2 parents 5b12cf1 + ee1272e commit 2788f4a
Show file tree
Hide file tree
Showing 4 changed files with 408 additions and 66 deletions.
21 changes: 21 additions & 0 deletions src/instance.rs
Expand Up @@ -221,13 +221,26 @@ unsafe impl<T> AsPyPointer for Py2<'_, T> {
///
/// The advantage of this over `&Py2` is that it avoids the need to have a pointer-to-pointer, as Py2
/// is already a pointer to an `ffi::PyObject``.
///
/// Similarly, this type is `Copy` and `Clone`, like a shared reference (`&T`).
#[repr(transparent)]
pub(crate) struct Py2Borrowed<'a, 'py, T>(
NonNull<ffi::PyObject>,
PhantomData<&'a Py<T>>,
Python<'py>,
);

impl<'py, T> Py2Borrowed<'_, 'py, T> {
/// Creates a new owned `Py2` from this borrowed reference by increasing the reference count.
pub(crate) fn to_owned(self) -> Py2<'py, T> {
unsafe { ffi::Py_INCREF(self.as_ptr()) };
Py2(
self.py(),
ManuallyDrop::new(unsafe { Py::from_non_null(self.0) }),
)
}
}

impl<'a, 'py> Py2Borrowed<'a, 'py, PyAny> {
/// # Safety
/// This is similar to `std::slice::from_raw_parts`, the lifetime `'a` is completely defined by
Expand Down Expand Up @@ -315,6 +328,14 @@ impl<'py, T> Deref for Py2Borrowed<'_, 'py, T> {
}
}

impl<T> Clone for Py2Borrowed<'_, '_, T> {
fn clone(&self) -> Self {
*self
}
}

impl<T> Copy for Py2Borrowed<'_, '_, T> {}

/// A GIL-independent reference to an object allocated on the Python heap.
///
/// This type does not auto-dereference to the inner object because you must prove you hold the GIL to access it.
Expand Down
1 change: 1 addition & 0 deletions src/prelude.rs
Expand Up @@ -32,6 +32,7 @@ pub use crate::wrap_pyfunction;
// pub(crate) use crate::types::bytes::PyBytesMethods;
// pub(crate) use crate::types::dict::PyDictMethods;
// pub(crate) use crate::types::float::PyFloatMethods;
// pub(crate) use crate::types::list::PyListMethods;
// pub(crate) use crate::types::mapping::PyMappingMethods;
// pub(crate) use crate::types::sequence::PySequenceMethods;
// pub(crate) use crate::types::string::PyStringMethods;
8 changes: 4 additions & 4 deletions src/types/dict.rs
Expand Up @@ -2,7 +2,7 @@ use super::PyMapping;
use crate::err::{self, PyErr, PyResult};
use crate::ffi::Py_ssize_t;
use crate::ffi_ptr_ext::FfiPtrExt;
use crate::instance::Py2;
use crate::instance::{Py2, Py2Borrowed};
use crate::py_result_ext::PyResultExt;
use crate::types::any::PyAnyMethods;
use crate::types::{PyAny, PyList};
Expand Down Expand Up @@ -406,7 +406,7 @@ impl<'py> PyDictMethods<'py> for Py2<'py, PyDict> {
match unsafe {
ffi::PyDict_GetItemWithError(dict.as_ptr(), key.as_ptr())
.assume_borrowed_or_opt(py)
.map(|borrowed_any| borrowed_any.clone())
.map(Py2Borrowed::to_owned)
} {
some @ Some(_) => Ok(some),
None => PyErr::take(py).map(Err).transpose(),
Expand Down Expand Up @@ -595,8 +595,8 @@ impl<'py> Iterator for PyDictIterator2<'py> {
// - 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_unchecked(py) }.clone(),
unsafe { value.assume_borrowed_unchecked(py) }.clone(),
unsafe { key.assume_borrowed_unchecked(py) }.to_owned(),
unsafe { value.assume_borrowed_unchecked(py) }.to_owned(),
))
} else {
None
Expand Down

0 comments on commit 2788f4a

Please sign in to comment.