Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add anyhow-integration feature which implements From<anyhow::Error> for PyErr #1822

Merged
merged 10 commits into from
Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Add `PyAny::py()` as a convenience for `PyNativeType::py()`. [#1751](https://github.com/PyO3/pyo3/pull/1751)
- Add implementation of `std::ops::Index<usize>` for `PyList`, `PyTuple` and `PySequence`. [#1825](https://github.com/PyO3/pyo3/pull/1825)
- Add range indexing implementations of `std::ops::Index` for `PyList`, `PyTuple` and `PySequence`. [#1829](https://github.com/PyO3/pyo3/pull/1829)
- Add `anyhow` feature which provides `impl From<anyhow::Error> for PyErr`. [#1822](https://github.com/PyO3/pyo3/pull/1822)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put this in the Packaging section too and make sure this and the eyre entry are written the same way?


### Changed

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ unindent = { version = "0.1.4", optional = true }
hashbrown = { version = ">= 0.9, < 0.12", optional = true }
indexmap = { version = ">= 1.6, < 1.8", optional = true }
serde = {version = "1.0", optional = true}
anyhow = { version = "1.0", optional = true }

[dev-dependencies]
assert_approx_eq = "1.1.0"
Expand Down
4 changes: 4 additions & 0 deletions guide/src/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,7 @@ struct User {
permissions: Vec<Py<Permission>>
}
```

### `anyhow`

This feature makes it possible to return [`anyhow::Result<T>`](https://docs.rs/anyhow/1.0.43/anyhow/type.Result.html) from functions and methods exposed to Python. It does so by adding `impl From<anyhow::Error> for PyErr`. Currently, the conversion simply stringifies the `anyhow::Error` and shoves it into a `PyRuntimeError`. As a consequence, there is no way to convert a `PyErr` back to the original `anyhow::Error`. It is mostly intended as a developer convenience.
41 changes: 41 additions & 0 deletions src/conversions/anyhow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::exceptions::PyRuntimeError;
use crate::PyErr;

impl From<anyhow::Error> for PyErr {
fn from(err: anyhow::Error) -> Self {
PyRuntimeError::new_err(format!("{:?}", err))
}
}

#[cfg(test)]
mod test_anyhow {
use crate::proc_macro::pyfunction;
use crate::py_run;
use crate::wrap_pyfunction;
use crate::{Python, ToPyObject};

#[pyfunction]
fn produce_ok_result() -> anyhow::Result<String> {
Ok(String::from("OK buddy"))
}

#[pyfunction]
fn produce_err_result() -> anyhow::Result<String> {
anyhow::bail!("error time")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually not ideal to use #[pyfunction] within unit tests of pyo3's lib, because it relies on pyo3 being available as in the global namespace. We happen to have a self-dev-dependency on pyo3 but it causes unexpected breakages - see #1817 for more detail.

The point here is that it's probably better to just test that let x: PyErr = produce_err_result().unwrap_err().into() works ok, and could check the string contents of the error are as expected.

(If you really want to check the #[pyfunction] case, that's good too, but it'll need to go in a separate file in tests/.)


#[test]
fn test_anyhow_py_function_ok_result() {
Python::with_gil(|py| {
let func = wrap_pyfunction!(produce_ok_result)(py).unwrap();

py_run!(
py,
func,
r#"
func()
"#
);
});
}
}
2 changes: 2 additions & 0 deletions src/conversions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ mod array;
pub mod indexmap;
mod osstr;
mod path;
#[cfg(feature = "anyhow")]
pub mod anyhow;