PyResultは、pyo3のResult型。
実体としては「Result<T, PyErr>」。
PyErrでは、Pythonのエラーを返すことが出来る。
PythonのRuntimeErrorを返す例。
#[pyfunction]
fn runtime_error() -> PyResult<()> {
return Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
format!("error-test")
));
}
#[pymodule] fn py_example(m: &Bound<PyModule>) -> PyResult<()> { 〜 m.add_function(wrap_pyfunction!(runtime_error, m)?)?; }
Pythonで実行すると、RuntimeErrorが発生する。
>>> import py_example
>>> py_example.runtime_error()
Traceback (most recent call last):
File "<python-input-1>", line 1, in <module>
py_example.runtime_error()
~~~~~~~~~~~~~~~~~~~~~~~~^^
RuntimeError: error-test