3
3
use crate :: array:: PyArray ;
4
4
use crate :: convert:: ToNpyDims ;
5
5
use crate :: types:: { NpyDataType , TypeNum } ;
6
- use pyo3:: { exceptions as exc, PyErr , PyResult } ;
6
+ use pyo3:: { exceptions as exc, PyErr , PyResult , Python } ;
7
7
use std:: error;
8
8
use std:: fmt;
9
9
@@ -34,42 +34,6 @@ impl<T, E: IntoPyErr> IntoPyResult for Result<T, E> {
34
34
}
35
35
}
36
36
37
- /// Represents a shape and dtype of numpy array.
38
- ///
39
- /// Only for error formatting.
40
- #[ derive( Debug ) ]
41
- pub struct ArrayShape {
42
- pub dims : Box < [ usize ] > ,
43
- pub dtype : NpyDataType ,
44
- }
45
-
46
- impl ArrayShape {
47
- fn boxed_dims ( dims : & [ usize ] ) -> Box < [ usize ] > {
48
- dims. into_iter ( )
49
- . map ( |& x| x)
50
- . collect :: < Vec < _ > > ( )
51
- . into_boxed_slice ( )
52
- }
53
- fn from_array < T : TypeNum , D > ( array : & PyArray < T , D > ) -> Self {
54
- ArrayShape {
55
- dims : Self :: boxed_dims ( array. shape ( ) ) ,
56
- dtype : T :: npy_data_type ( ) ,
57
- }
58
- }
59
- fn from_dims < T : TypeNum , D : ToNpyDims > ( dims : D ) -> Self {
60
- ArrayShape {
61
- dims : Self :: boxed_dims ( dims. slice ( ) ) ,
62
- dtype : T :: npy_data_type ( ) ,
63
- }
64
- }
65
- }
66
-
67
- impl fmt:: Display for ArrayShape {
68
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
69
- write ! ( f, "dims={:?}, dtype={:?}" , self . dims, self . dtype)
70
- }
71
- }
72
-
73
37
/// Represents a dimension and dtype of numpy array.
74
38
///
75
39
/// Only for error formatting.
@@ -96,8 +60,8 @@ pub enum ErrorKind {
96
60
PyToRust { from : ArrayDim , to : ArrayDim } ,
97
61
/// Error for casting rust's `Vec` into numpy array.
98
62
FromVec { dim1 : usize , dim2 : usize } ,
99
- /// Error in numpy -> numpy data conversion
100
- PyToPy ( Box < ( ArrayShape , ArrayShape ) > ) ,
63
+ /// Error occured in Python iterpreter
64
+ Python ( PyErr ) ,
101
65
/// The array need to be contiguous to finish the opretion
102
66
NotContiguous ,
103
67
}
@@ -120,18 +84,8 @@ impl ErrorKind {
120
84
} ,
121
85
}
122
86
}
123
- pub ( crate ) fn dtype_cast < T : TypeNum , D > ( from : & PyArray < T , D > , to : NpyDataType ) -> Self {
124
- let from = ArrayShape :: from_array ( from) ;
125
- let to = ArrayShape {
126
- dims : from. dims . clone ( ) ,
127
- dtype : to,
128
- } ;
129
- ErrorKind :: PyToPy ( Box :: new ( ( from, to) ) )
130
- }
131
- pub ( crate ) fn dims_cast < T : TypeNum , D > ( from : & PyArray < T , D > , to_dim : impl ToNpyDims ) -> Self {
132
- let from = ArrayShape :: from_array ( from) ;
133
- let to = ArrayShape :: from_dims :: < T , _ > ( to_dim) ;
134
- ErrorKind :: PyToPy ( Box :: new ( ( from, to) ) )
87
+ pub ( crate ) fn py ( py : Python < ' _ > ) -> Self {
88
+ ErrorKind :: Python ( PyErr :: fetch ( py) )
135
89
}
136
90
}
137
91
@@ -146,11 +100,7 @@ impl fmt::Display for ErrorKind {
146
100
"Cast failed: Vec To PyArray:\n expect all dim {} but {} was found" ,
147
101
dim1, dim2
148
102
) ,
149
- ErrorKind :: PyToPy ( e) => write ! (
150
- f,
151
- "Cast failed: from=ndarray({}), to=ndarray(dtype={})" ,
152
- e. 0 , e. 1 ,
153
- ) ,
103
+ ErrorKind :: Python ( e) => write ! ( f, "Python error: {:?}" , e) ,
154
104
ErrorKind :: NotContiguous => write ! ( f, "This array is not contiguous!" ) ,
155
105
}
156
106
}
@@ -160,7 +110,10 @@ impl error::Error for ErrorKind {}
160
110
161
111
impl From < ErrorKind > for PyErr {
162
112
fn from ( err : ErrorKind ) -> PyErr {
163
- PyErr :: new :: < exc:: TypeError , _ > ( format ! ( "{}" , err) )
113
+ match err {
114
+ ErrorKind :: Python ( e) => e,
115
+ _ => PyErr :: new :: < exc:: TypeError , _ > ( format ! ( "{}" , err) ) ,
116
+ }
164
117
}
165
118
}
166
119
0 commit comments