Struct cpython::PyErr

source ·
pub struct PyErr {
    pub ptype: PyObject,
    pub pvalue: Option<PyObject>,
    pub ptraceback: Option<PyObject>,
}
Expand description

Represents a Python exception that was raised.

Fields§

§ptype: PyObject

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

§pvalue: Option<PyObject>

The value of the exception.

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

§ptraceback: Option<PyObject>

The PyTraceBack object associated with the error.

Implementations§

source§

impl PyErr

source

pub fn new<T, V>(py: Python<'_>, value: V) -> PyErr

Creates a new PyErr of type T.

value can be:

  • NoArgs: the exception instance will be created using python T()
  • 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: return Err(PyErr::new::<exc::TypeError, _>(py, "Error message"));

source

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

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

source

pub fn new_type( py: Python<'_>, name: &str, base: Option<PyObject>, dict: Option<PyObject> ) -> PyType

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

source

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

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.

source

pub fn from_instance<O>(py: Python<'_>, obj: O) -> PyErr
where O: PythonObject,

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.

source

pub fn new_lazy_init(exc: PyType, value: Option<PyObject>) -> PyErr

Construct a new error, with the usual lazy initialization of Python exceptions. exc is the exception type; usually one of the standard exceptions like py.get_type::<exc::RuntimeError>(). value is the exception instance, or a tuple of arguments to pass to the exception constructor.

source

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

Print a standard traceback to sys.stderr.

source

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

Print a standard traceback to sys.stderr.

source

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

Return 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.

source

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

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

source

pub fn get_type(&self, py: Python<'_>) -> PyType

Retrieves the exception type.

source

pub fn instance(&mut self, py: Python<'_>) -> PyObject

Retrieves the exception instance for this error. This method takes &mut self because the error might need to be normalized in order to create the exception instance.

source

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

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

source

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

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

Trait Implementations§

source§

impl Debug for PyErr

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<Error> for PyErr

source§

fn from(error: Error) -> Self

Converts to this type from the input type.
source§

impl From<PyErr> for Error

source§

fn from(err: PyErr) -> Self

Converts to this type from the input type.
source§

impl<'p> From<PythonObjectDowncastError<'p>> for PyErr

Converts PythonObjectDowncastError to Python TypeError.

source§

fn from(err: PythonObjectDowncastError<'p>) -> PyErr

Converts to this type from the input type.
source§

impl PyClone for PyErr

source§

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

source§

impl PyDrop for PyErr

source§

fn release_ref(self, py: Python<'_>)

Auto Trait Implementations§

§

impl Freeze for PyErr

§

impl RefUnwindSafe for PyErr

§

impl Send for PyErr

§

impl Sync for PyErr

§

impl Unpin for PyErr

§

impl UnwindSafe for PyErr

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.