File tree Expand file tree Collapse file tree 2 files changed +7
-3
lines changed Expand file tree Collapse file tree 2 files changed +7
-3
lines changed Original file line number Diff line number Diff line change @@ -118,8 +118,10 @@ fn rust_ext(_py: Python, m: &PyModule) -> PyResult<()> {
118
118
x : & PyArrayDyn <f64 >,
119
119
y : & PyArrayDyn <f64 >,
120
120
) -> PyResult <PyArrayDyn <f64 >> {
121
+ // you can convert numpy error into PyErr via ?
121
122
let x = x . as_array ()? ;
122
- let y = y . as_array ()? ;
123
+ // you can also specify your error context, via closure
124
+ let y = y . as_array (). into_pyresult_with (|| " y must be f64 array" )? ;
123
125
Ok (axpy (a , x , y ). to_pyarray (py ). to_owned (py ))
124
126
}
125
127
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ extern crate numpy;
3
3
extern crate pyo3;
4
4
5
5
use ndarray:: { ArrayD , ArrayViewD , ArrayViewMutD } ;
6
- use numpy:: { PyArrayDyn , ToPyArray } ;
6
+ use numpy:: { IntoPyResult , PyArrayDyn , ToPyArray } ;
7
7
use pyo3:: prelude:: { pymodinit, PyModule , PyResult , Python } ;
8
8
9
9
#[ pymodinit]
@@ -26,8 +26,10 @@ fn rust_ext(_py: Python, m: &PyModule) -> PyResult<()> {
26
26
x : & PyArrayDyn < f64 > ,
27
27
y : & PyArrayDyn < f64 > ,
28
28
) -> PyResult < PyArrayDyn < f64 > > {
29
+ // you can convert numpy error into PyErr via ?
29
30
let x = x. as_array ( ) ?;
30
- let y = y. as_array ( ) ?;
31
+ // you can also specify your error context, via closure
32
+ let y = y. as_array ( ) . into_pyresult_with ( || "y must be f64 array" ) ?;
31
33
Ok ( axpy ( a, x, y) . to_pyarray ( py) . to_owned ( py) )
32
34
}
33
35
You can’t perform that action at this time.
0 commit comments