[][src]Struct pyo3::PyErr

pub struct PyErr { /* fields omitted */ }

Represents a Python exception that was raised.

Implementations

impl PyErr[src]

pub fn new<T, A>(args: A) -> PyErr where
    T: PyTypeObject,
    A: PyErrArguments + Send + Sync + '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)

Note: if value is not Send or Sync, consider using PyErr::from_instance instead.

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

Example:

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

In most cases, you can use a concrete exception's constructor instead, which is equivalent:

This example is not tested
return Err(exceptions::PyTypeError::new_err("Error message"));

pub fn from_type<A>(ty: &PyType, args: A) -> PyErr where
    A: PyErrArguments + Send + Sync + '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::PyRuntimeError. args is the a tuple of arguments to pass to the exception constructor.

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.

Example

use pyo3::{Python, PyErr, IntoPy, exceptions::PyTypeError, types::PyType};
Python::with_gil(|py| {
    // Case #1: Exception instance
    let err = PyErr::from_instance(PyTypeError::new_err("some type error",).instance(py));
    assert_eq!(err.to_string(), "TypeError: some type error");

    // Case #2: Exception type
    let err = PyErr::from_instance(PyType::new::<PyTypeError>(py));
    assert_eq!(err.to_string(), "TypeError: ");

    // Case #3: Invalid exception value
    let err = PyErr::from_instance("foo".into_py(py).as_ref(py));
    assert_eq!(err.to_string(), "TypeError: exceptions must derive from BaseException");
});

pub fn ptype<'py>(&'py self, py: Python<'py>) -> &'py PyType[src]

Get the type of this exception object.

The object will be normalized first if needed.

Example

use pyo3::{Python, PyErr, exceptions::PyTypeError, types::PyType};
Python::with_gil(|py| {
    let err = PyTypeError::new_err(("some type error",));
    assert_eq!(err.ptype(py), PyType::new::<PyTypeError>(py));
});

pub fn pvalue<'py>(&'py self, py: Python<'py>) -> &'py PyBaseException[src]

Get the value of this exception object.

The object will be normalized first if needed.

Example

use pyo3::{Python, PyErr, exceptions::PyTypeError, types::PyType};
Python::with_gil(|py| {
    let err = PyTypeError::new_err(("some type error",));
    assert_eq!(err.pvalue(py).to_string(), "TypeError: some type error");
});

pub fn ptraceback<'py>(&'py self, py: Python<'py>) -> Option<&'py PyAny>[src]

Get the value of this exception object.

The object will be normalized first if needed.

Example

use pyo3::{Python, PyErr, exceptions::PyTypeError, types::PyType};
Python::with_gil(|py| {
    let err = PyTypeError::new_err(("some type error",));
    assert_eq!(err.ptraceback(py), None);
});

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 instance<'py>(&'py self, py: Python<'py>) -> &'py PyBaseException[src]

Retrieves the exception instance for this error.

pub fn into_instance(self, py: Python<'_>) -> Py<PyBaseException>[src]

Consumes self to take ownership of the exception instance for this error.

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]

Clone the PyErr. This requires the GIL, which is why PyErr does not implement Clone.

Example

use pyo3::{Python, PyErr, exceptions::PyTypeError, types::PyType};
Python::with_gil(|py| {
    let err = PyTypeError::new_err(("some type error",));
    let err_clone = err.clone_ref(py);
    assert_eq!(err.ptype(py), err_clone.ptype(py));
    assert_eq!(err.pvalue(py), err_clone.pvalue(py));
    assert_eq!(err.ptraceback(py), err_clone.ptraceback(py));
});

Trait Implementations

impl Debug for PyErr[src]

impl Display for PyErr[src]

impl Error for PyErr[src]

impl<'_> From<&'_ CancelledError> for PyErr[src]

impl<'_> From<&'_ IncompleteReadError> for PyErr[src]

impl<'_> From<&'_ InvalidStateError> for PyErr[src]

impl<'_> From<&'_ LimitOverrunError> for PyErr[src]

impl<'_> From<&'_ PanicException> for PyErr[src]

impl<'_> From<&'_ PyArithmeticError> for PyErr[src]

impl<'_> From<&'_ PyAssertionError> for PyErr[src]

impl<'_> From<&'_ PyAttributeError> for PyErr[src]

impl<'_> From<&'_ PyBaseException> for PyErr[src]

impl<'_> From<&'_ PyBlockingIOError> for PyErr[src]

impl<'_> From<&'_ PyBrokenPipeError> for PyErr[src]

impl<'_> From<&'_ PyBufferError> for PyErr[src]

impl<'_> From<&'_ PyChildProcessError> for PyErr[src]

impl<'_> From<&'_ PyConnectionAbortedError> for PyErr[src]

impl<'_> From<&'_ PyConnectionError> for PyErr[src]

impl<'_> From<&'_ PyConnectionRefusedError> for PyErr[src]

impl<'_> From<&'_ PyConnectionResetError> for PyErr[src]

impl<'_> From<&'_ PyEOFError> for PyErr[src]

impl<'_> From<&'_ PyEnvironmentError> for PyErr[src]

impl<'_> From<&'_ PyException> for PyErr[src]

impl<'_> From<&'_ PyFileExistsError> for PyErr[src]

impl<'_> From<&'_ PyFileNotFoundError> for PyErr[src]

impl<'_> From<&'_ PyFloatingPointError> for PyErr[src]

impl<'_> From<&'_ PyGeneratorExit> for PyErr[src]

impl<'_> From<&'_ PyIOError> for PyErr[src]

impl<'_> From<&'_ PyImportError> for PyErr[src]

impl<'_> From<&'_ PyIndexError> for PyErr[src]

impl<'_> From<&'_ PyInterruptedError> for PyErr[src]

impl<'_> From<&'_ PyIsADirectoryError> for PyErr[src]

impl<'_> From<&'_ PyKeyError> for PyErr[src]

impl<'_> From<&'_ PyKeyboardInterrupt> for PyErr[src]

impl<'_> From<&'_ PyLookupError> for PyErr[src]

impl<'_> From<&'_ PyMemoryError> for PyErr[src]

impl<'_> From<&'_ PyModuleNotFoundError> for PyErr[src]

impl<'_> From<&'_ PyNameError> for PyErr[src]

impl<'_> From<&'_ PyNotADirectoryError> for PyErr[src]

impl<'_> From<&'_ PyNotImplementedError> for PyErr[src]

impl<'_> From<&'_ PyOSError> for PyErr[src]

impl<'_> From<&'_ PyOverflowError> for PyErr[src]

impl<'_> From<&'_ PyPermissionError> for PyErr[src]

impl<'_> From<&'_ PyProcessLookupError> for PyErr[src]

impl<'_> From<&'_ PyRecursionError> for PyErr[src]

impl<'_> From<&'_ PyReferenceError> for PyErr[src]

impl<'_> From<&'_ PyRuntimeError> for PyErr[src]

impl<'_> From<&'_ PyStopAsyncIteration> for PyErr[src]

impl<'_> From<&'_ PyStopIteration> for PyErr[src]

impl<'_> From<&'_ PySyntaxError> for PyErr[src]

impl<'_> From<&'_ PySystemError> for PyErr[src]

impl<'_> From<&'_ PySystemExit> for PyErr[src]

impl<'_> From<&'_ PyTimeoutError> for PyErr[src]

impl<'_> From<&'_ PyTypeError> for PyErr[src]

impl<'_> From<&'_ PyUnboundLocalError> for PyErr[src]

impl<'_> From<&'_ PyUnicodeDecodeError> for PyErr[src]

impl<'_> From<&'_ PyUnicodeEncodeError> for PyErr[src]

impl<'_> From<&'_ PyUnicodeError> for PyErr[src]

impl<'_> From<&'_ PyUnicodeTranslateError> for PyErr[src]

impl<'_> From<&'_ PyValueError> for PyErr[src]

impl<'_> From<&'_ PyZeroDivisionError> for PyErr[src]

impl<'_> From<&'_ QueueEmpty> for PyErr[src]

impl<'_> From<&'_ QueueFull> for PyErr[src]

impl<'_> From<&'_ TimeoutError> for PyErr[src]

impl<'_> From<&'_ gaierror> for PyErr[src]

impl<'_> From<&'_ herror> for PyErr[src]

impl<'_> From<&'_ timeout> for PyErr[src]

impl From<AddrParseError> for PyErr[src]

impl From<DecodeUtf16Error> for PyErr[src]

impl From<Error> for PyErr[src]

Create OSError from io::Error

impl From<FromUtf16Error> for PyErr[src]

impl From<FromUtf8Error> for PyErr[src]

impl From<Infallible> for PyErr[src]

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

impl From<IntoStringError> for PyErr[src]

impl From<NulError> for PyErr[src]

impl From<ParseBoolError> for PyErr[src]

impl From<ParseFloatError> for PyErr[src]

impl From<ParseIntError> for PyErr[src]

impl From<PyBorrowError> for PyErr[src]

impl From<PyBorrowMutError> for PyErr[src]

impl<'a> From<PyDowncastError<'a>> for PyErr[src]

Convert PyDowncastError to Python TypeError.

impl From<PyErr> for Error[src]

Convert PyErr to io::Error

impl From<TryFromIntError> for PyErr[src]

impl From<TryFromSliceError> for PyErr[src]

impl From<Utf8Error> for PyErr[src]

impl IntoPy<Py<PyAny>> for PyErr[src]

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

impl Send for PyErr[src]

impl Sync for PyErr[src]

impl ToPyObject for PyErr[src]

Auto Trait Implementations

impl !RefUnwindSafe 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, U> Into<U> for T where
    U: From<T>, 
[src]

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

impl<T> ToString for T where
    T: Display + ?Sized
[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.