make `DowncastError` and `DowncastIntoError` public

This commit is contained in:
David Hewitt 2023-12-21 12:51:39 +00:00
parent c08c6c0a41
commit 0f242c399d
4 changed files with 35 additions and 33 deletions

View File

@ -65,16 +65,16 @@ impl<'a> PyDowncastError<'a> {
/// Error that indicates a failure to convert a PyAny to a more specific Python type.
#[derive(Debug)]
pub struct PyDowncastError2<'a, 'py> {
pub struct DowncastError<'a, 'py> {
from: &'a Bound<'py, PyAny>,
to: Cow<'static, str>,
}
impl<'a, 'py> PyDowncastError2<'a, 'py> {
impl<'a, 'py> DowncastError<'a, 'py> {
/// Create a new `PyDowncastError` representing a failure to convert the object
/// `from` into the type named in `to`.
pub fn new(from: &'a Bound<'py, PyAny>, to: impl Into<Cow<'static, str>>) -> Self {
PyDowncastError2 {
DowncastError {
from,
to: to.into(),
}
@ -83,16 +83,16 @@ impl<'a, 'py> PyDowncastError2<'a, 'py> {
/// Error that indicates a failure to convert a PyAny to a more specific Python type.
#[derive(Debug)]
pub struct PyDowncastIntoError<'py> {
pub struct DowncastIntoError<'py> {
from: Bound<'py, PyAny>,
to: Cow<'static, str>,
}
impl<'py> PyDowncastIntoError<'py> {
/// Create a new `PyDowncastIntoError` representing a failure to convert the object
impl<'py> DowncastIntoError<'py> {
/// Create a new `DowncastIntoError` representing a failure to convert the object
/// `from` into the type named in `to`.
pub fn new(from: Bound<'py, PyAny>, to: impl Into<Cow<'static, str>>) -> Self {
PyDowncastIntoError {
DowncastIntoError {
from,
to: to.into(),
}
@ -815,9 +815,9 @@ impl<'a> std::fmt::Display for PyDowncastError<'a> {
}
}
/// Convert `PyDowncastError2` to Python `TypeError`.
impl std::convert::From<PyDowncastError2<'_, '_>> for PyErr {
fn from(err: PyDowncastError2<'_, '_>) -> PyErr {
/// Convert `DowncastError` to Python `TypeError`.
impl std::convert::From<DowncastError<'_, '_>> for PyErr {
fn from(err: DowncastError<'_, '_>) -> PyErr {
let args = PyDowncastErrorArguments {
from: err.from.get_type().into(),
to: err.to,
@ -827,17 +827,17 @@ impl std::convert::From<PyDowncastError2<'_, '_>> for PyErr {
}
}
impl std::error::Error for PyDowncastError2<'_, '_> {}
impl std::error::Error for DowncastError<'_, '_> {}
impl std::fmt::Display for PyDowncastError2<'_, '_> {
impl std::fmt::Display for DowncastError<'_, '_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
display_downcast_error(f, self.from, &self.to)
}
}
/// Convert `PyDowncastIntoError` to Python `TypeError`.
impl std::convert::From<PyDowncastIntoError<'_>> for PyErr {
fn from(err: PyDowncastIntoError<'_>) -> PyErr {
/// Convert `DowncastIntoError` to Python `TypeError`.
impl std::convert::From<DowncastIntoError<'_>> for PyErr {
fn from(err: DowncastIntoError<'_>) -> PyErr {
let args = PyDowncastErrorArguments {
from: err.from.get_type().into(),
to: err.to,
@ -847,9 +847,9 @@ impl std::convert::From<PyDowncastIntoError<'_>> for PyErr {
}
}
impl std::error::Error for PyDowncastIntoError<'_> {}
impl std::error::Error for DowncastIntoError<'_> {}
impl std::fmt::Display for PyDowncastIntoError<'_> {
impl std::fmt::Display for DowncastIntoError<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
display_downcast_error(f, &self.from, &self.to)
}

View File

@ -297,7 +297,9 @@ pub use crate::class::*;
pub use crate::conversion::{AsPyPointer, FromPyObject, FromPyPointer, IntoPy, ToPyObject};
#[allow(deprecated)]
pub use crate::conversion::{PyTryFrom, PyTryInto};
pub use crate::err::{PyDowncastError, PyErr, PyErrArguments, PyResult};
pub use crate::err::{
DowncastError, DowncastIntoError, PyDowncastError, PyErr, PyErrArguments, PyResult,
};
pub use crate::gil::GILPool;
#[cfg(not(PyPy))]
pub use crate::gil::{prepare_freethreaded_python, with_embedded_python_interpreter};

View File

@ -1,6 +1,6 @@
use crate::class::basic::CompareOp;
use crate::conversion::{AsPyPointer, FromPyObject, IntoPy, ToPyObject};
use crate::err::{PyDowncastError, PyDowncastError2, PyDowncastIntoError, PyErr, PyResult};
use crate::err::{DowncastError, DowncastIntoError, PyDowncastError, PyErr, PyResult};
use crate::exceptions::{PyAttributeError, PyTypeError};
use crate::ffi_ptr_ext::FfiPtrExt;
use crate::instance::Bound;
@ -1561,12 +1561,12 @@ pub trait PyAnyMethods<'py> {
/// })
/// # }
/// ```
fn downcast<T>(&self) -> Result<&Bound<'py, T>, PyDowncastError2<'_, 'py>>
fn downcast<T>(&self) -> Result<&Bound<'py, T>, DowncastError<'_, 'py>>
where
T: PyTypeCheck;
/// Like `downcast` but takes ownership of `self`.
fn downcast_into<T>(self) -> Result<Bound<'py, T>, PyDowncastIntoError<'py>>
fn downcast_into<T>(self) -> Result<Bound<'py, T>, DowncastIntoError<'py>>
where
T: PyTypeCheck;
@ -1600,12 +1600,12 @@ pub trait PyAnyMethods<'py> {
/// assert!(any.downcast_exact::<PyBool>().is_ok());
/// });
/// ```
fn downcast_exact<T>(&self) -> Result<&Bound<'py, T>, PyDowncastError2<'_, 'py>>
fn downcast_exact<T>(&self) -> Result<&Bound<'py, T>, DowncastError<'_, 'py>>
where
T: PyTypeInfo;
/// Like `downcast_exact` but takes ownership of `self`.
fn downcast_into_exact<T>(self) -> Result<Bound<'py, T>, PyDowncastIntoError<'py>>
fn downcast_into_exact<T>(self) -> Result<Bound<'py, T>, DowncastIntoError<'py>>
where
T: PyTypeInfo;
@ -2041,7 +2041,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {
}
#[inline]
fn downcast<T>(&self) -> Result<&Bound<'py, T>, PyDowncastError2<'_, 'py>>
fn downcast<T>(&self) -> Result<&Bound<'py, T>, DowncastError<'_, 'py>>
where
T: PyTypeCheck,
{
@ -2049,12 +2049,12 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {
// Safety: type_check is responsible for ensuring that the type is correct
Ok(unsafe { self.downcast_unchecked() })
} else {
Err(PyDowncastError2::new(self, T::NAME))
Err(DowncastError::new(self, T::NAME))
}
}
#[inline]
fn downcast_into<T>(self) -> Result<Bound<'py, T>, PyDowncastIntoError<'py>>
fn downcast_into<T>(self) -> Result<Bound<'py, T>, DowncastIntoError<'py>>
where
T: PyTypeCheck,
{
@ -2062,12 +2062,12 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {
// Safety: type_check is responsible for ensuring that the type is correct
Ok(unsafe { self.downcast_into_unchecked() })
} else {
Err(PyDowncastIntoError::new(self, T::NAME))
Err(DowncastIntoError::new(self, T::NAME))
}
}
#[inline]
fn downcast_exact<T>(&self) -> Result<&Bound<'py, T>, PyDowncastError2<'_, 'py>>
fn downcast_exact<T>(&self) -> Result<&Bound<'py, T>, DowncastError<'_, 'py>>
where
T: PyTypeInfo,
{
@ -2075,12 +2075,12 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {
// Safety: is_exact_instance_of is responsible for ensuring that the type is correct
Ok(unsafe { self.downcast_unchecked() })
} else {
Err(PyDowncastError2::new(self, T::NAME))
Err(DowncastError::new(self, T::NAME))
}
}
#[inline]
fn downcast_into_exact<T>(self) -> Result<Bound<'py, T>, PyDowncastIntoError<'py>>
fn downcast_into_exact<T>(self) -> Result<Bound<'py, T>, DowncastIntoError<'py>>
where
T: PyTypeInfo,
{
@ -2088,7 +2088,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {
// Safety: is_exact_instance_of is responsible for ensuring that the type is correct
Ok(unsafe { self.downcast_into_unchecked() })
} else {
Err(PyDowncastIntoError::new(self, T::NAME))
Err(DowncastIntoError::new(self, T::NAME))
}
}

View File

@ -9,8 +9,8 @@ error[E0277]: the trait bound `PyErr: From<MyError>` is not satisfied
<PyErr as From<PyBorrowMutError>>
<PyErr as From<std::io::Error>>
<PyErr as From<PyDowncastError<'a>>>
<PyErr as From<pyo3::err::PyDowncastError2<'_, '_>>>
<PyErr as From<pyo3::err::PyDowncastIntoError<'_>>>
<PyErr as From<DowncastError<'_, '_>>>
<PyErr as From<DowncastIntoError<'_>>>
<PyErr as From<NulError>>
<PyErr as From<IntoStringError>>
and $N others