[][src]Struct pyo3::prelude::PyErr

pub struct PyErr {
    pub ptype: Py<PyType>,
    pub pvalue: PyErrValue,
    pub ptraceback: Option<PyObject>,
}

Represents a Python exception that was raised.

Fields

ptype: Py<PyType>

The type of the exception. This should be either a PyClass or a PyType.

pvalue: PyErrValue

The value of the exception.

This can be either an instance of PyObject, a tuple of arguments to be passed to ptype's constructor, or a single argument to be passed to ptype's constructor. Call PyErr::to_object() to get the exception instance in all cases.

ptraceback: Option<PyObject>

The PyTraceBack object associated with the error.

Implementations

impl PyErr[src]

pub fn new<T, V>(value: V) -> PyErr where
    T: PyTypeObject,
    V: ToPyObject + 'static, 
[src]

Creates a new PyErr of type T.

value can be:

  • a tuple: the exception instance will be created using Python T(*tuple)
  • any other value: the exception instance will be created using Python T(value)

Panics if T is not a Python class derived from BaseException.

Example:

This example is not tested
return Err(PyErr::new::<exceptions::TypeError, _>("Error message"));

In most cases, you can use a concrete exception's constructors instead: the example is equivalent to

This example is not tested
return Err(exceptions::TypeError::py_err("Error message"));
return exceptions::TypeError::into("Error message");

pub fn from_type<A>(exc: &PyType, args: A) -> PyErr where
    A: ToPyObject + 'static, 
[src]

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. args is the a tuple of arguments to pass to the exception constructor.

pub fn from_value<T>(value: PyErrValue) -> PyErr where
    T: PyTypeObject
[src]

Creates a new PyErr of type T.

pub fn from_instance(obj: &PyAny) -> PyErr[src]

Creates a new PyErr.

obj must be an Python exception instance, the PyErr will use that instance. If obj is a Python exception type object, the PyErr will (lazily) create a new instance of that type. Otherwise, a TypeError is created instead.

pub fn occurred(_: Python<'_>) -> bool[src]

Gets whether an error is present in the Python interpreter's global state.

pub fn fetch(py: Python<'_>) -> PyErr[src]

Retrieves the current error from the Python interpreter's global state.

The error is cleared from the Python interpreter. If no error is set, returns a SystemError.

If the error fetched is a PanicException (which would have originated from a panic in a pyo3 callback) then this function will resume the panic.

pub fn new_type<'p>(
    _: Python<'p>,
    name: &str,
    base: Option<&PyType>,
    dict: Option<PyObject>
) -> NonNull<PyTypeObject>
[src]

Creates a new exception type with the given name, which must be of the form <module>.<ExceptionName>, as required by PyErr_NewException.

base can be an existing exception type to subclass, or a tuple of classes dict specifies an optional dictionary of class variables and methods

pub fn print(self, py: Python<'_>)[src]

Prints a standard traceback to sys.stderr.

pub fn print_and_set_sys_last_vars(self, py: Python<'_>)[src]

Prints a standard traceback to sys.stderr, and sets sys.last_{type,value,traceback} attributes to this exception's data.

pub fn matches<T>(&self, py: Python<'_>, exc: T) -> bool where
    T: ToBorrowedObject
[src]

Returns true if the current exception matches the exception in exc.

If exc is a class object, this also returns true when self is an instance of a subclass. If exc is a tuple, all exceptions in the tuple (and recursively in subtuples) are searched for a match.

pub fn is_instance<T>(&self, py: Python<'_>) -> bool where
    T: PyTypeObject
[src]

Returns true if the current exception is instance of T.

pub fn normalize(&mut self, py: Python<'_>)[src]

Normalizes the error. This ensures that the exception value is an instance of the exception type.

pub fn restore(self, py: Python<'_>)[src]

Writes the error back to the Python interpreter's global state. This is the opposite of PyErr::fetch().

pub fn warn(
    py: Python<'_>,
    category: &PyAny,
    message: &str,
    stacklevel: i32
) -> PyResult<()>
[src]

Issues a warning message. May return a PyErr if warnings-as-errors is enabled.

pub fn clone_ref(&self, py: Python<'_>) -> PyErr[src]

Trait Implementations

impl Debug for PyErr[src]

impl From<AddrParseError> for PyErr[src]

impl From<ArithmeticError> for PyErr[src]

impl From<AssertionError> for PyErr[src]

impl From<AttributeError> for PyErr[src]

impl From<BaseException> for PyErr[src]

impl From<BlockingIOError> for PyErr[src]

impl From<BrokenPipeError> for PyErr[src]

impl From<BufferError> for PyErr[src]

impl From<CancelledError> for PyErr[src]

impl From<ChildProcessError> for PyErr[src]

impl From<ConnectionAbortedError> for PyErr[src]

impl From<ConnectionError> for PyErr[src]

impl From<ConnectionRefusedError> for PyErr[src]

impl From<ConnectionResetError> for PyErr[src]

impl From<DecodeUtf16Error> for PyErr[src]

impl From<EOFError> for PyErr[src]

impl From<EnvironmentError> for PyErr[src]

impl From<Error> for PyErr[src]

Create OSError from io::Error

impl From<Exception> for PyErr[src]

impl From<FileExistsError> for PyErr[src]

impl From<FileNotFoundError> for PyErr[src]

impl From<FloatingPointError> for PyErr[src]

impl From<FromUtf16Error> for PyErr[src]

impl From<FromUtf8Error> for PyErr[src]

impl From<GeneratorExit> for PyErr[src]

impl From<IOError> for PyErr[src]

impl From<ImportError> for PyErr[src]

impl From<IncompleteReadError> for PyErr[src]

impl From<IndexError> for PyErr[src]

impl From<Infallible> for PyErr[src]

impl From<InterruptedError> for PyErr[src]

impl<W: 'static + Send + Debug> From<IntoInnerError<W>> for PyErr[src]

impl From<IntoStringError> for PyErr[src]

impl From<InvalidStateError> for PyErr[src]

impl From<IsADirectoryError> for PyErr[src]

impl From<KeyError> for PyErr[src]

impl From<KeyboardInterrupt> for PyErr[src]

impl From<LimitOverrunError> for PyErr[src]

impl From<LookupError> for PyErr[src]

impl From<MemoryError> for PyErr[src]

impl From<ModuleNotFoundError> for PyErr[src]

impl From<NameError> for PyErr[src]

impl From<NotADirectoryError> for PyErr[src]

impl From<NotImplementedError> for PyErr[src]

impl From<NulError> for PyErr[src]

impl From<OSError> for PyErr[src]

impl From<OverflowError> for PyErr[src]

impl From<PanicException> for PyErr[src]

impl From<ParseBoolError> for PyErr[src]

impl From<ParseFloatError> for PyErr[src]

impl From<ParseIntError> for PyErr[src]

impl From<PermissionError> for PyErr[src]

impl From<ProcessLookupError> for PyErr[src]

impl From<PyBorrowError> for PyErr[src]

impl From<PyBorrowMutError> for PyErr[src]

impl From<PyDowncastError> for PyErr[src]

Convert PyDowncastError to Python TypeError.

impl From<PyErr> for Error[src]

Convert PyErr to io::Error

impl From<QueueEmpty> for PyErr[src]

impl From<QueueFull> for PyErr[src]

impl From<RecursionError> for PyErr[src]

impl From<ReferenceError> for PyErr[src]

impl From<RuntimeError> for PyErr[src]

impl From<StopAsyncIteration> for PyErr[src]

impl From<StopIteration> for PyErr[src]

impl From<SyntaxError> for PyErr[src]

impl From<SystemError> for PyErr[src]

impl From<SystemExit> for PyErr[src]

impl From<TimeoutError> for PyErr[src]

impl From<TimeoutError> for PyErr[src]

impl From<TryFromIntError> for PyErr[src]

impl From<TryFromSliceError> for PyErr[src]

impl From<TypeError> for PyErr[src]

impl From<UnboundLocalError> for PyErr[src]

impl From<UnicodeDecodeError> for PyErr[src]

impl From<UnicodeEncodeError> for PyErr[src]

impl From<UnicodeError> for PyErr[src]

impl From<UnicodeTranslateError> for PyErr[src]

impl From<Utf8Error> for PyErr[src]

impl From<ValueError> for PyErr[src]

impl From<ZeroDivisionError> for PyErr[src]

impl From<gaierror> for PyErr[src]

impl From<herror> for PyErr[src]

impl From<timeout> for PyErr[src]

impl FromPy<PyErr> for PyObject[src]

impl<T> Into<Result<T, PyErr>> for PyErr[src]

Convert PyErr to PyResult<T>

impl<'a> IntoPy<PyObject> for &'a PyErr[src]

impl ToPyObject for PyErr[src]

Auto Trait Implementations

impl !RefUnwindSafe for PyErr

impl !Send for PyErr

impl !Sync for PyErr

impl Unpin for PyErr

impl !UnwindSafe for PyErr

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> FromPy<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> IntoPy<U> for T where
    U: FromPy<T>, 
[src]

impl<T> ToBorrowedObject for T where
    T: ToPyObject
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.