Skip to content

Commit 09f3a03

Browse files
committed
Rename exceptions to PyException etc; reintroduce deprecated ones
1 parent a06aad5 commit 09f3a03

30 files changed

+337
-194
lines changed

src/buffer.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,12 @@ pub unsafe trait Element: Copy {
156156
fn validate(b: &ffi::Py_buffer) -> PyResult<()> {
157157
// shape and stride information must be provided when we use PyBUF_FULL_RO
158158
if b.shape.is_null() {
159-
return Err(exceptions::BufferError::py_err("Shape is Null"));
159+
return Err(exceptions::PyBufferError::py_err("Shape is Null"));
160160
}
161161
if b.strides.is_null() {
162-
return Err(exceptions::BufferError::py_err("PyBuffer: Strides is Null"));
162+
return Err(exceptions::PyBufferError::py_err(
163+
"PyBuffer: Strides is Null",
164+
));
163165
}
164166
Ok(())
165167
}
@@ -188,7 +190,7 @@ impl<T: Element> PyBuffer<T> {
188190
{
189191
Ok(buf)
190192
} else {
191-
Err(exceptions::BufferError::py_err(
193+
Err(exceptions::PyBufferError::py_err(
192194
"Incompatible type as buffer",
193195
))
194196
}
@@ -439,7 +441,7 @@ impl<T: Element> PyBuffer<T> {
439441

440442
fn copy_to_slice_impl(&self, py: Python, target: &mut [T], fort: u8) -> PyResult<()> {
441443
if mem::size_of_val(target) != self.len_bytes() {
442-
return Err(exceptions::BufferError::py_err(
444+
return Err(exceptions::PyBufferError::py_err(
443445
"Slice length does not match buffer length.",
444446
));
445447
}
@@ -526,7 +528,7 @@ impl<T: Element> PyBuffer<T> {
526528
return buffer_readonly_error();
527529
}
528530
if mem::size_of_val(source) != self.len_bytes() {
529-
return Err(exceptions::BufferError::py_err(
531+
return Err(exceptions::PyBufferError::py_err(
530532
"Slice length does not match buffer length.",
531533
));
532534
}
@@ -562,7 +564,7 @@ impl<T: Element> PyBuffer<T> {
562564

563565
#[inline(always)]
564566
fn buffer_readonly_error() -> PyResult<()> {
565-
Err(exceptions::BufferError::py_err(
567+
Err(exceptions::PyBufferError::py_err(
566568
"Cannot write to read-only buffer.",
567569
))
568570
}

src/callback.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! Utilities for a Python callable object that invokes a Rust function.
44
55
use crate::err::PyResult;
6-
use crate::exceptions::OverflowError;
6+
use crate::exceptions::PyOverflowError;
77
use crate::ffi::{self, Py_hash_t};
88
use crate::IntoPyPointer;
99
use crate::{IntoPy, PyObject, Python};
@@ -85,7 +85,7 @@ impl IntoPyCallbackOutput<ffi::Py_ssize_t> for usize {
8585
if self <= (isize::MAX as usize) {
8686
Ok(self as isize)
8787
} else {
88-
Err(OverflowError::py_err(()))
88+
Err(PyOverflowError::py_err(()))
8989
}
9090
}
9191
}

src/class/basic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ where
260260
ffi::Py_NE => Ok(CompareOp::Ne),
261261
ffi::Py_GT => Ok(CompareOp::Gt),
262262
ffi::Py_GE => Ok(CompareOp::Ge),
263-
_ => Err(PyErr::new::<exceptions::ValueError, _>(
263+
_ => Err(PyErr::new::<exceptions::PyValueError, _>(
264264
"tp_richcompare called with invalid comparison operator",
265265
)),
266266
}

src/class/iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl IntoPyCallbackOutput<*mut ffi::PyObject> for PyIterNextOutput {
112112
fn convert(self, _py: Python) -> PyResult<*mut ffi::PyObject> {
113113
match self {
114114
IterNextOutput::Yield(o) => Ok(o.into_ptr()),
115-
IterNextOutput::Return(opt) => Err(crate::exceptions::StopIteration::py_err((opt,))),
115+
IterNextOutput::Return(opt) => Err(crate::exceptions::PyStopIteration::py_err((opt,))),
116116
}
117117
}
118118
}

src/class/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ macro_rules! py_func_set {
302302
let slf = py.from_borrowed_ptr::<$crate::PyCell<$generic>>(slf);
303303

304304
if value.is_null() {
305-
Err($crate::PyErr::new::<exceptions::NotImplementedError, _>(
305+
Err($crate::PyErr::new::<exceptions::PyNotImplementedError, _>(
306306
format!(
307307
"Subscript deletion not supported by {:?}",
308308
stringify!($generic)
@@ -338,7 +338,7 @@ macro_rules! py_func_del {
338338
.extract()?;
339339
slf.try_borrow_mut()?.$fn_del(name).convert(py)
340340
} else {
341-
Err(PyErr::new::<exceptions::NotImplementedError, _>(
341+
Err(PyErr::new::<exceptions::PyNotImplementedError, _>(
342342
"Subscript assignment not supported",
343343
))
344344
}

src/class/pyasync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl IntoPyCallbackOutput<*mut ffi::PyObject> for PyIterANextOutput {
119119
match self {
120120
IterANextOutput::Yield(o) => Ok(o.into_ptr()),
121121
IterANextOutput::Return(opt) => {
122-
Err(crate::exceptions::StopAsyncIteration::py_err((opt,)))
122+
Err(crate::exceptions::PyStopAsyncIteration::py_err((opt,)))
123123
}
124124
}
125125
}

src/class/sequence.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ mod sq_ass_item_impl {
223223
let slf = py.from_borrowed_ptr::<PyCell<T>>(slf);
224224

225225
if value.is_null() {
226-
return Err(PyErr::new::<exceptions::NotImplementedError, _>(format!(
226+
return Err(PyErr::new::<exceptions::PyNotImplementedError, _>(format!(
227227
"Item deletion is not supported by {:?}",
228228
stringify!(T)
229229
)));
@@ -256,7 +256,7 @@ mod sq_ass_item_impl {
256256
if value.is_null() {
257257
crate::callback::convert(py, slf.borrow_mut().__delitem__(key.into()))
258258
} else {
259-
Err(PyErr::new::<exceptions::NotImplementedError, _>(format!(
259+
Err(PyErr::new::<exceptions::PyNotImplementedError, _>(format!(
260260
"Item assignment not supported by {:?}",
261261
stringify!(T)
262262
)))

src/derive_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! Functionality for the code generated by the derive backend
66
77
use crate::err::{PyErr, PyResult};
8-
use crate::exceptions::TypeError;
8+
use crate::exceptions::PyTypeError;
99
use crate::instance::PyNativeType;
1010
use crate::pyclass::{PyClass, PyClassThreadChecker};
1111
use crate::types::{PyAny, PyDict, PyModule, PyTuple};
@@ -43,7 +43,7 @@ pub fn parse_fn_args<'p>(
4343
let nargs = args.len();
4444
let mut used_args = 0;
4545
macro_rules! raise_error {
46-
($s: expr $(,$arg:expr)*) => (return Err(TypeError::py_err(format!(
46+
($s: expr $(,$arg:expr)*) => (return Err(PyTypeError::py_err(format!(
4747
concat!("{} ", $s), fname.unwrap_or("function") $(,$arg)*
4848
))))
4949
}

src/err.rs

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ impl PyErr {
7575
///
7676
/// Example:
7777
/// ```ignore
78-
/// return Err(PyErr::new::<exceptions::TypeError, _>("Error message"));
78+
/// return Err(PyErr::new::<exceptions::PyTypeError, _>("Error message"));
7979
/// ```
8080
///
8181
/// In most cases, you can use a concrete exception's constructors instead:
8282
/// the example is equivalent to
8383
/// ```ignore
84-
/// return Err(exceptions::TypeError::py_err("Error message"));
85-
/// return exceptions::TypeError::into("Error message");
84+
/// return Err(exceptions::PyTypeError::py_err("Error message"));
85+
/// return exceptions::PyTypeError::into("Error message");
8686
/// ```
8787
pub fn new<T, V>(value: V) -> PyErr
8888
where
@@ -105,7 +105,7 @@ impl PyErr {
105105
/// Constructs a new error, with the usual lazy initialization of Python exceptions.
106106
///
107107
/// `exc` is the exception type; usually one of the standard exceptions
108-
/// like `exceptions::RuntimeError`.
108+
/// like `exceptions::PyRuntimeError`.
109109
/// `args` is the a tuple of arguments to pass to the exception constructor.
110110
pub fn from_type<A>(exc: &PyType, args: A) -> PyErr
111111
where
@@ -161,7 +161,7 @@ impl PyErr {
161161
}
162162
} else {
163163
PyErr {
164-
ptype: exceptions::TypeError::type_object(obj.py()).into(),
164+
ptype: exceptions::PyTypeError::type_object(obj.py()).into(),
165165
pvalue: PyErrValue::ToObject(Box::new("exceptions must derive from BaseException")),
166166
ptraceback: None,
167167
}
@@ -257,7 +257,7 @@ impl PyErr {
257257
};
258258

259259
let ptype = if ptype.is_null() {
260-
<exceptions::SystemError as PyTypeObject>::type_object(py).into()
260+
<exceptions::PySystemError as PyTypeObject>::type_object(py).into()
261261
} else {
262262
Py::from_owned_ptr(py, ptype)
263263
};
@@ -344,7 +344,7 @@ impl PyErr {
344344
///
345345
/// This method takes `mut self` because the error might need
346346
/// to be normalized in order to create the exception instance.
347-
pub fn instance(mut self, py: Python) -> &exceptions::BaseException {
347+
pub fn instance(mut self, py: Python) -> &exceptions::PyBaseException {
348348
self.normalize(py);
349349
match self.pvalue {
350350
PyErrValue::Value(ref instance) => {
@@ -439,7 +439,7 @@ impl FromPy<PyErr> for PyObject {
439439
}
440440
}
441441

442-
impl FromPy<PyErr> for Py<exceptions::BaseException> {
442+
impl FromPy<PyErr> for Py<exceptions::PyBaseException> {
443443
fn from_py(other: PyErr, py: Python) -> Self {
444444
other.instance(py).into()
445445
}
@@ -462,7 +462,7 @@ impl<'a> IntoPy<PyObject> for &'a PyErr {
462462
/// Convert `PyDowncastError` to Python `TypeError`.
463463
impl std::convert::From<PyDowncastError> for PyErr {
464464
fn from(_err: PyDowncastError) -> PyErr {
465-
exceptions::TypeError::py_err(())
465+
exceptions::PyTypeError::py_err(())
466466
}
467467
}
468468

@@ -515,28 +515,30 @@ impl std::convert::From<io::Error> for PyErr {
515515
}
516516
match err.kind() {
517517
io::ErrorKind::BrokenPipe => {
518-
PyErr::from_value::<exceptions::BrokenPipeError>(err_value!())
518+
PyErr::from_value::<exceptions::PyBrokenPipeError>(err_value!())
519519
}
520520
io::ErrorKind::ConnectionRefused => {
521-
PyErr::from_value::<exceptions::ConnectionRefusedError>(err_value!())
521+
PyErr::from_value::<exceptions::PyConnectionRefusedError>(err_value!())
522522
}
523523
io::ErrorKind::ConnectionAborted => {
524-
PyErr::from_value::<exceptions::ConnectionAbortedError>(err_value!())
524+
PyErr::from_value::<exceptions::PyConnectionAbortedError>(err_value!())
525525
}
526526
io::ErrorKind::ConnectionReset => {
527-
PyErr::from_value::<exceptions::ConnectionResetError>(err_value!())
527+
PyErr::from_value::<exceptions::PyConnectionResetError>(err_value!())
528528
}
529529
io::ErrorKind::Interrupted => {
530-
PyErr::from_value::<exceptions::InterruptedError>(err_value!())
530+
PyErr::from_value::<exceptions::PyInterruptedError>(err_value!())
531531
}
532532
io::ErrorKind::NotFound => {
533-
PyErr::from_value::<exceptions::FileNotFoundError>(err_value!())
533+
PyErr::from_value::<exceptions::PyFileNotFoundError>(err_value!())
534534
}
535535
io::ErrorKind::WouldBlock => {
536-
PyErr::from_value::<exceptions::BlockingIOError>(err_value!())
536+
PyErr::from_value::<exceptions::PyBlockingIOError>(err_value!())
537537
}
538-
io::ErrorKind::TimedOut => PyErr::from_value::<exceptions::TimeoutError>(err_value!()),
539-
_ => PyErr::from_value::<exceptions::OSError>(err_value!()),
538+
io::ErrorKind::TimedOut => {
539+
PyErr::from_value::<exceptions::PyTimeoutError>(err_value!())
540+
}
541+
_ => PyErr::from_value::<exceptions::PyOSError>(err_value!()),
540542
}
541543
}
542544
}
@@ -549,7 +551,7 @@ impl PyErrArguments for io::Error {
549551

550552
impl<W: 'static + Send + std::fmt::Debug> std::convert::From<std::io::IntoInnerError<W>> for PyErr {
551553
fn from(err: std::io::IntoInnerError<W>) -> PyErr {
552-
PyErr::from_value::<exceptions::OSError>(PyErrValue::from_err_args(err))
554+
PyErr::from_value::<exceptions::PyOSError>(PyErrValue::from_err_args(err))
553555
}
554556
}
555557

@@ -567,22 +569,28 @@ impl PyErrArguments for std::convert::Infallible {
567569

568570
impl std::convert::From<std::convert::Infallible> for PyErr {
569571
fn from(_: std::convert::Infallible) -> PyErr {
570-
PyErr::new::<exceptions::ValueError, _>("Infalliable!")
572+
PyErr::new::<exceptions::PyValueError, _>("Infalliable!")
571573
}
572574
}
573575

574-
impl_to_pyerr!(std::array::TryFromSliceError, exceptions::ValueError);
575-
impl_to_pyerr!(std::num::ParseIntError, exceptions::ValueError);
576-
impl_to_pyerr!(std::num::ParseFloatError, exceptions::ValueError);
577-
impl_to_pyerr!(std::num::TryFromIntError, exceptions::ValueError);
578-
impl_to_pyerr!(std::str::ParseBoolError, exceptions::ValueError);
579-
impl_to_pyerr!(std::ffi::IntoStringError, exceptions::UnicodeDecodeError);
580-
impl_to_pyerr!(std::ffi::NulError, exceptions::ValueError);
581-
impl_to_pyerr!(std::str::Utf8Error, exceptions::UnicodeDecodeError);
582-
impl_to_pyerr!(std::string::FromUtf8Error, exceptions::UnicodeDecodeError);
583-
impl_to_pyerr!(std::string::FromUtf16Error, exceptions::UnicodeDecodeError);
584-
impl_to_pyerr!(std::char::DecodeUtf16Error, exceptions::UnicodeDecodeError);
585-
impl_to_pyerr!(std::net::AddrParseError, exceptions::ValueError);
576+
impl_to_pyerr!(std::array::TryFromSliceError, exceptions::PyValueError);
577+
impl_to_pyerr!(std::num::ParseIntError, exceptions::PyValueError);
578+
impl_to_pyerr!(std::num::ParseFloatError, exceptions::PyValueError);
579+
impl_to_pyerr!(std::num::TryFromIntError, exceptions::PyValueError);
580+
impl_to_pyerr!(std::str::ParseBoolError, exceptions::PyValueError);
581+
impl_to_pyerr!(std::ffi::IntoStringError, exceptions::PyUnicodeDecodeError);
582+
impl_to_pyerr!(std::ffi::NulError, exceptions::PyValueError);
583+
impl_to_pyerr!(std::str::Utf8Error, exceptions::PyUnicodeDecodeError);
584+
impl_to_pyerr!(std::string::FromUtf8Error, exceptions::PyUnicodeDecodeError);
585+
impl_to_pyerr!(
586+
std::string::FromUtf16Error,
587+
exceptions::PyUnicodeDecodeError
588+
);
589+
impl_to_pyerr!(
590+
std::char::DecodeUtf16Error,
591+
exceptions::PyUnicodeDecodeError
592+
);
593+
impl_to_pyerr!(std::net::AddrParseError, exceptions::PyValueError);
586594

587595
pub fn panic_after_error(_py: Python) -> ! {
588596
unsafe {
@@ -611,7 +619,7 @@ mod tests {
611619
fn set_typeerror() {
612620
let gil = Python::acquire_gil();
613621
let py = gil.python();
614-
let err: PyErr = exceptions::TypeError::py_err(());
622+
let err: PyErr = exceptions::PyTypeError::py_err(());
615623
err.restore(py);
616624
assert!(PyErr::occurred(py));
617625
drop(PyErr::fetch(py));

0 commit comments

Comments
 (0)