1
1
//! Untyped safe interface for NumPy ndarray
2
2
3
- use cpython:: * ;
4
3
use ndarray:: * ;
5
4
use npyffi;
6
- use pyffi ;
5
+ use pyo3 :: * ;
7
6
8
7
use std:: os:: raw:: c_void;
9
8
use std:: ptr:: null_mut;
@@ -13,22 +12,19 @@ use super::*;
13
12
14
13
/// Untyped safe interface for NumPy ndarray.
15
14
pub struct PyArray ( PyObject ) ;
15
+ pyobject_native_type ! ( PyArray , * npyffi:: PyArray_Type_Ptr , npyffi:: PyArray_Check ) ;
16
16
17
17
impl PyArray {
18
- pub fn as_ptr ( & self ) -> * mut npyffi:: PyArrayObject {
19
- self . 0 . as_ptr ( ) as * mut npyffi :: PyArrayObject
18
+ pub fn as_array_ptr ( & self ) -> * mut npyffi:: PyArrayObject {
19
+ self . as_ptr ( ) as _
20
20
}
21
21
22
- pub fn steal_ptr ( self ) -> * mut npyffi:: PyArrayObject {
23
- self . 0 . steal_ptr ( ) as * mut npyffi:: PyArrayObject
24
- }
25
-
26
- pub unsafe fn from_owned_ptr ( py : Python , ptr : * mut pyffi:: PyObject ) -> Self {
22
+ pub unsafe fn from_owned_ptr ( py : Python , ptr : * mut pyo3:: ffi:: PyObject ) -> Self {
27
23
let obj = PyObject :: from_owned_ptr ( py, ptr) ;
28
24
PyArray ( obj)
29
25
}
30
26
31
- pub unsafe fn from_borrowed_ptr ( py : Python , ptr : * mut pyffi :: PyObject ) -> Self {
27
+ pub unsafe fn from_borrowed_ptr ( py : Python , ptr : * mut pyo3 :: ffi :: PyObject ) -> Self {
32
28
let obj = PyObject :: from_borrowed_ptr ( py, ptr) ;
33
29
PyArray ( obj)
34
30
}
@@ -37,7 +33,7 @@ impl PyArray {
37
33
///
38
34
/// https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_NDIM
39
35
pub fn ndim ( & self ) -> usize {
40
- let ptr = self . as_ptr ( ) ;
36
+ let ptr = self . as_array_ptr ( ) ;
41
37
unsafe { ( * ptr) . nd as usize }
42
38
}
43
39
@@ -46,7 +42,7 @@ impl PyArray {
46
42
/// https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_DIMS
47
43
pub fn dims ( & self ) -> Vec < usize > {
48
44
let n = self . ndim ( ) ;
49
- let ptr = self . as_ptr ( ) ;
45
+ let ptr = self . as_array_ptr ( ) ;
50
46
let dims = unsafe {
51
47
let p = ( * ptr) . dimensions ;
52
48
:: std:: slice:: from_raw_parts ( p, n)
@@ -69,7 +65,7 @@ impl PyArray {
69
65
/// - https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.strides.html#numpy.ndarray.strides
70
66
pub fn strides ( & self ) -> Vec < isize > {
71
67
let n = self . ndim ( ) ;
72
- let ptr = self . as_ptr ( ) ;
68
+ let ptr = self . as_array_ptr ( ) ;
73
69
let dims = unsafe {
74
70
let p = ( * ptr) . strides ;
75
71
:: std:: slice:: from_raw_parts ( p, n)
@@ -78,7 +74,7 @@ impl PyArray {
78
74
}
79
75
80
76
unsafe fn data < T > ( & self ) -> * mut T {
81
- let ptr = self . as_ptr ( ) ;
77
+ let ptr = self . as_array_ptr ( ) ;
82
78
( * ptr) . data as * mut T
83
79
}
84
80
@@ -94,7 +90,7 @@ impl PyArray {
94
90
95
91
pub fn typenum ( & self ) -> i32 {
96
92
unsafe {
97
- let descr = ( * self . as_ptr ( ) ) . descr ;
93
+ let descr = ( * self . as_array_ptr ( ) ) . descr ;
98
94
( * descr) . type_num
99
95
}
100
96
}
@@ -143,7 +139,7 @@ impl PyArray {
143
139
unsafe { Ok ( :: std:: slice:: from_raw_parts_mut ( self . data ( ) , self . len ( ) ) ) }
144
140
}
145
141
146
- pub unsafe fn new_ < T : TypeNum > (
142
+ pub unsafe fn new_ < T : types :: TypeNum > (
147
143
py : Python ,
148
144
np : & PyArrayModule ,
149
145
dims : & [ usize ] ,
@@ -204,75 +200,3 @@ impl PyArray {
204
200
}
205
201
}
206
202
}
207
-
208
- impl < ' source > FromPyObject < ' source > for PyArray {
209
- fn extract ( py : Python , obj : & ' source PyObject ) -> PyResult < Self > {
210
- Ok ( obj. clone_ref ( py) . cast_into :: < PyArray > ( py) ?)
211
- }
212
- }
213
-
214
- impl < ' source > FromPyObject < ' source > for & ' source PyArray {
215
- fn extract ( py : Python , obj : & ' source PyObject ) -> PyResult < Self > {
216
- Ok ( obj. cast_as :: < PyArray > ( py) ?)
217
- }
218
- }
219
-
220
- impl ToPyObject for PyArray {
221
- type ObjectType = Self ;
222
-
223
- fn to_py_object ( & self , py : Python ) -> Self {
224
- PyClone :: clone_ref ( self , py)
225
- }
226
- }
227
-
228
- impl PythonObject for PyArray {
229
- #[ inline]
230
- fn as_object ( & self ) -> & PyObject {
231
- & self . 0
232
- }
233
-
234
- #[ inline]
235
- fn into_object ( self ) -> PyObject {
236
- self . 0
237
- }
238
-
239
- #[ inline]
240
- unsafe fn unchecked_downcast_from ( obj : PyObject ) -> Self {
241
- PyArray ( obj)
242
- }
243
-
244
- #[ inline]
245
- unsafe fn unchecked_downcast_borrow_from < ' a > ( obj : & ' a PyObject ) -> & ' a Self {
246
- :: std:: mem:: transmute ( obj)
247
- }
248
- }
249
-
250
- impl PythonObjectWithCheckedDowncast for PyArray {
251
- fn downcast_from < ' p > (
252
- py : Python < ' p > ,
253
- obj : PyObject ,
254
- ) -> Result < PyArray , PythonObjectDowncastError < ' p > > {
255
- let np = PyArrayModule :: import ( py) . unwrap ( ) ;
256
- unsafe {
257
- if npyffi:: PyArray_Check ( & np, obj. as_ptr ( ) ) != 0 {
258
- Ok ( PyArray ( obj) )
259
- } else {
260
- Err ( PythonObjectDowncastError ( py) )
261
- }
262
- }
263
- }
264
-
265
- fn downcast_borrow_from < ' a , ' p > (
266
- py : Python < ' p > ,
267
- obj : & ' a PyObject ,
268
- ) -> Result < & ' a PyArray , PythonObjectDowncastError < ' p > > {
269
- let np = PyArrayModule :: import ( py) . unwrap ( ) ;
270
- unsafe {
271
- if npyffi:: PyArray_Check ( & np, obj. as_ptr ( ) ) != 0 {
272
- Ok ( :: std:: mem:: transmute ( obj) )
273
- } else {
274
- Err ( PythonObjectDowncastError ( py) )
275
- }
276
- }
277
- }
278
- }
0 commit comments