macro_rules! import_exception {
    ($module: expr, $name: ident) => { ... };
Expand description

Defines a Rust type for an exception defined in Python code.


import_exception!(module, MyError)

  • module is the name of the containing module.
  • MyError is the name of the new exception type.


use pyo3::import_exception;
use pyo3::types::IntoPyDict;
use pyo3::Python;

import_exception!(socket, gaierror);

Python::with_gil(|py| {
    let ctx = [("gaierror", py.get_type::<gaierror>())].into_py_dict(py);
    pyo3::py_run!(py, *ctx, "import socket; assert gaierror is socket.gaierror");