-
Notifications
You must be signed in to change notification settings - Fork 760
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow other Result types in
#[pyfunction]
(#1118)
* Added a couple basic tests * Implemented suggested change * Fixed type inference * cargo fmt * Finished tests and removed warnings * Include in CHANGELOG.md * Moved test into separate file * &'static str and function rename * Mention in the book
- Loading branch information
Mario
authored
Aug 29, 2020
1 parent
1631129
commit 608aea7
Showing
9 changed files
with
95 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//! Testing https://github.com/PyO3/pyo3/issues/1106. A result type that | ||
//! *doesn't* implement `From<MyError> for PyErr` won't be automatically | ||
//! converted when using `#[pyfunction]`. | ||
use pyo3::prelude::*; | ||
use pyo3::wrap_pyfunction; | ||
|
||
use std::fmt; | ||
|
||
/// A basic error type for the tests. It's missing `From<MyError> for PyErr`, | ||
/// though, so it shouldn't work. | ||
#[derive(Debug)] | ||
struct MyError { | ||
pub descr: &'static str, | ||
} | ||
|
||
impl fmt::Display for MyError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "My error message: {}", self.descr) | ||
} | ||
} | ||
|
||
#[pyfunction] | ||
fn should_not_work() -> Result<(), MyError> { | ||
Err(MyError { descr: "something went wrong" }) | ||
} | ||
|
||
fn main() { | ||
let gil = Python::acquire_gil(); | ||
let py = gil.python(); | ||
wrap_pyfunction!(should_not_work)(py); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error[E0277]: the trait bound `std::result::Result<(), MyError>: pyo3::callback::IntoPyCallbackOutput<_>` is not satisfied | ||
--> $DIR/invalid_result_conversion.rs:22:1 | ||
| | ||
22 | #[pyfunction] | ||
| ^^^^^^^^^^^^^ the trait `pyo3::callback::IntoPyCallbackOutput<_>` is not implemented for `std::result::Result<(), MyError>` | ||
| | ||
::: $WORKSPACE/src/callback.rs | ||
| | ||
| T: IntoPyCallbackOutput<U>, | ||
| ----------------------- required by this bound in `pyo3::callback::convert` | ||
| | ||
= help: the following implementations were found: | ||
<std::result::Result<T, E> as pyo3::callback::IntoPyCallbackOutput<U>> | ||
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info) |