fusion-engineering / inline-python

Inline Python code directly in your Rust code
https://docs.rs/inline-python
BSD 2-Clause "Simplified" License
1.15k stars 37 forks source link

Aborts when using matplotlib with cargo test #52

Closed acshi closed 1 year ago

acshi commented 2 years ago

In the following on a M1 Max Macbook Pro, cargo run works fine, but cargo test results in an abort with "fatal runtime error: Rust cannot catch foreign exceptions"

use inline_python::python;

fn main() {
    python! {
        import matplotlib.pyplot as plt
        plt.plot([1, 2], [3, 4])
        plt.show()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_run() {
        python! {
            import matplotlib.pyplot as plt
            plt.plot([1, 2], [3, 4])
            plt.show()
        }
    }
}
acshi commented 1 year ago

This is because matplotlib (and many other GUI applications) must run on the main thread. An effective workaround is to run with -- --test-threads=1 so that tests all run on the main thread.