Skip to content

Commit

Permalink
Merge pull request #3694 from davidhewitt/set-frozenset
Browse files Browse the repository at this point in the history
implement `PySetMethods` and `PyFrozenSetMethods`
  • Loading branch information
adamreichold authored Dec 26, 2023
2 parents c44d2f5 + 442d13d commit 0344921
Show file tree
Hide file tree
Showing 8 changed files with 388 additions and 140 deletions.
26 changes: 17 additions & 9 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ impl<'py, T> Bound<'py, T> {
)
}

/// Removes the connection for this `Bound<T>` from the GIL, allowing
/// it to cross thread boundaries.
pub fn unbind(self) -> Py<T> {
// Safety: the type T is known to be correct and the ownership of the
// pointer is transferred to the new Py<T> instance.
unsafe { Py::from_non_null(self.into_non_null()) }
}

/// Casts this `Bound<T>` as the corresponding "GIL Ref" type.
///
/// This is a helper to be used for migration from the deprecated "GIL Refs" API.
Expand Down Expand Up @@ -973,7 +981,7 @@ impl<T> Py<T> {
where
N: IntoPy<Py<PyString>>,
{
self.bind(py).as_any().getattr(attr_name).map(Into::into)
self.bind(py).as_any().getattr(attr_name).map(Bound::unbind)
}

/// Sets an attribute value.
Expand Down Expand Up @@ -1017,21 +1025,21 @@ impl<T> Py<T> {
args: impl IntoPy<Py<PyTuple>>,
kwargs: Option<&PyDict>,
) -> PyResult<PyObject> {
self.bind(py).as_any().call(args, kwargs).map(Into::into)
self.bind(py).as_any().call(args, kwargs).map(Bound::unbind)
}

/// Calls the object with only positional arguments.
///
/// This is equivalent to the Python expression `self(*args)`.
pub fn call1(&self, py: Python<'_>, args: impl IntoPy<Py<PyTuple>>) -> PyResult<PyObject> {
self.bind(py).as_any().call1(args).map(Into::into)
self.bind(py).as_any().call1(args).map(Bound::unbind)
}

/// Calls the object without arguments.
///
/// This is equivalent to the Python expression `self()`.
pub fn call0(&self, py: Python<'_>) -> PyResult<PyObject> {
self.bind(py).as_any().call0().map(Into::into)
self.bind(py).as_any().call0().map(Bound::unbind)
}

/// Calls a method on the object.
Expand All @@ -1054,7 +1062,7 @@ impl<T> Py<T> {
self.bind(py)
.as_any()
.call_method(name, args, kwargs)
.map(Into::into)
.map(Bound::unbind)
}

/// Calls a method on the object with only positional arguments.
Expand All @@ -1071,7 +1079,7 @@ impl<T> Py<T> {
self.bind(py)
.as_any()
.call_method1(name, args)
.map(Into::into)
.map(Bound::unbind)
}

/// Calls a method on the object with no arguments.
Expand All @@ -1084,7 +1092,7 @@ impl<T> Py<T> {
where
N: IntoPy<Py<PyString>>,
{
self.bind(py).as_any().call_method0(name).map(Into::into)
self.bind(py).as_any().call_method0(name).map(Bound::unbind)
}

/// Create a `Py<T>` instance by taking ownership of the given FFI pointer.
Expand Down Expand Up @@ -1292,7 +1300,7 @@ where
impl<T> std::convert::From<Bound<'_, T>> for Py<T> {
#[inline]
fn from(other: Bound<'_, T>) -> Self {
unsafe { Self::from_non_null(other.into_non_null()) }
other.unbind()
}
}

Expand Down Expand Up @@ -1618,7 +1626,7 @@ a = A()
.as_borrowed()
.to_owned();
let ptr = instance.as_ptr();
let instance: PyObject = instance.clone().into();
let instance: PyObject = instance.clone().unbind();
assert_eq!(instance.as_ptr(), ptr);
})
}
Expand Down
2 changes: 2 additions & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ pub use crate::types::bytearray::PyByteArrayMethods;
pub use crate::types::bytes::PyBytesMethods;
pub use crate::types::dict::PyDictMethods;
pub use crate::types::float::PyFloatMethods;
pub use crate::types::frozenset::PyFrozenSetMethods;
pub use crate::types::list::PyListMethods;
pub use crate::types::mapping::PyMappingMethods;
pub use crate::types::sequence::PySequenceMethods;
pub use crate::types::set::PySetMethods;
pub use crate::types::string::PyStringMethods;
9 changes: 0 additions & 9 deletions src/types/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,15 +626,6 @@ impl<'py> BoundDictIterator<'py> {
}
}

impl<'py> IntoIterator for &'_ Bound<'py, PyDict> {
type Item = (Bound<'py, PyAny>, Bound<'py, PyAny>);
type IntoIter = BoundDictIterator<'py>;

fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

impl<'py> IntoIterator for Bound<'py, PyDict> {
type Item = (Bound<'py, PyAny>, Bound<'py, PyAny>);
type IntoIter = BoundDictIterator<'py>;
Expand Down
Loading

0 comments on commit 0344921

Please sign in to comment.