2021-10-17 06:54:29 +00:00
|
|
|
#![cfg(feature = "anyhow")]
|
|
|
|
|
2024-03-12 22:57:03 +00:00
|
|
|
use pyo3::wrap_pyfunction_bound;
|
|
|
|
|
2021-10-17 06:54:29 +00:00
|
|
|
#[test]
|
|
|
|
fn test_anyhow_py_function_ok_result() {
|
2024-03-12 22:57:03 +00:00
|
|
|
use pyo3::{py_run, pyfunction, Python};
|
2021-10-17 06:54:29 +00:00
|
|
|
|
|
|
|
#[pyfunction]
|
2022-02-28 08:03:54 +00:00
|
|
|
#[allow(clippy::unnecessary_wraps)]
|
2021-10-17 06:54:29 +00:00
|
|
|
fn produce_ok_result() -> anyhow::Result<String> {
|
|
|
|
Ok(String::from("OK buddy"))
|
|
|
|
}
|
|
|
|
|
|
|
|
Python::with_gil(|py| {
|
2024-03-12 22:57:03 +00:00
|
|
|
let func = wrap_pyfunction_bound!(produce_ok_result)(py).unwrap();
|
2021-10-17 06:54:29 +00:00
|
|
|
|
|
|
|
py_run!(
|
|
|
|
py,
|
|
|
|
func,
|
|
|
|
r#"
|
|
|
|
func()
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_anyhow_py_function_err_result() {
|
2024-02-05 18:44:00 +00:00
|
|
|
use pyo3::prelude::PyDictMethods;
|
2024-03-12 22:57:03 +00:00
|
|
|
use pyo3::{pyfunction, types::PyDict, Python};
|
2021-10-17 06:54:29 +00:00
|
|
|
|
|
|
|
#[pyfunction]
|
|
|
|
fn produce_err_result() -> anyhow::Result<String> {
|
|
|
|
anyhow::bail!("error time")
|
|
|
|
}
|
|
|
|
|
|
|
|
Python::with_gil(|py| {
|
2024-03-12 22:57:03 +00:00
|
|
|
let func = wrap_pyfunction_bound!(produce_err_result)(py).unwrap();
|
2024-02-05 18:44:00 +00:00
|
|
|
let locals = PyDict::new_bound(py);
|
2021-10-17 06:54:29 +00:00
|
|
|
locals.set_item("func", func).unwrap();
|
|
|
|
|
2024-02-05 18:44:00 +00:00
|
|
|
py.run_bound(
|
2021-10-17 06:54:29 +00:00
|
|
|
r#"
|
|
|
|
func()
|
|
|
|
"#,
|
|
|
|
None,
|
2024-02-05 18:44:00 +00:00
|
|
|
Some(&locals),
|
2021-10-17 06:54:29 +00:00
|
|
|
)
|
|
|
|
.unwrap_err();
|
|
|
|
});
|
|
|
|
}
|