Skip to content

Commit

Permalink
expose Bound::as_gil_ref and Bound::into_gil_ref
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Dec 24, 2023
1 parent 7d24584 commit fc2a62e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::pycell::{PyBorrowError, PyBorrowMutError, PyCell};
use crate::pyclass::boolean_struct::{False, True};
use crate::type_object::HasPyGilRef;
use crate::types::any::PyAnyMethods;
use crate::types::string::PyStringMethods;
use crate::types::{PyDict, PyString, PyTuple};
use crate::{
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyClass, PyClassInitializer, PyRef, PyRefMut,
Expand Down Expand Up @@ -97,8 +98,10 @@ fn python_format(
f: &mut std::fmt::Formatter<'_>,
) -> Result<(), std::fmt::Error> {
match format_result {
Result::Ok(s) => return f.write_str(&s.as_gil_ref().to_string_lossy()),
Result::Err(err) => {
Result::Ok(s) => return f.write_str(&s.to_string_lossy()),
Result::Err(err) =>
{
#[allow(deprecated)]
err.write_unraisable(any.py(), std::option::Option::Some(any.as_gil_ref()))
}
}
Expand Down Expand Up @@ -184,17 +187,28 @@ impl<'py, T> Bound<'py, T> {
unsafe { std::mem::transmute(gil_ref) }
}

/// Internal helper to get to pool references for backwards compatibility
#[doc(hidden)] // public and doc(hidden) to use in examples and tests for now
/// 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.
#[deprecated(
since = "0.21.0",
note = "`as_gil_ref` is a temporary migration tool for the deprecated GIL Ref API"
)]
pub fn as_gil_ref(&'py self) -> &'py T::AsRefTarget
where
T: HasPyGilRef,
{
unsafe { self.py().from_borrowed_ptr(self.as_ptr()) }
}

/// Internal helper to get to pool references for backwards compatibility
#[doc(hidden)] // public but hidden, to use for tests for now
/// Casts this `Bound<T>` as the corresponding "GIL Ref" type, registering the pointer on the
/// [release pool](Python::from_owned_ptr).
///
/// This is a helper to be used for migration from the deprecated "GIL Refs" API.
#[deprecated(
since = "0.21.0",
note = "`as_gil_ref` is a temporary migration tool for the deprecated GIL Ref API"
)]
pub fn into_gil_ref(self) -> &'py T::AsRefTarget
where
T: HasPyGilRef,
Expand Down
6 changes: 6 additions & 0 deletions src/types/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pyobject_native_type_extract!(PyAny);

pyobject_native_type_sized!(PyAny, ffi::PyObject);

#[allow(deprecated)]
impl PyAny {
/// Returns whether `self` and `other` point to the same object. To compare
/// the equality of two objects (the `==` operator), use [`eq`](PyAny::eq).
Expand Down Expand Up @@ -2049,6 +2050,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {
where
T: PyTypeCheck,
{
#[allow(deprecated)]
if T::type_check(self.as_gil_ref()) {
// Safety: type_check is responsible for ensuring that the type is correct
Ok(unsafe { self.downcast_unchecked() })
Expand All @@ -2062,6 +2064,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {
where
T: PyTypeCheck,
{
#[allow(deprecated)]
if T::type_check(self.as_gil_ref()) {
// Safety: type_check is responsible for ensuring that the type is correct
Ok(unsafe { self.downcast_into_unchecked() })
Expand Down Expand Up @@ -2110,6 +2113,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {
where
D: FromPyObject<'a>,
{
#[allow(deprecated)]
FromPyObject::extract(self.as_gil_ref())
}

Expand Down Expand Up @@ -2167,11 +2171,13 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {

#[inline]
fn is_instance_of<T: PyTypeInfo>(&self) -> bool {
#[allow(deprecated)]
T::is_type_of(self.as_gil_ref())
}

#[inline]
fn is_exact_instance_of<T: PyTypeInfo>(&self) -> bool {
#[allow(deprecated)]
T::is_exact_type_of(self.as_gil_ref())
}

Expand Down
2 changes: 2 additions & 0 deletions src/types/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pyobject_native_type_core!(
#checkfunction=ffi::PyDictItems_Check
);

#[allow(deprecated)]
impl PyDict {
/// Creates a new empty dictionary.
pub fn new(py: Python<'_>) -> &PyDict {
Expand Down Expand Up @@ -514,6 +515,7 @@ fn dict_len(dict: &Bound<'_, PyDict>) -> Py_ssize_t {
/// PyO3 implementation of an iterator for a Python `dict` object.
pub struct PyDictIterator<'py>(BoundDictIterator<'py>);

#[allow(deprecated)]
impl<'py> Iterator for PyDictIterator<'py> {
type Item = (&'py PyAny, &'py PyAny);

Expand Down
1 change: 1 addition & 0 deletions src/types/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl PyIterator {
///
/// Equivalent to Python's built-in `iter` function.
pub fn from_object(obj: &PyAny) -> PyResult<&PyIterator> {
#[allow(deprecated)]
Self::from_object2(Bound::borrowed_from_gil_ref(&obj)).map(Bound::into_gil_ref)
}

Expand Down
3 changes: 3 additions & 0 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub(crate) fn new_from_iter<'py>(
}
}

#[allow(deprecated)]
impl PyList {
/// Constructs a new list with the given elements.
///
Expand Down Expand Up @@ -555,6 +556,7 @@ impl<'py> PyListMethods<'py> for Bound<'py, PyList> {
/// Used by `PyList::iter()`.
pub struct PyListIterator<'a>(BoundListIterator<'a>);

#[allow(deprecated)]
impl<'a> Iterator for PyListIterator<'a> {
type Item = &'a PyAny;

Expand All @@ -569,6 +571,7 @@ impl<'a> Iterator for PyListIterator<'a> {
}
}

#[allow(deprecated)]
impl<'a> DoubleEndedIterator for PyListIterator<'a> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
Expand Down
3 changes: 3 additions & 0 deletions src/types/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct PyMapping(PyAny);
pyobject_native_type_named!(PyMapping);
pyobject_native_type_extract!(PyMapping);

#[allow(deprecated)]
impl PyMapping {
/// Returns the number of objects in the mapping.
///
Expand Down Expand Up @@ -100,7 +101,9 @@ impl PyMapping {
.items()
.map(Bound::into_gil_ref)
}
}

impl PyMapping {
/// Register a pyclass as a subclass of `collections.abc.Mapping` (from the Python standard
/// library). This is equvalent to `collections.abc.Mapping.register(T)` in Python.
/// This registration is required for a pyclass to be downcastable from `PyAny` to `PyMapping`.
Expand Down
1 change: 1 addition & 0 deletions src/types/pysuper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl PySuper {
/// }
/// ```
pub fn new<'py>(ty: &'py PyType, obj: &'py PyAny) -> PyResult<&'py PySuper> {
#[allow(deprecated)]
Self::new2(
Bound::borrowed_from_gil_ref(&ty),
Bound::borrowed_from_gil_ref(&obj),
Expand Down
1 change: 1 addition & 0 deletions src/types/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct PySequence(PyAny);
pyobject_native_type_named!(PySequence);
pyobject_native_type_extract!(PySequence);

#[allow(deprecated)]
impl PySequence {
/// Returns the number of objects in sequence.
///
Expand Down

0 comments on commit fc2a62e

Please sign in to comment.