[][src]Macro pyo3::py_exception

macro_rules! py_exception {
    ($module: ident, $name: ident, $base: ty) => { ... };
}

Defines a new exception type.

Syntax

py_exception!(module, MyError, pyo3::exceptions::Exception)

  • module is the name of the containing module.
  • MyError is the name of the new exception type.
  • pyo3::exceptions::Exception is the name of the base type

Example

#[macro_use]
extern crate pyo3;

use pyo3::Python;
use pyo3::types::PyDict;

py_exception!(mymodule, CustomError, pyo3::exceptions::Exception);

fn main() {
    let gil = Python::acquire_gil();
    let py = gil.python();
    let ctx = PyDict::new(py);

    ctx.set_item("CustomError", py.get_type::<CustomError>()).unwrap();

    py.run("assert str(CustomError) == \"<class 'mymodule.CustomError'>\"",
           None, Some(&ctx)).unwrap();
    py.run("assert CustomError('oops').args == ('oops',)", None, Some(ctx)).unwrap();
}