diff --git a/src/buffer.rs b/src/buffer.rs index fd004568b6b..087df0ceef5 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -156,10 +156,12 @@ pub unsafe trait Element: Copy { fn validate(b: &ffi::Py_buffer) -> PyResult<()> { // shape and stride information must be provided when we use PyBUF_FULL_RO if b.shape.is_null() { - return Err(exceptions::BufferError::py_err("Shape is Null")); + return Err(exceptions::PyBufferError::py_err("Shape is Null")); } if b.strides.is_null() { - return Err(exceptions::BufferError::py_err("PyBuffer: Strides is Null")); + return Err(exceptions::PyBufferError::py_err( + "PyBuffer: Strides is Null", + )); } Ok(()) } @@ -188,7 +190,7 @@ impl PyBuffer { { Ok(buf) } else { - Err(exceptions::BufferError::py_err( + Err(exceptions::PyBufferError::py_err( "Incompatible type as buffer", )) } @@ -439,7 +441,7 @@ impl PyBuffer { fn copy_to_slice_impl(&self, py: Python, target: &mut [T], fort: u8) -> PyResult<()> { if mem::size_of_val(target) != self.len_bytes() { - return Err(exceptions::BufferError::py_err( + return Err(exceptions::PyBufferError::py_err( "Slice length does not match buffer length.", )); } @@ -526,7 +528,7 @@ impl PyBuffer { return buffer_readonly_error(); } if mem::size_of_val(source) != self.len_bytes() { - return Err(exceptions::BufferError::py_err( + return Err(exceptions::PyBufferError::py_err( "Slice length does not match buffer length.", )); } @@ -562,7 +564,7 @@ impl PyBuffer { #[inline(always)] fn buffer_readonly_error() -> PyResult<()> { - Err(exceptions::BufferError::py_err( + Err(exceptions::PyBufferError::py_err( "Cannot write to read-only buffer.", )) } diff --git a/src/callback.rs b/src/callback.rs index 0220436f65a..7dbd1aff822 100644 --- a/src/callback.rs +++ b/src/callback.rs @@ -3,7 +3,7 @@ //! Utilities for a Python callable object that invokes a Rust function. use crate::err::PyResult; -use crate::exceptions::OverflowError; +use crate::exceptions::PyOverflowError; use crate::ffi::{self, Py_hash_t}; use crate::IntoPyPointer; use crate::{IntoPy, PyObject, Python}; @@ -85,7 +85,7 @@ impl IntoPyCallbackOutput for usize { if self <= (isize::MAX as usize) { Ok(self as isize) } else { - Err(OverflowError::py_err(())) + Err(PyOverflowError::py_err(())) } } } diff --git a/src/class/basic.rs b/src/class/basic.rs index f7dd4a54a15..efb45212d05 100644 --- a/src/class/basic.rs +++ b/src/class/basic.rs @@ -260,7 +260,7 @@ where ffi::Py_NE => Ok(CompareOp::Ne), ffi::Py_GT => Ok(CompareOp::Gt), ffi::Py_GE => Ok(CompareOp::Ge), - _ => Err(PyErr::new::( + _ => Err(PyErr::new::( "tp_richcompare called with invalid comparison operator", )), } diff --git a/src/class/iter.rs b/src/class/iter.rs index ccf84044559..0818feb0a44 100644 --- a/src/class/iter.rs +++ b/src/class/iter.rs @@ -112,7 +112,7 @@ impl IntoPyCallbackOutput<*mut ffi::PyObject> for PyIterNextOutput { fn convert(self, _py: Python) -> PyResult<*mut ffi::PyObject> { match self { IterNextOutput::Yield(o) => Ok(o.into_ptr()), - IterNextOutput::Return(opt) => Err(crate::exceptions::StopIteration::py_err((opt,))), + IterNextOutput::Return(opt) => Err(crate::exceptions::PyStopIteration::py_err((opt,))), } } } diff --git a/src/class/macros.rs b/src/class/macros.rs index c56fe97ed9f..fcb20a0a9ec 100644 --- a/src/class/macros.rs +++ b/src/class/macros.rs @@ -302,7 +302,7 @@ macro_rules! py_func_set { let slf = py.from_borrowed_ptr::<$crate::PyCell<$generic>>(slf); if value.is_null() { - Err($crate::PyErr::new::( + Err($crate::PyErr::new::( format!( "Subscript deletion not supported by {:?}", stringify!($generic) @@ -338,7 +338,7 @@ macro_rules! py_func_del { .extract()?; slf.try_borrow_mut()?.$fn_del(name).convert(py) } else { - Err(PyErr::new::( + Err(PyErr::new::( "Subscript assignment not supported", )) } diff --git a/src/class/pyasync.rs b/src/class/pyasync.rs index 7986adf69bc..82548835c7f 100644 --- a/src/class/pyasync.rs +++ b/src/class/pyasync.rs @@ -119,7 +119,7 @@ impl IntoPyCallbackOutput<*mut ffi::PyObject> for PyIterANextOutput { match self { IterANextOutput::Yield(o) => Ok(o.into_ptr()), IterANextOutput::Return(opt) => { - Err(crate::exceptions::StopAsyncIteration::py_err((opt,))) + Err(crate::exceptions::PyStopAsyncIteration::py_err((opt,))) } } } diff --git a/src/class/sequence.rs b/src/class/sequence.rs index e85edf02db5..a9c62f03d0b 100644 --- a/src/class/sequence.rs +++ b/src/class/sequence.rs @@ -223,7 +223,7 @@ mod sq_ass_item_impl { let slf = py.from_borrowed_ptr::>(slf); if value.is_null() { - return Err(PyErr::new::(format!( + return Err(PyErr::new::(format!( "Item deletion is not supported by {:?}", stringify!(T) ))); @@ -256,7 +256,7 @@ mod sq_ass_item_impl { if value.is_null() { crate::callback::convert(py, slf.borrow_mut().__delitem__(key.into())) } else { - Err(PyErr::new::(format!( + Err(PyErr::new::(format!( "Item assignment not supported by {:?}", stringify!(T) ))) diff --git a/src/derive_utils.rs b/src/derive_utils.rs index c10c172cf18..2a736d7ebcd 100644 --- a/src/derive_utils.rs +++ b/src/derive_utils.rs @@ -5,7 +5,7 @@ //! Functionality for the code generated by the derive backend use crate::err::{PyErr, PyResult}; -use crate::exceptions::TypeError; +use crate::exceptions::PyTypeError; use crate::instance::PyNativeType; use crate::pyclass::{PyClass, PyClassThreadChecker}; use crate::types::{PyAny, PyDict, PyModule, PyTuple}; @@ -43,7 +43,7 @@ pub fn parse_fn_args<'p>( let nargs = args.len(); let mut used_args = 0; macro_rules! raise_error { - ($s: expr $(,$arg:expr)*) => (return Err(TypeError::py_err(format!( + ($s: expr $(,$arg:expr)*) => (return Err(PyTypeError::py_err(format!( concat!("{} ", $s), fname.unwrap_or("function") $(,$arg)* )))) } diff --git a/src/err.rs b/src/err.rs index b1ad5ad5270..839ca0c5e92 100644 --- a/src/err.rs +++ b/src/err.rs @@ -75,14 +75,14 @@ impl PyErr { /// /// Example: /// ```ignore - /// return Err(PyErr::new::("Error message")); + /// return Err(PyErr::new::("Error message")); /// ``` /// /// In most cases, you can use a concrete exception's constructors instead: /// the example is equivalent to /// ```ignore - /// return Err(exceptions::TypeError::py_err("Error message")); - /// return exceptions::TypeError::into("Error message"); + /// return Err(exceptions::PyTypeError::py_err("Error message")); + /// return exceptions::PyTypeError::into("Error message"); /// ``` pub fn new(value: V) -> PyErr where @@ -105,7 +105,7 @@ impl PyErr { /// Constructs a new error, with the usual lazy initialization of Python exceptions. /// /// `exc` is the exception type; usually one of the standard exceptions - /// like `exceptions::RuntimeError`. + /// like `exceptions::PyRuntimeError`. /// `args` is the a tuple of arguments to pass to the exception constructor. pub fn from_type(exc: &PyType, args: A) -> PyErr where @@ -161,7 +161,7 @@ impl PyErr { } } else { PyErr { - ptype: exceptions::TypeError::type_object(obj.py()).into(), + ptype: exceptions::PyTypeError::type_object(obj.py()).into(), pvalue: PyErrValue::ToObject(Box::new("exceptions must derive from BaseException")), ptraceback: None, } @@ -257,7 +257,7 @@ impl PyErr { }; let ptype = if ptype.is_null() { - ::type_object(py).into() + ::type_object(py).into() } else { Py::from_owned_ptr(py, ptype) }; @@ -344,7 +344,7 @@ impl PyErr { /// /// This method takes `mut self` because the error might need /// to be normalized in order to create the exception instance. - pub fn instance(mut self, py: Python) -> &exceptions::BaseException { + pub fn instance(mut self, py: Python) -> &exceptions::PyBaseException { self.normalize(py); match self.pvalue { PyErrValue::Value(ref instance) => { @@ -439,7 +439,7 @@ impl FromPy for PyObject { } } -impl FromPy for Py { +impl FromPy for Py { fn from_py(other: PyErr, py: Python) -> Self { other.instance(py).into() } @@ -462,7 +462,7 @@ impl<'a> IntoPy for &'a PyErr { /// Convert `PyDowncastError` to Python `TypeError`. impl std::convert::From for PyErr { fn from(_err: PyDowncastError) -> PyErr { - exceptions::TypeError::py_err(()) + exceptions::PyTypeError::py_err(()) } } @@ -515,28 +515,30 @@ impl std::convert::From for PyErr { } match err.kind() { io::ErrorKind::BrokenPipe => { - PyErr::from_value::(err_value!()) + PyErr::from_value::(err_value!()) } io::ErrorKind::ConnectionRefused => { - PyErr::from_value::(err_value!()) + PyErr::from_value::(err_value!()) } io::ErrorKind::ConnectionAborted => { - PyErr::from_value::(err_value!()) + PyErr::from_value::(err_value!()) } io::ErrorKind::ConnectionReset => { - PyErr::from_value::(err_value!()) + PyErr::from_value::(err_value!()) } io::ErrorKind::Interrupted => { - PyErr::from_value::(err_value!()) + PyErr::from_value::(err_value!()) } io::ErrorKind::NotFound => { - PyErr::from_value::(err_value!()) + PyErr::from_value::(err_value!()) } io::ErrorKind::WouldBlock => { - PyErr::from_value::(err_value!()) + PyErr::from_value::(err_value!()) } - io::ErrorKind::TimedOut => PyErr::from_value::(err_value!()), - _ => PyErr::from_value::(err_value!()), + io::ErrorKind::TimedOut => { + PyErr::from_value::(err_value!()) + } + _ => PyErr::from_value::(err_value!()), } } } @@ -549,7 +551,7 @@ impl PyErrArguments for io::Error { impl std::convert::From> for PyErr { fn from(err: std::io::IntoInnerError) -> PyErr { - PyErr::from_value::(PyErrValue::from_err_args(err)) + PyErr::from_value::(PyErrValue::from_err_args(err)) } } @@ -567,22 +569,28 @@ impl PyErrArguments for std::convert::Infallible { impl std::convert::From for PyErr { fn from(_: std::convert::Infallible) -> PyErr { - PyErr::new::("Infalliable!") + PyErr::new::("Infalliable!") } } -impl_to_pyerr!(std::array::TryFromSliceError, exceptions::ValueError); -impl_to_pyerr!(std::num::ParseIntError, exceptions::ValueError); -impl_to_pyerr!(std::num::ParseFloatError, exceptions::ValueError); -impl_to_pyerr!(std::num::TryFromIntError, exceptions::ValueError); -impl_to_pyerr!(std::str::ParseBoolError, exceptions::ValueError); -impl_to_pyerr!(std::ffi::IntoStringError, exceptions::UnicodeDecodeError); -impl_to_pyerr!(std::ffi::NulError, exceptions::ValueError); -impl_to_pyerr!(std::str::Utf8Error, exceptions::UnicodeDecodeError); -impl_to_pyerr!(std::string::FromUtf8Error, exceptions::UnicodeDecodeError); -impl_to_pyerr!(std::string::FromUtf16Error, exceptions::UnicodeDecodeError); -impl_to_pyerr!(std::char::DecodeUtf16Error, exceptions::UnicodeDecodeError); -impl_to_pyerr!(std::net::AddrParseError, exceptions::ValueError); +impl_to_pyerr!(std::array::TryFromSliceError, exceptions::PyValueError); +impl_to_pyerr!(std::num::ParseIntError, exceptions::PyValueError); +impl_to_pyerr!(std::num::ParseFloatError, exceptions::PyValueError); +impl_to_pyerr!(std::num::TryFromIntError, exceptions::PyValueError); +impl_to_pyerr!(std::str::ParseBoolError, exceptions::PyValueError); +impl_to_pyerr!(std::ffi::IntoStringError, exceptions::PyUnicodeDecodeError); +impl_to_pyerr!(std::ffi::NulError, exceptions::PyValueError); +impl_to_pyerr!(std::str::Utf8Error, exceptions::PyUnicodeDecodeError); +impl_to_pyerr!(std::string::FromUtf8Error, exceptions::PyUnicodeDecodeError); +impl_to_pyerr!( + std::string::FromUtf16Error, + exceptions::PyUnicodeDecodeError +); +impl_to_pyerr!( + std::char::DecodeUtf16Error, + exceptions::PyUnicodeDecodeError +); +impl_to_pyerr!(std::net::AddrParseError, exceptions::PyValueError); pub fn panic_after_error(_py: Python) -> ! { unsafe { @@ -611,7 +619,7 @@ mod tests { fn set_typeerror() { let gil = Python::acquire_gil(); let py = gil.python(); - let err: PyErr = exceptions::TypeError::py_err(()); + let err: PyErr = exceptions::PyTypeError::py_err(()); err.restore(py); assert!(PyErr::occurred(py)); drop(PyErr::fetch(py)); diff --git a/src/exceptions.rs b/src/exceptions.rs index c2ffa860c21..b6d23289b3f 100644 --- a/src/exceptions.rs +++ b/src/exceptions.rs @@ -2,13 +2,54 @@ //! Exception types defined by Python. -use crate::types::{PyAny, PyTuple}; use crate::type_object::PySizedLayout; +use crate::types::{PyAny, PyTuple}; use crate::{ffi, AsPyPointer, PyResult, Python}; use std::ffi::CStr; use std::ops; use std::os::raw::c_char; +/// The boilerplate to convert between a Rust type and a Python exception. +#[macro_export] +macro_rules! impl_legacy_native_exception { + ($name:ident, $legacy_name:ident) => { + #[deprecated(note = "Exceptions now have a `Py` prefix, e.g. `PyException`, `PyTypeError`")] + pub struct $legacy_name; + + #[allow(deprecated)] + impl ::std::convert::From<$legacy_name> for $crate::PyErr { + fn from(_err: $legacy_name) -> $crate::PyErr { + $name::py_err(()) + } + } + + #[allow(deprecated)] + impl ::std::convert::Into<$crate::PyResult> for $legacy_name { + fn into(self) -> $crate::PyResult { + Err($name::py_err(())) + } + } + + #[allow(deprecated)] + impl $legacy_name { + pub fn py_err(args: T) -> $crate::PyErr { + $crate::PyErr::new::(args) + } + + pub fn into(args: T) -> $crate::PyResult { + $crate::PyErr::new::(args).into() + } + } + + #[allow(deprecated)] + unsafe impl $crate::type_object::PyTypeObject for $legacy_name { + fn type_object(py: $crate::Python) -> &crate::types::PyType { + <$name as $crate::type_object::PyTypeObject>::type_object(py) + } + } + }; +} + /// The boilerplate to convert between a Rust type and a Python exception. #[macro_export] macro_rules! impl_exception_boilerplate { @@ -65,7 +106,7 @@ macro_rules! impl_exception_boilerplate { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { unsafe { use $crate::{AsPyPointer, PyNativeType}; - let cause: &$crate::exceptions::BaseException = self + let cause: &$crate::exceptions::PyBaseException = self .py() .from_owned_ptr_or_opt($crate::ffi::PyException_GetCause(self.as_ptr()))?; @@ -132,7 +173,7 @@ macro_rules! import_exception { unsafe fn check(ptr: *mut $crate::ffi::PyObject) -> $crate::libc::c_int { $crate::ffi::PyObject_TypeCheck( ptr, - Self::type_object_raw($crate::Python::assume_gil_acquired()) as *mut _ + Self::type_object_raw($crate::Python::assume_gil_acquired()) as *mut _, ) } @@ -171,16 +212,16 @@ macro_rules! import_exception { /// /// * `module` is the name of the containing module. /// * `MyError` is the name of the new exception type. -/// * `BaseException` is the superclass of `MyError`, usually `pyo3::exceptions::Exception`. +/// * `BaseException` is the superclass of `MyError`, usually `pyo3::exceptions::PyException`. /// /// # Example /// ``` /// use pyo3::prelude::*; /// use pyo3::create_exception; /// use pyo3::types::IntoPyDict; -/// use pyo3::exceptions::Exception; +/// use pyo3::exceptions::PyException; /// -/// create_exception!(mymodule, CustomError, Exception); +/// create_exception!(mymodule, CustomError, PyException); /// /// fn main() { /// let gil = Python::acquire_gil(); @@ -235,7 +276,7 @@ macro_rules! create_exception_type_object { unsafe fn check(ptr: *mut $crate::ffi::PyObject) -> $crate::libc::c_int { $crate::ffi::PyObject_TypeCheck( ptr, - Self::type_object_raw($crate::Python::assume_gil_acquired()) as *mut _ + Self::type_object_raw($crate::Python::assume_gil_acquired()) as *mut _, ) } @@ -265,9 +306,10 @@ macro_rules! create_exception_type_object { } macro_rules! impl_native_exception ( - ($name:ident, $exc_name:ident, $layout:path) => ( + ($name:ident, $legacy_name:ident, $exc_name:ident, $layout:path) => ( pub struct $name($crate::PyAny); + $crate::impl_legacy_native_exception!($name, $legacy_name); $crate::impl_exception_boilerplate!($name); impl $name { @@ -282,80 +324,168 @@ macro_rules! impl_native_exception ( $crate::pyobject_native_type_core!($name, $layout, *(ffi::$exc_name as *mut ffi::PyTypeObject), Some("builtins"), $name::check); ); - ($name:ident, $exc_name:ident) => ( - impl_native_exception!($name, $exc_name, ffi::PyBaseExceptionObject); + ($name:ident, $legacy_name:ident, $exc_name:ident) => ( + impl_native_exception!($name, $legacy_name, $exc_name, ffi::PyBaseExceptionObject); ) ); -impl PySizedLayout for ffi::PyBaseExceptionObject {} +impl PySizedLayout for ffi::PyBaseExceptionObject {} -impl_native_exception!(BaseException, PyExc_BaseException); -impl_native_exception!(Exception, PyExc_Exception); -impl_native_exception!(StopAsyncIteration, PyExc_StopAsyncIteration); +impl_native_exception!(PyBaseException, BaseException, PyExc_BaseException); +impl_native_exception!(PyException, Exception, PyExc_Exception); impl_native_exception!( + PyStopAsyncIteration, + StopAsyncIteration, + PyExc_StopAsyncIteration +); +impl_native_exception!( + PyStopIteration, StopIteration, PyExc_StopIteration, ffi::PyStopIterationObject ); -impl_native_exception!(GeneratorExit, PyExc_GeneratorExit); -impl_native_exception!(ArithmeticError, PyExc_ArithmeticError); -impl_native_exception!(LookupError, PyExc_LookupError); - -impl_native_exception!(AssertionError, PyExc_AssertionError); -impl_native_exception!(AttributeError, PyExc_AttributeError); -impl_native_exception!(BufferError, PyExc_BufferError); -impl_native_exception!(EOFError, PyExc_EOFError); -impl_native_exception!(FloatingPointError, PyExc_FloatingPointError); -impl_native_exception!(OSError, PyExc_OSError, ffi::PyOSErrorObject); -impl_native_exception!(ImportError, PyExc_ImportError); +impl_native_exception!(PyGeneratorExit, GeneratorExit, PyExc_GeneratorExit); +impl_native_exception!(PyArithmeticError, ArithmeticError, PyExc_ArithmeticError); +impl_native_exception!(PyLookupError, LookupError, PyExc_LookupError); + +impl_native_exception!(PyAssertionError, AssertionError, PyExc_AssertionError); +impl_native_exception!(PyAttributeError, AttributeError, PyExc_AttributeError); +impl_native_exception!(PyBufferError, BufferError, PyExc_BufferError); +impl_native_exception!(PyEOFError, EOFError, PyExc_EOFError); +impl_native_exception!( + PyFloatingPointError, + FloatingPointError, + PyExc_FloatingPointError +); +impl_native_exception!(PyOSError, OSError, PyExc_OSError, ffi::PyOSErrorObject); +impl_native_exception!(PyImportError, ImportError, PyExc_ImportError); #[cfg(Py_3_6)] -impl_native_exception!(ModuleNotFoundError, PyExc_ModuleNotFoundError); - -impl_native_exception!(IndexError, PyExc_IndexError); -impl_native_exception!(KeyError, PyExc_KeyError); -impl_native_exception!(KeyboardInterrupt, PyExc_KeyboardInterrupt); -impl_native_exception!(MemoryError, PyExc_MemoryError); -impl_native_exception!(NameError, PyExc_NameError); -impl_native_exception!(OverflowError, PyExc_OverflowError); -impl_native_exception!(RuntimeError, PyExc_RuntimeError); -impl_native_exception!(RecursionError, PyExc_RecursionError); -impl_native_exception!(NotImplementedError, PyExc_NotImplementedError); -impl_native_exception!(SyntaxError, PyExc_SyntaxError, ffi::PySyntaxErrorObject); -impl_native_exception!(ReferenceError, PyExc_ReferenceError); -impl_native_exception!(SystemError, PyExc_SystemError); -impl_native_exception!(SystemExit, PyExc_SystemExit, ffi::PySystemExitObject); -impl_native_exception!(TypeError, PyExc_TypeError); -impl_native_exception!(UnboundLocalError, PyExc_UnboundLocalError); -impl_native_exception!(UnicodeError, PyExc_UnicodeError, ffi::PyUnicodeErrorObject); -impl_native_exception!(UnicodeDecodeError, PyExc_UnicodeDecodeError); -impl_native_exception!(UnicodeEncodeError, PyExc_UnicodeEncodeError); -impl_native_exception!(UnicodeTranslateError, PyExc_UnicodeTranslateError); -impl_native_exception!(ValueError, PyExc_ValueError); -impl_native_exception!(ZeroDivisionError, PyExc_ZeroDivisionError); - -impl_native_exception!(BlockingIOError, PyExc_BlockingIOError); -impl_native_exception!(BrokenPipeError, PyExc_BrokenPipeError); -impl_native_exception!(ChildProcessError, PyExc_ChildProcessError); -impl_native_exception!(ConnectionError, PyExc_ConnectionError); -impl_native_exception!(ConnectionAbortedError, PyExc_ConnectionAbortedError); -impl_native_exception!(ConnectionRefusedError, PyExc_ConnectionRefusedError); -impl_native_exception!(ConnectionResetError, PyExc_ConnectionResetError); -impl_native_exception!(FileExistsError, PyExc_FileExistsError); -impl_native_exception!(FileNotFoundError, PyExc_FileNotFoundError); -impl_native_exception!(InterruptedError, PyExc_InterruptedError); -impl_native_exception!(IsADirectoryError, PyExc_IsADirectoryError); -impl_native_exception!(NotADirectoryError, PyExc_NotADirectoryError); -impl_native_exception!(PermissionError, PyExc_PermissionError); -impl_native_exception!(ProcessLookupError, PyExc_ProcessLookupError); -impl_native_exception!(TimeoutError, PyExc_TimeoutError); - -impl_native_exception!(EnvironmentError, PyExc_EnvironmentError); -impl_native_exception!(IOError, PyExc_IOError); +impl_native_exception!( + PyModuleNotFoundError, + ModuleNotFoundError, + PyExc_ModuleNotFoundError +); + +impl_native_exception!(PyIndexError, IndexError, PyExc_IndexError); +impl_native_exception!(PyKeyError, KeyError, PyExc_KeyError); +impl_native_exception!( + PyKeyboardInterrupt, + KeyboardInterrupt, + PyExc_KeyboardInterrupt +); +impl_native_exception!(PyMemoryError, MemoryError, PyExc_MemoryError); +impl_native_exception!(PyNameError, NameError, PyExc_NameError); +impl_native_exception!(PyOverflowError, OverflowError, PyExc_OverflowError); +impl_native_exception!(PyRuntimeError, RuntimeError, PyExc_RuntimeError); +impl_native_exception!(PyRecursionError, RecursionError, PyExc_RecursionError); +impl_native_exception!( + PyNotImplementedError, + NotImplementedError, + PyExc_NotImplementedError +); +impl_native_exception!( + PySyntaxError, + SyntaxError, + PyExc_SyntaxError, + ffi::PySyntaxErrorObject +); +impl_native_exception!(PyReferenceError, ReferenceError, PyExc_ReferenceError); +impl_native_exception!(PySystemError, SystemError, PyExc_SystemError); +impl_native_exception!( + PySystemExit, + SystemExit, + PyExc_SystemExit, + ffi::PySystemExitObject +); +impl_native_exception!(PyTypeError, TypeError, PyExc_TypeError); +impl_native_exception!( + PyUnboundLocalError, + UnboundLocalError, + PyExc_UnboundLocalError +); +impl_native_exception!( + PyUnicodeError, + UnicodeError, + PyExc_UnicodeError, + ffi::PyUnicodeErrorObject +); +impl_native_exception!( + PyUnicodeDecodeError, + UnicodeDecodeError, + PyExc_UnicodeDecodeError +); +impl_native_exception!( + PyUnicodeEncodeError, + UnicodeEncodeError, + PyExc_UnicodeEncodeError +); +impl_native_exception!( + PyUnicodeTranslateError, + UnicodeTranslateError, + PyExc_UnicodeTranslateError +); +impl_native_exception!(PyValueError, ValueError, PyExc_ValueError); +impl_native_exception!( + PyZeroDivisionError, + ZeroDivisionError, + PyExc_ZeroDivisionError +); + +impl_native_exception!(PyBlockingIOError, BlockingIOError, PyExc_BlockingIOError); +impl_native_exception!(PyBrokenPipeError, BrokenPipeError, PyExc_BrokenPipeError); +impl_native_exception!( + PyChildProcessError, + ChildProcessError, + PyExc_ChildProcessError +); +impl_native_exception!(PyConnectionError, ConnectionError, PyExc_ConnectionError); +impl_native_exception!( + PyConnectionAbortedError, + ConnectionAbortedError, + PyExc_ConnectionAbortedError +); +impl_native_exception!( + PyConnectionRefusedError, + ConnectionRefusedError, + PyExc_ConnectionRefusedError +); +impl_native_exception!( + PyConnectionResetError, + ConnectionResetError, + PyExc_ConnectionResetError +); +impl_native_exception!(PyFileExistsError, FileExistsError, PyExc_FileExistsError); +impl_native_exception!( + PyFileNotFoundError, + FileNotFoundError, + PyExc_FileNotFoundError +); +impl_native_exception!(PyInterruptedError, InterruptedError, PyExc_InterruptedError); +impl_native_exception!( + PyIsADirectoryError, + IsADirectoryError, + PyExc_IsADirectoryError +); +impl_native_exception!( + PyNotADirectoryError, + NotADirectoryError, + PyExc_NotADirectoryError +); +impl_native_exception!(PyPermissionError, PermissionError, PyExc_PermissionError); +impl_native_exception!( + PyProcessLookupError, + ProcessLookupError, + PyExc_ProcessLookupError +); +impl_native_exception!(PyTimeoutError, TimeoutError, PyExc_TimeoutError); + +impl_native_exception!(PyEnvironmentError, EnvironmentError, PyExc_EnvironmentError); +impl_native_exception!(PyIOError, IOError, PyExc_IOError); #[cfg(target_os = "windows")] -impl_native_exception!(WindowsError, PyExc_WindowsError); +impl_native_exception!(PyWindowsError, WindowsError, PyExc_WindowsError); -impl UnicodeDecodeError { +impl PyUnicodeDecodeError { pub fn new_err<'p>( py: Python<'p>, encoding: &CStr, @@ -383,7 +513,7 @@ impl UnicodeDecodeError { err: std::str::Utf8Error, ) -> PyResult<&'p PyAny> { let pos = err.valid_up_to(); - UnicodeDecodeError::new_err( + PyUnicodeDecodeError::new_err( py, CStr::from_bytes_with_nul(b"utf-8\0").unwrap(), input, @@ -393,7 +523,7 @@ impl UnicodeDecodeError { } } -impl StopIteration { +impl PyStopIteration { pub fn stop_iteration(_py: Python, args: &PyTuple) { unsafe { ffi::PyErr_SetObject( @@ -424,7 +554,7 @@ pub mod socket { #[cfg(test)] mod test { - use crate::exceptions::Exception; + use crate::exceptions::PyException; use crate::types::{IntoPyDict, PyDict}; use crate::{AsPyPointer, PyErr, Python}; use std::error::Error; @@ -487,7 +617,7 @@ mod test { #[test] fn custom_exception() { - create_exception!(mymodule, CustomError, Exception); + create_exception!(mymodule, CustomError, PyException); let gil = Python::acquire_gil(); let py = gil.python(); @@ -536,7 +666,7 @@ mod test { write!(&mut out, "{}", err).expect("successful format"); assert_eq!(out, "Exception: banana"); out.clear(); - let convert_ref: &super::BaseException = + let convert_ref: &super::PyBaseException = unsafe { &*(err.as_ptr() as *const _ as *const _) }; let source = convert_ref.source().expect("cause should exist"); write!(&mut out, "{}", source).expect("successful format"); diff --git a/src/instance.rs b/src/instance.rs index dc612428c87..3052288a4ed 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -384,13 +384,14 @@ where impl std::error::Error for Py where T: std::error::Error + PyTypeInfo, - T::AsRefTarget: std::fmt::Display -{ } + T::AsRefTarget: std::fmt::Display, +{ +} impl std::fmt::Display for Py where T: PyTypeInfo, - T::AsRefTarget: std::fmt::Display + T::AsRefTarget: std::fmt::Display, { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { let gil = Python::acquire_gil(); diff --git a/src/panic.rs b/src/panic.rs index f6096606914..4884278d4c8 100644 --- a/src/panic.rs +++ b/src/panic.rs @@ -1,5 +1,4 @@ -use crate::exceptions::BaseException; - +use crate::exceptions::PyBaseException; pyo3_exception!( " @@ -10,5 +9,5 @@ pyo3_exception!( Python interpreter to exit. ", PanicException, - BaseException -); \ No newline at end of file + PyBaseException +); diff --git a/src/pycell.rs b/src/pycell.rs index b711876f1cf..83d7d73ad73 100644 --- a/src/pycell.rs +++ b/src/pycell.rs @@ -1,7 +1,7 @@ //! Includes `PyCell` implementation. use crate::conversion::{AsPyPointer, FromPyPointer, ToPyObject}; +use crate::exceptions::PyRuntimeError; use crate::pyclass::{PyClass, PyClassThreadChecker}; -use crate::exceptions::RuntimeError; use crate::pyclass_init::PyClassInitializer; use crate::pyclass_slots::{PyClassDict, PyClassWeakRef}; use crate::type_object::{PyBorrowFlagLayout, PyLayout, PySizedLayout, PyTypeInfo}; @@ -708,7 +708,7 @@ impl fmt::Display for PyBorrowError { impl From for PyErr { fn from(other: PyBorrowError) -> Self { - RuntimeError::py_err(other.to_string()) + PyRuntimeError::py_err(other.to_string()) } } @@ -733,6 +733,6 @@ impl fmt::Display for PyBorrowMutError { impl From for PyErr { fn from(other: PyBorrowMutError) -> Self { - RuntimeError::py_err(other.to_string()) + PyRuntimeError::py_err(other.to_string()) } } diff --git a/src/python.rs b/src/python.rs index 081166dd62c..c6bffaf9630 100644 --- a/src/python.rs +++ b/src/python.rs @@ -76,7 +76,7 @@ impl<'p> Python<'p> { /// # Example /// ``` /// # use pyo3::prelude::*; use pyo3::types::IntoPyDict; use pyo3::wrap_pyfunction; - /// use pyo3::exceptions::RuntimeError; + /// use pyo3::exceptions::PyRuntimeError; /// use std::sync::Arc; /// use std::thread; /// #[pyfunction] @@ -89,7 +89,7 @@ impl<'p> Python<'p> { /// .collect(); /// let mut sum = 0; /// for t in threads { - /// sum += t.join().map_err(|_| PyErr::new::(()))?; + /// sum += t.join().map_err(|_| PyErr::new::(()))?; /// } /// Ok(sum) /// }) diff --git a/src/types/any.rs b/src/types/any.rs index 3539f299c74..c42313be189 100644 --- a/src/types/any.rs +++ b/src/types/any.rs @@ -3,7 +3,7 @@ use crate::conversion::{ AsPyPointer, FromPyObject, IntoPy, IntoPyPointer, PyTryFrom, ToBorrowedObject, ToPyObject, }; use crate::err::{PyDowncastError, PyErr, PyResult}; -use crate::exceptions::TypeError; +use crate::exceptions::PyTypeError; use crate::types::{PyDict, PyIterator, PyList, PyString, PyTuple, PyType}; use crate::{err, ffi, Py, PyNativeType, PyObject}; use libc::c_int; @@ -163,7 +163,7 @@ impl PyAny { } else if do_compare(other, ffi::Py_GT)? { Ok(Ordering::Greater) } else { - Err(TypeError::py_err( + Err(PyTypeError::py_err( "PyAny::compare(): All comparisons returned false", )) } diff --git a/src/types/bytearray.rs b/src/types/bytearray.rs index c468479fe9c..5ddbbee5dd0 100644 --- a/src/types/bytearray.rs +++ b/src/types/bytearray.rs @@ -210,7 +210,7 @@ mod test { let py = gil.python(); if let Err(err) = PyByteArray::from(py, &py.None()) { - assert!(err.is_instance::(py)); + assert!(err.is_instance::(py)); } else { panic!("error"); } diff --git a/src/types/module.rs b/src/types/module.rs index f586ee220d9..1f499d0221d 100644 --- a/src/types/module.rs +++ b/src/types/module.rs @@ -82,7 +82,7 @@ impl PyModule { match self.getattr("__all__") { Ok(idx) => idx.downcast().map_err(PyErr::from), Err(err) => { - if err.is_instance::(self.py()) { + if err.is_instance::(self.py()) { let l = PyList::empty(self.py()); self.setattr("__all__", l).map_err(PyErr::from)?; Ok(l) @@ -101,7 +101,7 @@ impl PyModule { match str::from_utf8(slice) { Ok(s) => Ok(s), Err(e) => Err(PyErr::from_instance( - exceptions::UnicodeDecodeError::new_utf8(self.py(), slice, e)?, + exceptions::PyUnicodeDecodeError::new_utf8(self.py(), slice, e)?, )), } } diff --git a/src/types/num.rs b/src/types/num.rs index 9b2e786a5e4..e3cc3aceec8 100644 --- a/src/types/num.rs +++ b/src/types/num.rs @@ -40,7 +40,7 @@ macro_rules! int_fits_larger_int { fn extract(obj: &'source PyAny) -> PyResult { let val: $larger_type = obj.extract()?; <$rust_type>::try_from(val) - .map_err(|e| exceptions::OverflowError::py_err(e.to_string())) + .map_err(|e| exceptions::PyOverflowError::py_err(e.to_string())) } } }; @@ -145,7 +145,7 @@ macro_rules! int_fits_c_long { } }?; <$rust_type>::try_from(val) - .map_err(|e| exceptions::OverflowError::py_err(e.to_string())) + .map_err(|e| exceptions::PyOverflowError::py_err(e.to_string())) } } }; @@ -551,7 +551,7 @@ mod test { ); let obj = PyObject::from_owned_ptr_or_panic(py, obj); let err = obj.extract::(py).unwrap_err(); - assert!(err.is_instance::(py)); + assert!(err.is_instance::(py)); } } @@ -569,7 +569,7 @@ mod test { let obj = ("123").to_object(py); let err = obj.extract::<$t>(py).unwrap_err(); - assert!(err.is_instance::(py)); + assert!(err.is_instance::(py)); } #[test] @@ -579,7 +579,7 @@ mod test { let obj = (12.3).to_object(py); let err = obj.extract::<$t>(py).unwrap_err(); - assert!(err.is_instance::(py)); + assert!(err.is_instance::(py)); } #[test] diff --git a/src/types/sequence.rs b/src/types/sequence.rs index 97dd819e2fc..dc02da80d29 100644 --- a/src/types/sequence.rs +++ b/src/types/sequence.rs @@ -361,7 +361,7 @@ where { let seq = ::try_from(obj)?; if seq.len()? as usize != slice.len() { - return Err(exceptions::BufferError::py_err( + return Err(exceptions::PyBufferError::py_err( "Slice length does not match buffer length.", )); } diff --git a/src/types/tuple.rs b/src/types/tuple.rs index 39c7aeca800..046adf6eef1 100644 --- a/src/types/tuple.rs +++ b/src/types/tuple.rs @@ -141,7 +141,7 @@ fn wrong_tuple_length(t: &PyTuple, expected_length: usize) -> PyErr { expected_length, t.len() ); - exceptions::ValueError::py_err(msg) + exceptions::PyValueError::py_err(msg) } macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+} => { diff --git a/tests/test_arithmetics.rs b/tests/test_arithmetics.rs index d0d5357d5da..d86783aa64f 100644 --- a/tests/test_arithmetics.rs +++ b/tests/test_arithmetics.rs @@ -404,22 +404,22 @@ fn rich_comparisons_python_3_type_error() { let py = gil.python(); let c2 = PyCell::new(py, RichComparisons2 {}).unwrap(); - py_expect_exception!(py, c2, "c2 < c2", TypeError); - py_expect_exception!(py, c2, "c2 < 1", TypeError); - py_expect_exception!(py, c2, "1 < c2", TypeError); - py_expect_exception!(py, c2, "c2 <= c2", TypeError); - py_expect_exception!(py, c2, "c2 <= 1", TypeError); - py_expect_exception!(py, c2, "1 <= c2", TypeError); + py_expect_exception!(py, c2, "c2 < c2", PyTypeError); + py_expect_exception!(py, c2, "c2 < 1", PyTypeError); + py_expect_exception!(py, c2, "1 < c2", PyTypeError); + py_expect_exception!(py, c2, "c2 <= c2", PyTypeError); + py_expect_exception!(py, c2, "c2 <= 1", PyTypeError); + py_expect_exception!(py, c2, "1 <= c2", PyTypeError); py_run!(py, c2, "assert (c2 == c2) == True"); py_run!(py, c2, "assert (c2 == 1) == True"); py_run!(py, c2, "assert (1 == c2) == True"); py_run!(py, c2, "assert (c2 != c2) == False"); py_run!(py, c2, "assert (c2 != 1) == False"); py_run!(py, c2, "assert (1 != c2) == False"); - py_expect_exception!(py, c2, "c2 > c2", TypeError); - py_expect_exception!(py, c2, "c2 > 1", TypeError); - py_expect_exception!(py, c2, "1 > c2", TypeError); - py_expect_exception!(py, c2, "c2 >= c2", TypeError); - py_expect_exception!(py, c2, "c2 >= 1", TypeError); - py_expect_exception!(py, c2, "1 >= c2", TypeError); + py_expect_exception!(py, c2, "c2 > c2", PyTypeError); + py_expect_exception!(py, c2, "c2 > 1", PyTypeError); + py_expect_exception!(py, c2, "1 > c2", PyTypeError); + py_expect_exception!(py, c2, "c2 >= c2", PyTypeError); + py_expect_exception!(py, c2, "c2 >= 1", PyTypeError); + py_expect_exception!(py, c2, "1 >= c2", PyTypeError); } diff --git a/tests/test_buffer_protocol.rs b/tests/test_buffer_protocol.rs index 93903b1fc5c..c7343e19331 100644 --- a/tests/test_buffer_protocol.rs +++ b/tests/test_buffer_protocol.rs @@ -1,6 +1,6 @@ use pyo3::buffer::PyBuffer; use pyo3::class::PyBufferProtocol; -use pyo3::exceptions::BufferError; +use pyo3::exceptions::PyBufferError; use pyo3::ffi; use pyo3::prelude::*; use pyo3::types::IntoPyDict; @@ -21,11 +21,11 @@ struct TestBufferClass { impl PyBufferProtocol for TestBufferClass { fn bf_getbuffer(slf: PyRefMut, view: *mut ffi::Py_buffer, flags: c_int) -> PyResult<()> { if view.is_null() { - return Err(BufferError::py_err("View is null")); + return Err(PyBufferError::py_err("View is null")); } if (flags & ffi::PyBUF_WRITABLE) == ffi::PyBUF_WRITABLE { - return Err(BufferError::py_err("Object is not writable")); + return Err(PyBufferError::py_err("Object is not writable")); } unsafe { diff --git a/tests/test_class_attributes.rs b/tests/test_class_attributes.rs index a2e4fe8264f..752935d76e5 100644 --- a/tests/test_class_attributes.rs +++ b/tests/test_class_attributes.rs @@ -56,7 +56,7 @@ fn class_attributes_are_immutable() { let gil = Python::acquire_gil(); let py = gil.python(); let foo_obj = py.get_type::(); - py_expect_exception!(py, foo_obj, "foo_obj.a = 6", TypeError); + py_expect_exception!(py, foo_obj, "foo_obj.a = 6", PyTypeError); } #[pymethods] diff --git a/tests/test_dunder.rs b/tests/test_dunder.rs index f3799b033fa..7b1f9a52508 100644 --- a/tests/test_dunder.rs +++ b/tests/test_dunder.rs @@ -2,7 +2,7 @@ use pyo3::class::{ PyAsyncProtocol, PyContextProtocol, PyDescrProtocol, PyIterProtocol, PyMappingProtocol, PyObjectProtocol, PySequenceProtocol, }; -use pyo3::exceptions::{IndexError, ValueError}; +use pyo3::exceptions::{PyIndexError, PyValueError}; use pyo3::prelude::*; use pyo3::types::{IntoPyDict, PyBytes, PySlice, PyType}; use pyo3::{ffi, py_run, AsPyPointer, PyCell}; @@ -42,7 +42,7 @@ fn len() { }, ) .unwrap(); - py_expect_exception!(py, inst, "len(inst)", OverflowError); + py_expect_exception!(py, inst, "len(inst)", PyOverflowError); } #[pyclass] @@ -171,7 +171,7 @@ impl PySequenceProtocol for Sequence { if let Some(s) = self.fields.get(idx) { Ok(s.clone()) } else { - Err(PyErr::new::(())) + Err(PyErr::new::(())) } } @@ -181,7 +181,7 @@ impl PySequenceProtocol for Sequence { *elem = value; Ok(()) } else { - Err(PyErr::new::(())) + Err(PyErr::new::(())) } } } @@ -202,7 +202,7 @@ fn sequence() { assert c[0] == 'H' "# ); - py_expect_exception!(py, c, "c['abc']", TypeError); + py_expect_exception!(py, c, "c['abc']", PyTypeError); } #[pyclass] @@ -256,7 +256,7 @@ fn setitem() { assert_eq!(c.key, 1); assert_eq!(c.val, 2); } - py_expect_exception!(py, c, "del c[1]", NotImplementedError); + py_expect_exception!(py, c, "del c[1]", PyNotImplementedError); } #[pyclass] @@ -282,7 +282,7 @@ fn delitem() { let c = c.borrow(); assert_eq!(c.key, 1); } - py_expect_exception!(py, c, "c[1] = 2", NotImplementedError); + py_expect_exception!(py, c, "c[1] = 2", PyNotImplementedError); } #[pyclass] @@ -354,7 +354,7 @@ fn contains() { let c = Py::new(py, Contains {}).unwrap(); py_run!(py, c, "assert 1 in c"); py_run!(py, c, "assert -1 not in c"); - py_expect_exception!(py, c, "assert 'wrong type' not in c", TypeError); + py_expect_exception!(py, c, "assert 'wrong type' not in c", PyTypeError); } #[pyclass] @@ -376,7 +376,7 @@ impl<'p> PyContextProtocol<'p> for ContextManager { ) -> bool { let gil = GILGuard::acquire(); self.exit_called = true; - ty == Some(gil.python().get_type::()) + ty == Some(gil.python().get_type::()) } } @@ -402,7 +402,7 @@ fn context_manager() { py, c, "with c as x: raise NotImplementedError", - NotImplementedError + PyNotImplementedError ); let c = c.borrow(); assert!(c.exit_called); @@ -438,7 +438,7 @@ impl<'p> PyMappingProtocol<'p> for Test { return Ok("int".into_py(gil.python())); } } - Err(PyErr::new::("error")) + Err(PyErr::new::("error")) } } diff --git a/tests/test_exceptions.rs b/tests/test_exceptions.rs index 20428b3015d..3726dfb7e39 100644 --- a/tests/test_exceptions.rs +++ b/tests/test_exceptions.rs @@ -46,7 +46,7 @@ impl fmt::Display for CustomError { impl std::convert::From for PyErr { fn from(err: CustomError) -> PyErr { - exceptions::OSError::py_err(err.to_string()) + exceptions::PyOSError::py_err(err.to_string()) } } diff --git a/tests/test_inheritance.rs b/tests/test_inheritance.rs index d63d91549af..4c346563e87 100644 --- a/tests/test_inheritance.rs +++ b/tests/test_inheritance.rs @@ -100,7 +100,10 @@ fn mutation_fails() { let e = py .run("obj.base_set(lambda: obj.sub_set_and_ret(1))", global, None) .unwrap_err(); - assert_eq!(&e.instance(py).to_string(), "RuntimeError: Already borrowed") + assert_eq!( + &e.instance(py).to_string(), + "RuntimeError: Already borrowed" + ) } #[pyclass] diff --git a/tests/test_mapping.rs b/tests/test_mapping.rs index 41bdb029c55..35369e01b4e 100644 --- a/tests/test_mapping.rs +++ b/tests/test_mapping.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use pyo3::exceptions::KeyError; +use pyo3::exceptions::PyKeyError; use pyo3::prelude::*; use pyo3::types::IntoPyDict; use pyo3::types::PyList; @@ -40,7 +40,7 @@ impl PyMappingProtocol for Mapping { self.index .get(&query) .copied() - .ok_or_else(|| KeyError::py_err("unknown key")) + .ok_or_else(|| PyKeyError::py_err("unknown key")) } fn __setitem__(&mut self, key: String, value: usize) { @@ -49,7 +49,7 @@ impl PyMappingProtocol for Mapping { fn __delitem__(&mut self, key: String) -> PyResult<()> { if self.index.remove(&key).is_none() { - KeyError::py_err("unknown key").into() + PyKeyError::py_err("unknown key").into() } else { Ok(()) } diff --git a/tests/test_methods.rs b/tests/test_methods.rs index da473fccc6a..20292fdae49 100644 --- a/tests/test_methods.rs +++ b/tests/test_methods.rs @@ -273,7 +273,7 @@ fn meth_args() { py_run!(py, inst, "assert inst.get_default() == 10"); py_run!(py, inst, "assert inst.get_default(100) == 100"); py_run!(py, inst, "assert inst.get_kwarg() == 10"); - py_expect_exception!(py, inst, "inst.get_kwarg(100)", TypeError); + py_expect_exception!(py, inst, "inst.get_kwarg(100)", PyTypeError); py_run!(py, inst, "assert inst.get_kwarg(test=100) == 100"); py_run!(py, inst, "assert inst.get_kwargs() == [(), None]"); py_run!(py, inst, "assert inst.get_kwargs(1,2,3) == [(1,2,3), None]"); @@ -300,9 +300,9 @@ fn meth_args() { "assert inst.get_pos_arg_kw(1, b=2) == [1, (), {'b': 2}]" ); py_run!(py, inst, "assert inst.get_pos_arg_kw(a=1) == [1, (), None]"); - py_expect_exception!(py, inst, "inst.get_pos_arg_kw()", TypeError); - py_expect_exception!(py, inst, "inst.get_pos_arg_kw(1, a=1)", TypeError); - py_expect_exception!(py, inst, "inst.get_pos_arg_kw(b=2)", TypeError); + py_expect_exception!(py, inst, "inst.get_pos_arg_kw()", PyTypeError); + py_expect_exception!(py, inst, "inst.get_pos_arg_kw(1, a=1)", PyTypeError); + py_expect_exception!(py, inst, "inst.get_pos_arg_kw(b=2)", PyTypeError); py_run!(py, inst, "assert inst.get_pos_arg_kw_sep1(1) == 6"); py_run!(py, inst, "assert inst.get_pos_arg_kw_sep1(1, 2) == 6"); @@ -311,7 +311,7 @@ fn meth_args() { inst, "assert inst.get_pos_arg_kw_sep1(1, 2, c=13) == 16" ); - py_expect_exception!(py, inst, "inst.get_pos_arg_kw_sep1(1, 2, 3)", TypeError); + py_expect_exception!(py, inst, "inst.get_pos_arg_kw_sep1(1, 2, 3)", PyTypeError); py_run!(py, inst, "assert inst.get_pos_arg_kw_sep2(1) == 6"); py_run!( @@ -319,10 +319,10 @@ fn meth_args() { inst, "assert inst.get_pos_arg_kw_sep2(1, b=12, c=13) == 26" ); - py_expect_exception!(py, inst, "inst.get_pos_arg_kw_sep2(1, 2)", TypeError); + py_expect_exception!(py, inst, "inst.get_pos_arg_kw_sep2(1, 2)", PyTypeError); py_run!(py, inst, "assert inst.get_pos_kw(1, b=2) == [1, {'b': 2}]"); - py_expect_exception!(py, inst, "inst.get_pos_kw(1,2)", TypeError); + py_expect_exception!(py, inst, "inst.get_pos_kw(1,2)", PyTypeError); py_run!(py, inst, "assert inst.args_as_vec(1,2,3) == 6"); } diff --git a/tests/test_pyfunction.rs b/tests/test_pyfunction.rs index 825670f2bc2..e8e95bdf332 100644 --- a/tests/test_pyfunction.rs +++ b/tests/test_pyfunction.rs @@ -47,7 +47,7 @@ a = array.array("i", [0, 1, 2, 3]) b = array.array("I", [0, 1, 2, 3]) f(a, b) "#, - BufferError + PyBufferError ); pyo3::py_run!( diff --git a/tests/test_sequence.rs b/tests/test_sequence.rs index 1b4ffc4827d..d46edfb3b45 100644 --- a/tests/test_sequence.rs +++ b/tests/test_sequence.rs @@ -1,5 +1,5 @@ use pyo3::class::PySequenceProtocol; -use pyo3::exceptions::{IndexError, ValueError}; +use pyo3::exceptions::{PyIndexError, PyValueError}; use pyo3::prelude::*; use pyo3::types::{IntoPyDict, PyList}; @@ -41,7 +41,7 @@ impl PySequenceProtocol for ByteSequence { self.elements .get(idx as usize) .copied() - .ok_or_else(|| IndexError::py_err("list index out of range")) + .ok_or_else(|| PyIndexError::py_err("list index out of range")) } fn __setitem__(&mut self, idx: isize, value: u8) { @@ -53,7 +53,7 @@ impl PySequenceProtocol for ByteSequence { self.elements.remove(idx as usize); Ok(()) } else { - Err(IndexError::py_err("list index out of range")) + Err(PyIndexError::py_err("list index out of range")) } } @@ -78,7 +78,7 @@ impl PySequenceProtocol for ByteSequence { } Ok(Self { elements }) } else { - Err(ValueError::py_err("invalid repeat count")) + Err(PyValueError::py_err("invalid repeat count")) } } } @@ -242,7 +242,7 @@ impl PySequenceProtocol for OptionList { fn __getitem__(&self, idx: isize) -> PyResult> { match self.items.get(idx as usize) { Some(x) => Ok(*x), - None => Err(PyErr::new::("Index out of bounds")), + None => Err(PyIndexError::py_err("Index out of bounds")), } } } @@ -263,5 +263,5 @@ fn test_option_list_get() { py_assert!(py, list, "list[0] == 1"); py_assert!(py, list, "list[1] == None"); - py_expect_exception!(py, list, "list[2]", IndexError); + py_expect_exception!(py, list, "list[2]", PyIndexError); }