pyo3::prelude

Trait PyTracebackMethods

source
pub trait PyTracebackMethods<'py>: Sealed {
    // Required method
    fn format(&self) -> PyResult<String>;
}
Expand description

Implementation of functionality for PyTraceback.

These methods are defined for the Bound<'py, PyTraceback> smart pointer, so to use method call syntax these methods are separated into a trait, because stable Rust does not yet support arbitrary_self_types.

Required Methods§

source

fn format(&self) -> PyResult<String>

Formats the traceback as a string.

This does not include the exception type and value. The exception type and value can be formatted using the Display implementation for PyErr.

§Example

The following code formats a Python traceback and exception pair from Rust:

Python::with_gil(|py| {
    let err = py
        .run(c_str!("raise Exception('banana')"), None, None)
        .expect_err("raise will create a Python error");

    let traceback = err.traceback(py).expect("raised exception will have a traceback");
    assert_eq!(
        format!("{}{}", traceback.format()?, err),
        "\
Traceback (most recent call last):
  File \"<string>\", line 1, in <module>
Exception: banana\
"
    );
    Ok(())
})

Implementors§