Macro cpython::py_exception
source · macro_rules! py_exception { ($module: ident, $name: ident, $base: ty) => { ... }; ($module: ident, $name: ident) => { ... }; }
Expand description
Defines a new exception type.
§Syntax
py_exception!(module, MyError)
module
is the name of the containing module.MyError
is the name of the new exception type.
§Example
use cpython::{Python, PyDict, py_exception};
py_exception!(mymodule, CustomError);
fn main() {
let gil = Python::acquire_gil();
let py = gil.python();
let ctx = PyDict::new(py);
ctx.set_item(py, "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();
}