Skip to content

Commit

Permalink
remove more gil-ref stuff (#4342)
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu authored Jul 12, 2024
1 parent e40e13b commit e51251b
Show file tree
Hide file tree
Showing 26 changed files with 6 additions and 833 deletions.
8 changes: 4 additions & 4 deletions guide/src/memory.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ held. (If PyO3 could not assume this, every PyO3 API would need to take a
`Python` GIL token to prove that the GIL is held.) This allows us to write
very simple and easy-to-understand programs like this:

```rust
```rust,ignore
# #![allow(unused_imports)]
# use pyo3::prelude::*;
# use pyo3::types::PyString;
Expand All @@ -58,7 +58,7 @@ the `GILPool` is also dropped and the Python reference counts of the variables
it owns are decreased, releasing them to the Python garbage collector. Most
of the time we don't have to think about this, but consider the following:

```rust
```rust,ignore
# #![allow(unused_imports)]
# use pyo3::prelude::*;
# use pyo3::types::PyString;
Expand Down Expand Up @@ -99,7 +99,7 @@ PyO3 0.21 has introduced a new API known as the Bound API, which doesn't have th
In general we don't want unbounded memory growth during loops! One workaround
is to acquire and release the GIL with each iteration of the loop.

```rust
```rust,ignore
# #![allow(unused_imports)]
# use pyo3::prelude::*;
# use pyo3::types::PyString;
Expand All @@ -123,7 +123,7 @@ It might not be practical or performant to acquire and release the GIL so many
times. Another workaround is to work with the `GILPool` object directly, but
this is unsafe.

```rust
```rust,ignore
# #![allow(unused_imports)]
# use pyo3::prelude::*;
# use pyo3::types::PyString;
Expand Down
12 changes: 0 additions & 12 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

//! `PyBuffer` implementation
use crate::Bound;
#[cfg(feature = "gil-refs")]
use crate::PyNativeType;
use crate::{err, exceptions::PyBufferError, ffi, FromPyObject, PyAny, PyResult, Python};
use std::marker::PhantomData;
use std::os::raw;
Expand Down Expand Up @@ -191,16 +189,6 @@ impl<'py, T: Element> FromPyObject<'py> for PyBuffer<T> {
}

impl<T: Element> PyBuffer<T> {
/// Deprecated form of [`PyBuffer::get_bound`]
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyBuffer::get` will be replaced by `PyBuffer::get_bound` in a future PyO3 version"
)]
pub fn get(obj: &PyAny) -> PyResult<PyBuffer<T>> {
Self::get_bound(&obj.as_borrowed())
}

/// Gets the underlying buffer from the specified python object.
pub fn get_bound(obj: &Bound<'_, PyAny>) -> PyResult<PyBuffer<T>> {
// TODO: use nightly API Box::new_uninit() once stable
Expand Down
11 changes: 0 additions & 11 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,17 +349,6 @@ where
}
}

#[allow(deprecated)]
#[cfg(feature = "gil-refs")]
impl<'py, T> FromPyObject<'py> for &'py crate::PyCell<T>
where
T: PyClass,
{
fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self> {
obj.clone().into_gil_ref().downcast().map_err(Into::into)
}
}

impl<T> FromPyObject<'_> for T
where
T: PyClass + Clone,
Expand Down
186 changes: 0 additions & 186 deletions src/err/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use crate::panic::PanicException;
use crate::type_object::PyTypeInfo;
use crate::types::any::PyAnyMethods;
use crate::types::{string::PyStringMethods, typeobject::PyTypeMethods, PyTraceback, PyType};
#[cfg(feature = "gil-refs")]
use crate::PyNativeType;
use crate::{
exceptions::{self, PyBaseException},
ffi,
Expand Down Expand Up @@ -47,34 +45,6 @@ unsafe impl Sync for PyErr {}
/// Represents the result of a Python call.
pub type PyResult<T> = Result<T, PyErr>;

/// Error that indicates a failure to convert a PyAny to a more specific Python type.
#[derive(Debug)]
#[cfg(feature = "gil-refs")]
pub struct PyDowncastError<'a> {
from: &'a PyAny,
to: Cow<'static, str>,
}

#[cfg(feature = "gil-refs")]
impl<'a> PyDowncastError<'a> {
/// Create a new `PyDowncastError` representing a failure to convert the object
/// `from` into the type named in `to`.
pub fn new(from: &'a PyAny, to: impl Into<Cow<'static, str>>) -> Self {
PyDowncastError {
from,
to: to.into(),
}
}

/// Compatibility API to convert the Bound variant `DowncastError` into the
/// gil-ref variant
pub(crate) fn from_downcast_err(DowncastError { from, to }: DowncastError<'a, 'a>) -> Self {
#[allow(deprecated)]
let from = unsafe { from.py().from_borrowed_ptr(from.as_ptr()) };
Self { from, to }
}
}

/// Error that indicates a failure to convert a PyAny to a more specific Python type.
#[derive(Debug)]
pub struct DowncastError<'a, 'py> {
Expand Down Expand Up @@ -194,19 +164,6 @@ impl PyErr {
})))
}

/// Deprecated form of [`PyErr::from_type_bound`]
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyErr::from_type` will be replaced by `PyErr::from_type_bound` in a future PyO3 version"
)]
pub fn from_type<A>(ty: &PyType, args: A) -> PyErr
where
A: PyErrArguments + Send + Sync + 'static,
{
PyErr::from_state(PyErrState::lazy(ty.into(), args))
}

/// Constructs a new PyErr from the given Python type and arguments.
///
/// `ty` is the exception type; usually one of the standard exceptions
Expand All @@ -224,16 +181,6 @@ impl PyErr {
PyErr::from_state(PyErrState::lazy(ty.unbind().into_any(), args))
}

/// Deprecated form of [`PyErr::from_value_bound`].
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyErr::from_value` will be replaced by `PyErr::from_value_bound` in a future PyO3 version"
)]
pub fn from_value(obj: &PyAny) -> PyErr {
PyErr::from_value_bound(obj.as_borrowed().to_owned())
}

/// Creates a new PyErr.
///
/// If `obj` is a Python exception object, the PyErr will contain that object.
Expand Down Expand Up @@ -282,16 +229,6 @@ impl PyErr {
PyErr::from_state(state)
}

/// Deprecated form of [`PyErr::get_type_bound`].
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyErr::get_type` will be replaced by `PyErr::get_type_bound` in a future PyO3 version"
)]
pub fn get_type<'py>(&'py self, py: Python<'py>) -> &'py PyType {
self.get_type_bound(py).into_gil_ref()
}

/// Returns the type of this exception.
///
/// # Examples
Expand All @@ -307,16 +244,6 @@ impl PyErr {
self.normalized(py).ptype(py)
}

/// Deprecated form of [`PyErr::value_bound`].
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyErr::value` will be replaced by `PyErr::value_bound` in a future PyO3 version"
)]
pub fn value<'py>(&'py self, py: Python<'py>) -> &'py PyBaseException {
self.value_bound(py).as_gil_ref()
}

/// Returns the value of this exception.
///
/// # Examples
Expand Down Expand Up @@ -349,16 +276,6 @@ impl PyErr {
exc
}

/// Deprecated form of [`PyErr::traceback_bound`].
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyErr::traceback` will be replaced by `PyErr::traceback_bound` in a future PyO3 version"
)]
pub fn traceback<'py>(&'py self, py: Python<'py>) -> Option<&'py PyTraceback> {
self.normalized(py).ptraceback(py).map(|b| b.into_gil_ref())
}

/// Returns the traceback of this exception object.
///
/// # Examples
Expand Down Expand Up @@ -500,28 +417,6 @@ impl PyErr {
}
}

/// Deprecated form of [`PyErr::new_type_bound`]
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyErr::new_type` will be replaced by `PyErr::new_type_bound` in a future PyO3 version"
)]
pub fn new_type(
py: Python<'_>,
name: &str,
doc: Option<&str>,
base: Option<&PyType>,
dict: Option<PyObject>,
) -> PyResult<Py<PyType>> {
Self::new_type_bound(
py,
name,
doc,
base.map(PyNativeType::as_borrowed).as_deref(),
dict,
)
}

/// Creates a new exception type with the given name and docstring.
///
/// - `base` can be an existing exception type to subclass, or a tuple of classes.
Expand Down Expand Up @@ -626,17 +521,6 @@ impl PyErr {
self.is_instance_bound(py, exc.to_object(py).bind(py))
}

/// Deprecated form of `PyErr::is_instance_bound`.
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyErr::is_instance` will be replaced by `PyErr::is_instance_bound` in a future PyO3 version"
)]
#[inline]
pub fn is_instance(&self, py: Python<'_>, ty: &PyAny) -> bool {
self.is_instance_bound(py, &ty.as_borrowed())
}

/// Returns true if the current exception is instance of `T`.
#[inline]
pub fn is_instance_bound(&self, py: Python<'_>, ty: &Bound<'_, PyAny>) -> bool {
Expand All @@ -663,17 +547,6 @@ impl PyErr {
.restore(py)
}

/// Deprecated form of `PyErr::write_unraisable_bound`.
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyErr::write_unraisable` will be replaced by `PyErr::write_unraisable_bound` in a future PyO3 version"
)]
#[inline]
pub fn write_unraisable(self, py: Python<'_>, obj: Option<&PyAny>) {
self.write_unraisable_bound(py, obj.map(PyAny::as_borrowed).as_deref())
}

/// Reports the error as unraisable.
///
/// This calls `sys.unraisablehook()` using the current exception and obj argument.
Expand Down Expand Up @@ -708,16 +581,6 @@ impl PyErr {
unsafe { ffi::PyErr_WriteUnraisable(obj.map_or(std::ptr::null_mut(), Bound::as_ptr)) }
}

/// Deprecated form of [`PyErr::warn_bound`].
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyErr::warn` will be replaced by `PyErr::warn_bound` in a future PyO3 version"
)]
pub fn warn(py: Python<'_>, category: &PyAny, message: &str, stacklevel: i32) -> PyResult<()> {
Self::warn_bound(py, &category.as_borrowed(), message, stacklevel)
}

/// Issues a warning message.
///
/// May return an `Err(PyErr)` if warnings-as-errors is enabled.
Expand Down Expand Up @@ -755,32 +618,6 @@ impl PyErr {
})
}

/// Deprecated form of [`PyErr::warn_explicit_bound`].
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyErr::warn_explicit` will be replaced by `PyErr::warn_explicit_bound` in a future PyO3 version"
)]
pub fn warn_explicit(
py: Python<'_>,
category: &PyAny,
message: &str,
filename: &str,
lineno: i32,
module: Option<&str>,
registry: Option<&PyAny>,
) -> PyResult<()> {
Self::warn_explicit_bound(
py,
&category.as_borrowed(),
message,
filename,
lineno,
module,
registry.map(PyNativeType::as_borrowed).as_deref(),
)
}

/// Issues a warning message, with more control over the warning attributes.
///
/// May return a `PyErr` if warnings-as-errors is enabled.
Expand Down Expand Up @@ -998,29 +835,6 @@ where
}
}

/// Convert `PyDowncastError` to Python `TypeError`.
#[cfg(feature = "gil-refs")]
impl<'a> std::convert::From<PyDowncastError<'a>> for PyErr {
fn from(err: PyDowncastError<'_>) -> PyErr {
let args = PyDowncastErrorArguments {
from: err.from.get_type().into(),
to: err.to,
};

exceptions::PyTypeError::new_err(args)
}
}

#[cfg(feature = "gil-refs")]
impl<'a> std::error::Error for PyDowncastError<'a> {}

#[cfg(feature = "gil-refs")]
impl<'a> std::fmt::Display for PyDowncastError<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
display_downcast_error(f, &self.from.as_borrowed(), &self.to)
}
}

/// Convert `DowncastError` to Python `TypeError`.
impl std::convert::From<DowncastError<'_, '_>> for PyErr {
fn from(err: DowncastError<'_, '_>) -> PyErr {
Expand Down
Loading

0 comments on commit e51251b

Please sign in to comment.