pyo3/tests/test_anyhow.rs
Icxolu ee89b2e8e2
deprecate wrap_pyfunction with py argument (#3954)
* deprecate `wrap_pyfunction` with `py` argument

The Python token in `wrap_pyfunction` is not handled automatically by
`WrapPyFunctionArg`, for backwards compatibility. This uses deref
specialization to deprecate this variant.

* merge `Extractor`s

* add deprecation ui test, revert closure variant due to test failure

* fix nightly
2024-03-12 22:57:03 +00:00

53 lines
1.1 KiB
Rust

#![cfg(feature = "anyhow")]
use pyo3::wrap_pyfunction_bound;
#[test]
fn test_anyhow_py_function_ok_result() {
use pyo3::{py_run, pyfunction, Python};
#[pyfunction]
#[allow(clippy::unnecessary_wraps)]
fn produce_ok_result() -> anyhow::Result<String> {
Ok(String::from("OK buddy"))
}
Python::with_gil(|py| {
let func = wrap_pyfunction_bound!(produce_ok_result)(py).unwrap();
py_run!(
py,
func,
r#"
func()
"#
);
});
}
#[test]
fn test_anyhow_py_function_err_result() {
use pyo3::prelude::PyDictMethods;
use pyo3::{pyfunction, types::PyDict, Python};
#[pyfunction]
fn produce_err_result() -> anyhow::Result<String> {
anyhow::bail!("error time")
}
Python::with_gil(|py| {
let func = wrap_pyfunction_bound!(produce_err_result)(py).unwrap();
let locals = PyDict::new_bound(py);
locals.set_item("func", func).unwrap();
py.run_bound(
r#"
func()
"#,
None,
Some(&locals),
)
.unwrap_err();
});
}