From cf0352ea232f4d0434b68a5db48b8dc5c75f199d Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Thu, 21 Dec 2023 12:51:39 +0000 Subject: [PATCH] make `DowncastError` and `DowncastIntoError` public --- src/err/mod.rs | 34 +++++++++++------------ src/lib.rs | 4 ++- src/types/any.rs | 26 ++++++++--------- tests/ui/invalid_result_conversion.stderr | 4 +-- 4 files changed, 35 insertions(+), 33 deletions(-) diff --git a/src/err/mod.rs b/src/err/mod.rs index d87854e74a9..00ba111f8c5 100644 --- a/src/err/mod.rs +++ b/src/err/mod.rs @@ -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>) -> 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>) -> 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> for PyErr { - fn from(err: PyDowncastError2<'_, '_>) -> PyErr { +/// Convert `DowncastError` to Python `TypeError`. +impl std::convert::From> 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> 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> for PyErr { - fn from(err: PyDowncastIntoError<'_>) -> PyErr { +/// Convert `DowncastIntoError` to Python `TypeError`. +impl std::convert::From> 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> 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) } diff --git a/src/lib.rs b/src/lib.rs index 6742f822499..f70ea01b91f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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}; diff --git a/src/types/any.rs b/src/types/any.rs index 51fdabd4c29..4d82fad617e 100644 --- a/src/types/any.rs +++ b/src/types/any.rs @@ -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(&self) -> Result<&Bound<'py, T>, PyDowncastError2<'_, 'py>> + fn downcast(&self) -> Result<&Bound<'py, T>, DowncastError<'_, 'py>> where T: PyTypeCheck; /// Like `downcast` but takes ownership of `self`. - fn downcast_into(self) -> Result, PyDowncastIntoError<'py>> + fn downcast_into(self) -> Result, DowncastIntoError<'py>> where T: PyTypeCheck; @@ -1600,12 +1600,12 @@ pub trait PyAnyMethods<'py> { /// assert!(any.downcast_exact::().is_ok()); /// }); /// ``` - fn downcast_exact(&self) -> Result<&Bound<'py, T>, PyDowncastError2<'_, 'py>> + fn downcast_exact(&self) -> Result<&Bound<'py, T>, DowncastError<'_, 'py>> where T: PyTypeInfo; /// Like `downcast_exact` but takes ownership of `self`. - fn downcast_into_exact(self) -> Result, PyDowncastIntoError<'py>> + fn downcast_into_exact(self) -> Result, DowncastIntoError<'py>> where T: PyTypeInfo; @@ -2041,7 +2041,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> { } #[inline] - fn downcast(&self) -> Result<&Bound<'py, T>, PyDowncastError2<'_, 'py>> + fn downcast(&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(self) -> Result, PyDowncastIntoError<'py>> + fn downcast_into(self) -> Result, 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(&self) -> Result<&Bound<'py, T>, PyDowncastError2<'_, 'py>> + fn downcast_exact(&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(self) -> Result, PyDowncastIntoError<'py>> + fn downcast_into_exact(self) -> Result, 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)) } } diff --git a/tests/ui/invalid_result_conversion.stderr b/tests/ui/invalid_result_conversion.stderr index 834585ef3f1..1f3dc1a5774 100644 --- a/tests/ui/invalid_result_conversion.stderr +++ b/tests/ui/invalid_result_conversion.stderr @@ -9,8 +9,8 @@ error[E0277]: the trait bound `PyErr: From` is not satisfied > > >> - >> - >> + >> + >> > > and $N others