Skip to content

Commit d4a7610

Browse files
committed
Remove PyArrayModule in favour of PyArrayAPI
1 parent e544705 commit d4a7610

File tree

4 files changed

+96
-161
lines changed

4 files changed

+96
-161
lines changed

src/array.rs

Lines changed: 31 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Safe interface for NumPy ndarray
22
33
use ndarray::*;
4-
use npyffi;
4+
use npyffi::{self, PyArrayAPI};
55
use pyo3::*;
66
use std::marker::PhantomData;
77
use std::os::raw::c_void;
@@ -15,7 +15,7 @@ pub struct PyArray<T>(PyObject, PhantomData<T>);
1515

1616
pyobject_native_type_convert!(
1717
PyArray<T>,
18-
*npyffi::PyArray_Type_Ptr,
18+
*npyffi::PyArrayAPI.get_type_object(npyffi::ArrayType::PyArray_Type),
1919
npyffi::PyArray_Check,
2020
T
2121
);
@@ -162,8 +162,8 @@ impl<T: TypeNum> PyArray<T> {
162162
/// assert_eq!(pyarray.as_slice().unwrap(), &[1, 2, 3, 4, 5]);
163163
/// # }
164164
/// ```
165-
pub fn from_boxed_slice(py: Python, np: &PyArrayModule, v: Box<[T]>) -> PyArray<T> {
166-
IntoPyArray::into_pyarray(v, py, np)
165+
pub fn from_boxed_slice(py: Python, v: Box<[T]>) -> PyArray<T> {
166+
IntoPyArray::into_pyarray(v, py)
167167
}
168168

169169
/// Construct one-dimension PyArray from `impl IntoIterator`.
@@ -180,8 +180,8 @@ impl<T: TypeNum> PyArray<T> {
180180
/// assert_eq!(pyarray.as_slice().unwrap(), &[1, 2, 3, 4, 5]);
181181
/// # }
182182
/// ```
183-
pub fn from_iter(py: Python, np: &PyArrayModule, i: impl IntoIterator<Item = T>) -> PyArray<T> {
184-
i.into_iter().collect::<Vec<_>>().into_pyarray(py, np)
183+
pub fn from_iter(py: Python, i: impl IntoIterator<Item = T>) -> PyArray<T> {
184+
i.into_iter().collect::<Vec<_>>().into_pyarray(py)
185185
}
186186

187187
/// Construct one-dimension PyArray from Vec.
@@ -196,8 +196,8 @@ impl<T: TypeNum> PyArray<T> {
196196
/// assert_eq!(pyarray.as_slice().unwrap(), &[1, 2, 3, 4, 5]);
197197
/// # }
198198
/// ```
199-
pub fn from_vec(py: Python, np: &PyArrayModule, v: Vec<T>) -> PyArray<T> {
200-
IntoPyArray::into_pyarray(v, py, np)
199+
pub fn from_vec(py: Python, v: Vec<T>) -> PyArray<T> {
200+
IntoPyArray::into_pyarray(v, py)
201201
}
202202

203203
/// Construct a two-dimension PyArray from `Vec<Vec<T>>`.
@@ -217,11 +217,7 @@ impl<T: TypeNum> PyArray<T> {
217217
/// assert!(PyArray::from_vec2(gil.python(), &np, &vec![vec![1], vec![2, 3]]).is_err());
218218
/// # }
219219
/// ```
220-
pub fn from_vec2(
221-
py: Python,
222-
np: &PyArrayModule,
223-
v: &Vec<Vec<T>>,
224-
) -> Result<PyArray<T>, ArrayCastError> {
220+
pub fn from_vec2(py: Python, v: &Vec<Vec<T>>) -> Result<PyArray<T>, ArrayCastError> {
225221
let last_len = v.last().map_or(0, |v| v.len());
226222
if v.iter().any(|v| v.len() != last_len) {
227223
return Err(ArrayCastError::FromVec);
@@ -230,7 +226,7 @@ impl<T: TypeNum> PyArray<T> {
230226
let flattend: Vec<_> = v.iter().cloned().flatten().collect();
231227
unsafe {
232228
let data = convert::into_raw(flattend);
233-
Ok(PyArray::new_(py, np, &dims, null_mut(), data))
229+
Ok(PyArray::new_(py, &dims, null_mut(), data))
234230
}
235231
}
236232

@@ -254,11 +250,7 @@ impl<T: TypeNum> PyArray<T> {
254250
/// assert!(PyArray::from_vec3(gil.python(), &np, &vec![vec![vec![1], vec![]]]).is_err());
255251
/// # }
256252
/// ```
257-
pub fn from_vec3(
258-
py: Python,
259-
np: &PyArrayModule,
260-
v: &Vec<Vec<Vec<T>>>,
261-
) -> Result<PyArray<T>, ArrayCastError> {
253+
pub fn from_vec3(py: Python, v: &Vec<Vec<Vec<T>>>) -> Result<PyArray<T>, ArrayCastError> {
262254
let dim2 = v.last().map_or(0, |v| v.len());
263255
if v.iter().any(|v| v.len() != dim2) {
264256
return Err(ArrayCastError::FromVec);
@@ -271,7 +263,7 @@ impl<T: TypeNum> PyArray<T> {
271263
let flattend: Vec<_> = v.iter().flat_map(|v| v.iter().cloned().flatten()).collect();
272264
unsafe {
273265
let data = convert::into_raw(flattend);
274-
Ok(PyArray::new_(py, np, &dims, null_mut(), data))
266+
Ok(PyArray::new_(py, &dims, null_mut(), data))
275267
}
276268
}
277269

@@ -287,11 +279,11 @@ impl<T: TypeNum> PyArray<T> {
287279
/// assert_eq!(pyarray.as_array().unwrap(), array![[1, 2], [3, 4]].into_dyn());
288280
/// # }
289281
/// ```
290-
pub fn from_ndarray<D>(py: Python, np: &PyArrayModule, arr: Array<T, D>) -> PyArray<T>
282+
pub fn from_ndarray<D>(py: Python, arr: Array<T, D>) -> PyArray<T>
291283
where
292284
D: Dimension,
293285
{
294-
IntoPyArray::into_pyarray(arr, py, np)
286+
IntoPyArray::into_pyarray(arr, py)
295287
}
296288

297289
/// Returns the pointer to the first element of the inner array.
@@ -366,14 +358,13 @@ impl<T: TypeNum> PyArray<T> {
366358
/// Please use `new` or from methods instead.
367359
pub unsafe fn new_(
368360
py: Python,
369-
np: &PyArrayModule,
370361
dims: &[usize],
371362
strides: *mut npy_intp,
372363
data: *mut c_void,
373364
) -> Self {
374365
let dims: Vec<_> = dims.iter().map(|d| *d as npy_intp).collect();
375-
let ptr = np.PyArray_New(
376-
np.get_type_object(npyffi::ArrayType::PyArray_Type),
366+
let ptr = PyArrayAPI.PyArray_New(
367+
PyArrayAPI.get_type_object(npyffi::ArrayType::PyArray_Type),
377368
dims.len() as i32,
378369
dims.as_ptr() as *mut npy_intp,
379370
T::typenum_default(),
@@ -400,8 +391,8 @@ impl<T: TypeNum> PyArray<T> {
400391
/// assert_eq!(pyarray.shape(), &[4, 5, 6]);
401392
/// # }
402393
/// ```
403-
pub fn new(py: Python, np: &PyArrayModule, dims: &[usize]) -> Self {
404-
unsafe { Self::new_(py, np, dims, null_mut(), null_mut()) }
394+
pub fn new(py: Python, dims: &[usize]) -> Self {
395+
unsafe { Self::new_(py, dims, null_mut(), null_mut()) }
405396
}
406397

407398
/// Construct a new nd-dimensional array filled with 0. If `is_fortran` is true, then
@@ -419,11 +410,11 @@ impl<T: TypeNum> PyArray<T> {
419410
/// assert_eq!(pyarray.as_array().unwrap(), array![[0, 0], [0, 0]].into_dyn());
420411
/// # }
421412
/// ```
422-
pub fn zeros(py: Python, np: &PyArrayModule, dims: &[usize], is_fortran: bool) -> Self {
413+
pub fn zeros(py: Python, dims: &[usize], is_fortran: bool) -> Self {
423414
let dims: Vec<npy_intp> = dims.iter().map(|d| *d as npy_intp).collect();
424415
unsafe {
425-
let descr = np.PyArray_DescrFromType(T::typenum_default());
426-
let ptr = np.PyArray_Zeros(
416+
let descr = PyArrayAPI.PyArray_DescrFromType(T::typenum_default());
417+
let ptr = PyArrayAPI.PyArray_Zeros(
427418
dims.len() as i32,
428419
dims.as_ptr() as *mut npy_intp,
429420
descr,
@@ -449,9 +440,9 @@ impl<T: TypeNum> PyArray<T> {
449440
/// let pyarray = PyArray::<i32>::arange(gil.python(), &np, -2.0, 4.0, 3.0);
450441
/// assert_eq!(pyarray.as_slice().unwrap(), &[-2, 1]);
451442
/// # }
452-
pub fn arange(py: Python, np: &PyArrayModule, start: f64, stop: f64, step: f64) -> Self {
443+
pub fn arange(py: Python, start: f64, stop: f64, step: f64) -> Self {
453444
unsafe {
454-
let ptr = np.PyArray_Arange(start, stop, step, T::typenum_default());
445+
let ptr = PyArrayAPI.PyArray_Arange(start, stop, step, T::typenum_default());
455446
Self::from_owned_ptr(py, ptr)
456447
}
457448
}
@@ -468,44 +459,10 @@ impl<T: TypeNum> PyArray<T> {
468459
/// assert!(pyarray_f.copy_to(&np, &mut pyarray_i).is_ok());
469460
/// assert_eq!(pyarray_i.as_slice().unwrap(), &[2, 3, 4]);
470461
/// # }
471-
pub fn copy_to<U: TypeNum>(
472-
&self,
473-
np: &PyArrayModule,
474-
other: &mut PyArray<U>,
475-
) -> Result<(), ArrayCastError> {
462+
pub fn copy_to<U: TypeNum>(&self, other: &mut PyArray<U>) -> Result<(), ArrayCastError> {
476463
let self_ptr = self.as_array_ptr();
477464
let other_ptr = other.as_array_ptr();
478-
let result = unsafe { np.PyArray_CopyInto(other_ptr, self_ptr) };
479-
if result == -1 {
480-
Err(ArrayCastError::Numpy {
481-
from: T::npy_data_type(),
482-
to: U::npy_data_type(),
483-
})
484-
} else {
485-
Ok(())
486-
}
487-
}
488-
489-
/// Move the data of self into `other`, performing a data-type conversion if necessary.
490-
/// # Example
491-
/// ```
492-
/// # extern crate pyo3; extern crate numpy; fn main() {
493-
/// use numpy::{PyArray, PyArrayModule, IntoPyArray};
494-
/// let gil = pyo3::Python::acquire_gil();
495-
/// let np = PyArrayModule::import(gil.python()).unwrap();
496-
/// let pyarray_f = PyArray::<f64>::arange(gil.python(), &np, 2.0, 5.0, 1.0);
497-
/// let mut pyarray_i = PyArray::<i64>::new(gil.python(), &np, &[3]);
498-
/// assert!(pyarray_f.move_to(&np, &mut pyarray_i).is_ok());
499-
/// assert_eq!(pyarray_i.as_slice().unwrap(), &[2, 3, 4]);
500-
/// # }
501-
pub fn move_to<U: TypeNum>(
502-
self,
503-
np: &PyArrayModule,
504-
other: &mut PyArray<U>,
505-
) -> Result<(), ArrayCastError> {
506-
let self_ptr = self.as_array_ptr();
507-
let other_ptr = other.as_array_ptr();
508-
let result = unsafe { np.PyArray_MoveInto(other_ptr, self_ptr) };
465+
let result = unsafe { PyArrayAPI.PyArray_CopyInto(other_ptr, self_ptr) };
509466
if result == -1 {
510467
Err(ArrayCastError::Numpy {
511468
from: T::npy_data_type(),
@@ -530,12 +487,15 @@ impl<T: TypeNum> PyArray<T> {
530487
pub fn cast<U: TypeNum>(
531488
&self,
532489
py: Python,
533-
np: &PyArrayModule,
534490
is_fortran: bool,
535491
) -> Result<PyArray<U>, ArrayCastError> {
536492
let ptr = unsafe {
537-
let descr = np.PyArray_DescrFromType(U::typenum_default());
538-
np.PyArray_CastToType(self.as_array_ptr(), descr, if is_fortran { -1 } else { 0 })
493+
let descr = PyArrayAPI.PyArray_DescrFromType(U::typenum_default());
494+
PyArrayAPI.PyArray_CastToType(
495+
self.as_array_ptr(),
496+
descr,
497+
if is_fortran { -1 } else { 0 },
498+
)
539499
};
540500
if ptr.is_null() {
541501
Err(ArrayCastError::Numpy {

src/convert.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,29 @@ use super::*;
2424
/// ```
2525
pub trait IntoPyArray {
2626
type Item: TypeNum;
27-
fn into_pyarray(self, Python, &PyArrayModule) -> PyArray<Self::Item>;
27+
fn into_pyarray(self, Python) -> PyArray<Self::Item>;
2828
}
2929

3030
impl<T: TypeNum> IntoPyArray for Box<[T]> {
3131
type Item = T;
32-
fn into_pyarray(self, py: Python, np: &PyArrayModule) -> PyArray<Self::Item> {
32+
fn into_pyarray(self, py: Python) -> PyArray<Self::Item> {
3333
let dims = [self.len()];
3434
let ptr = Box::into_raw(self);
35-
unsafe { PyArray::new_(py, np, &dims, null_mut(), ptr as *mut c_void) }
35+
unsafe { PyArray::new_(py, &dims, null_mut(), ptr as *mut c_void) }
3636
}
3737
}
3838

3939
impl<T: TypeNum> IntoPyArray for Vec<T> {
4040
type Item = T;
41-
fn into_pyarray(self, py: Python, np: &PyArrayModule) -> PyArray<Self::Item> {
41+
fn into_pyarray(self, py: Python) -> PyArray<Self::Item> {
4242
let dims = [self.len()];
43-
unsafe { PyArray::new_(py, np, &dims, null_mut(), into_raw(self)) }
43+
unsafe { PyArray::new_(py, &dims, null_mut(), into_raw(self)) }
4444
}
4545
}
4646

4747
impl<A: TypeNum, D: Dimension> IntoPyArray for Array<A, D> {
4848
type Item = A;
49-
fn into_pyarray(self, py: Python, np: &PyArrayModule) -> PyArray<Self::Item> {
49+
fn into_pyarray(self, py: Python) -> PyArray<Self::Item> {
5050
let dims: Vec<_> = self.shape().iter().cloned().collect();
5151
let mut strides: Vec<_> = self
5252
.strides()
@@ -55,7 +55,7 @@ impl<A: TypeNum, D: Dimension> IntoPyArray for Array<A, D> {
5555
.collect();
5656
unsafe {
5757
let data = into_raw(self.into_raw_vec());
58-
PyArray::new_(py, np, &dims, strides.as_mut_ptr(), data)
58+
PyArray::new_(py, &dims, strides.as_mut_ptr(), data)
5959
}
6060
}
6161
}
@@ -65,11 +65,11 @@ macro_rules! array_impls {
6565
$(
6666
impl<T: TypeNum> IntoPyArray for [T; $N] {
6767
type Item = T;
68-
fn into_pyarray(self, py: Python, np: &PyArrayModule) -> PyArray<T> {
68+
fn into_pyarray(self, py: Python) -> PyArray<T> {
6969
let dims = [$N];
7070
let ptr = Box::into_raw(Box::new(self));
7171
unsafe {
72-
PyArray::new_(py, np, &dims, null_mut(), ptr as *mut c_void)
72+
PyArray::new_(py, &dims, null_mut(), ptr as *mut c_void)
7373
}
7474
}
7575
}

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
//! );
2929
//! }
3030
//! ```
31-
3231
#![feature(specialization)]
3332

3433
#[macro_use]
@@ -48,5 +47,5 @@ pub mod types;
4847
pub use array::PyArray;
4948
pub use convert::IntoPyArray;
5049
pub use error::*;
51-
pub use npyffi::{PyArrayModule, PyUFuncModule};
50+
pub use npyffi::{PyArrayAPI, PyUFuncModule};
5251
pub use types::*;

0 commit comments

Comments
 (0)