Skip to content

Commit f67af12

Browse files
committed
initial migration to pyo3 0.23
1 parent ce5003f commit f67af12

File tree

6 files changed

+26
-97
lines changed

6 files changed

+26
-97
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "numpy"
3-
version = "0.22.1"
3+
version = "0.23.0-dev"
44
authors = [
55
"The rust-numpy Project Developers",
66
"PyO3 Project and Contributors <https://github.com/PyO3>"
@@ -22,11 +22,11 @@ num-complex = ">= 0.2, < 0.5"
2222
num-integer = "0.1"
2323
num-traits = "0.2"
2424
ndarray = ">= 0.15, < 0.17"
25-
pyo3 = { version = "0.22.0", default-features = false, features = ["macros"] }
25+
pyo3 = { version = "0.23.0-dev", default-features = false, features = ["macros"] }
2626
rustc-hash = "1.1"
2727

2828
[dev-dependencies]
29-
pyo3 = { version = "0.22.0", default-features = false, features = ["auto-initialize"] }
29+
pyo3 = { version = "0.23.0-dev", default-features = false, features = ["auto-initialize"] }
3030
nalgebra = { version = ">=0.30, <0.34", default-features = false, features = ["std"] }
3131

3232
[package.metadata.docs.rs]

src/array.rs

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use std::{
66
marker::PhantomData,
77
mem,
8-
ops::Deref,
98
os::raw::{c_int, c_void},
109
ptr, slice,
1110
};
@@ -17,10 +16,9 @@ use ndarray::{
1716
};
1817
use num_traits::AsPrimitive;
1918
use pyo3::{
20-
ffi, pyobject_native_type_base,
19+
ffi,
2120
types::{DerefToPyAny, PyAnyMethods, PyModule},
22-
AsPyPointer, Bound, DowncastError, IntoPy, Py, PyAny, PyErr, PyObject, PyResult, PyTypeInfo,
23-
Python,
21+
Bound, DowncastError, Py, PyAny, PyErr, PyObject, PyResult, PyTypeInfo, Python,
2422
};
2523

2624
use crate::borrow::{PyReadonlyArray, PyReadwriteArray};
@@ -133,63 +131,11 @@ unsafe impl<T: Element, D: Dimension> PyTypeInfo for PyArray<T, D> {
133131
unsafe { npyffi::PY_ARRAY_API.get_type_object(py, npyffi::NpyTypes::PyArray_Type) }
134132
}
135133

136-
fn is_type_of_bound(ob: &Bound<'_, PyAny>) -> bool {
134+
fn is_type_of(ob: &Bound<'_, PyAny>) -> bool {
137135
Self::extract::<IgnoreError>(ob).is_ok()
138136
}
139137
}
140138

141-
pyobject_native_type_base!(PyArray<T, D>; T; D);
142-
143-
impl<T, D> AsRef<PyAny> for PyArray<T, D> {
144-
#[inline]
145-
fn as_ref(&self) -> &PyAny {
146-
&self.0
147-
}
148-
}
149-
150-
impl<T, D> Deref for PyArray<T, D> {
151-
type Target = PyUntypedArray;
152-
153-
#[inline]
154-
fn deref(&self) -> &Self::Target {
155-
self.as_untyped()
156-
}
157-
}
158-
159-
unsafe impl<T, D> AsPyPointer for PyArray<T, D> {
160-
#[inline]
161-
fn as_ptr(&self) -> *mut ffi::PyObject {
162-
self.0.as_ptr()
163-
}
164-
}
165-
166-
impl<T, D> IntoPy<Py<PyArray<T, D>>> for &'_ PyArray<T, D> {
167-
#[inline]
168-
fn into_py<'py>(self, py: Python<'py>) -> Py<PyArray<T, D>> {
169-
unsafe { Py::from_borrowed_ptr(py, self.as_ptr()) }
170-
}
171-
}
172-
173-
impl<'a, T, D> From<&'a PyArray<T, D>> for &'a PyAny {
174-
fn from(ob: &'a PyArray<T, D>) -> Self {
175-
unsafe { &*(ob as *const PyArray<T, D> as *const PyAny) }
176-
}
177-
}
178-
179-
impl<T, D> IntoPy<PyObject> for PyArray<T, D> {
180-
fn into_py<'py>(self, py: Python<'py>) -> PyObject {
181-
unsafe { PyObject::from_borrowed_ptr(py, self.as_ptr()) }
182-
}
183-
}
184-
185-
impl<T, D> PyArray<T, D> {
186-
/// Access an untyped representation of this array.
187-
#[inline(always)]
188-
pub fn as_untyped(&self) -> &PyUntypedArray {
189-
unsafe { &*(self as *const Self as *const PyUntypedArray) }
190-
}
191-
}
192-
193139
impl<T: Element, D: Dimension> PyArray<T, D> {
194140
fn extract<'a, 'py, E>(ob: &'a Bound<'py, PyAny>) -> Result<&'a Bound<'py, Self>, E>
195141
where

src/dtype.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use pyo3::sync::GILOnceCell;
1010
use pyo3::{
1111
exceptions::{PyIndexError, PyValueError},
1212
ffi::{self, PyTuple_Size},
13-
pyobject_native_type_extract, pyobject_native_type_named,
13+
pyobject_native_type_named,
1414
types::{PyAnyMethods, PyDict, PyDictMethods, PyTuple, PyType},
1515
Borrowed, Bound, Py, PyAny, PyObject, PyResult, PyTypeInfo, Python, ToPyObject,
1616
};
@@ -60,8 +60,6 @@ unsafe impl PyTypeInfo for PyArrayDescr {
6060
}
6161
}
6262

63-
pyobject_native_type_extract!(PyArrayDescr);
64-
6563
/// Returns the type descriptor ("dtype") for a registered type.
6664
pub fn dtype_bound<'py, T: Element>(py: Python<'py>) -> Bound<'py, PyArrayDescr> {
6765
T::get_dtype_bound(py)
@@ -148,7 +146,7 @@ pub trait PyArrayDescrMethods<'py>: Sealed {
148146
/// [enumerated-types]: https://numpy.org/doc/stable/reference/c-api/dtype.html#enumerated-types
149147
/// [dtype-num]: https://numpy.org/doc/stable/reference/generated/numpy.dtype.num.html
150148
fn num(&self) -> c_int {
151-
unsafe { *self.as_dtype_ptr() }.type_num
149+
unsafe { &*self.as_dtype_ptr() }.type_num
152150
}
153151

154152
/// Returns the element size of this type descriptor.
@@ -173,7 +171,7 @@ pub trait PyArrayDescrMethods<'py>: Sealed {
173171
///
174172
/// [dtype-byteorder]: https://numpy.org/doc/stable/reference/generated/numpy.dtype.byteorder.html
175173
fn byteorder(&self) -> u8 {
176-
unsafe { *self.as_dtype_ptr() }.byteorder.max(0) as _
174+
unsafe { &*self.as_dtype_ptr() }.byteorder.max(0) as _
177175
}
178176

179177
/// Returns a unique ASCII character for each of the 21 different built-in types.
@@ -184,7 +182,7 @@ pub trait PyArrayDescrMethods<'py>: Sealed {
184182
///
185183
/// [dtype-char]: https://numpy.org/doc/stable/reference/generated/numpy.dtype.char.html
186184
fn char(&self) -> u8 {
187-
unsafe { *self.as_dtype_ptr() }.type_.max(0) as _
185+
unsafe { &*self.as_dtype_ptr() }.type_.max(0) as _
188186
}
189187

190188
/// Returns an ASCII character (one of `biufcmMOSUV`) identifying the general kind of data.
@@ -195,7 +193,7 @@ pub trait PyArrayDescrMethods<'py>: Sealed {
195193
///
196194
/// [dtype-kind]: https://numpy.org/doc/stable/reference/generated/numpy.dtype.kind.html
197195
fn kind(&self) -> u8 {
198-
unsafe { *self.as_dtype_ptr() }.kind.max(0) as _
196+
unsafe { &*self.as_dtype_ptr() }.kind.max(0) as _
199197
}
200198

201199
/// Returns bit-flags describing how this type descriptor is to be interpreted.
@@ -319,7 +317,7 @@ impl<'py> PyArrayDescrMethods<'py> for Bound<'py, PyArrayDescr> {
319317
}
320318

321319
fn typeobj(&self) -> Bound<'py, PyType> {
322-
let dtype_type_ptr = unsafe { *self.as_dtype_ptr() }.typeobj;
320+
let dtype_type_ptr = unsafe { &*self.as_dtype_ptr() }.typeobj;
323321
unsafe { PyType::from_borrowed_type_ptr(self.py(), dtype_type_ptr) }
324322
}
325323

@@ -725,7 +723,7 @@ mod tests {
725723
assert!(!dt.has_subarray());
726724
assert!(dt.base().is_equiv_to(&dt));
727725
assert_eq!(dt.ndim(), 0);
728-
assert_eq!(dt.shape(), vec![]);
726+
assert_eq!(dt.shape(), Vec::<usize>::new());
729727
});
730728
}
731729

@@ -801,7 +799,7 @@ mod tests {
801799
assert!(dt.is_aligned_struct());
802800
assert!(!dt.has_subarray());
803801
assert_eq!(dt.ndim(), 0);
804-
assert_eq!(dt.shape(), vec![]);
802+
assert_eq!(dt.shape(), Vec::<usize>::new());
805803
assert!(dt.base().is_equiv_to(&dt));
806804
let x = dt.get_field("x").unwrap();
807805
assert!(x.0.is_equiv_to(&dtype_bound::<u8>(py)));

src/npyffi/objects.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use super::types::*;
1111
use crate::npyffi::*;
1212

1313
#[repr(C)]
14-
#[derive(Copy, Clone)]
1514
pub struct PyArrayObject {
1615
pub ob_base: PyObject,
1716
pub data: *mut c_char,
@@ -25,7 +24,6 @@ pub struct PyArrayObject {
2524
}
2625

2726
#[repr(C)]
28-
#[derive(Copy, Clone)]
2927
pub struct PyArray_Descr {
3028
pub ob_base: PyObject,
3129
pub typeobj: *mut PyTypeObject,
@@ -37,7 +35,6 @@ pub struct PyArray_Descr {
3735
}
3836

3937
#[repr(C)]
40-
#[derive(Copy, Clone)]
4138
pub struct PyArray_DescrProto {
4239
pub ob_base: PyObject,
4340
pub typeobj: *mut PyTypeObject,
@@ -58,7 +55,6 @@ pub struct PyArray_DescrProto {
5855
}
5956

6057
#[repr(C)]
61-
#[derive(Copy, Clone)]
6258
pub struct _PyArray_DescrNumPy2 {
6359
pub ob_base: PyObject,
6460
pub typeobj: *mut PyTypeObject,
@@ -76,7 +72,6 @@ pub struct _PyArray_DescrNumPy2 {
7672
}
7773

7874
#[repr(C)]
79-
#[derive(Copy, Clone)]
8075
struct _PyArray_LegacyDescr {
8176
pub ob_base: PyObject,
8277
pub typeobj: *mut PyTypeObject,
@@ -310,7 +305,6 @@ pub type PyArray_FastTakeFunc = Option<
310305
>;
311306

312307
#[repr(C)]
313-
#[derive(Clone, Copy)]
314308
pub struct PyArrayFlagsObject {
315309
pub ob_base: PyObject,
316310
pub arr: *mut PyObject,
@@ -325,7 +319,6 @@ pub struct PyArray_Dims {
325319
}
326320

327321
#[repr(C)]
328-
#[derive(Clone, Copy)]
329322
pub struct PyArray_Chunk {
330323
pub ob_base: PyObject,
331324
pub base: *mut PyObject,
@@ -349,7 +342,6 @@ pub struct PyArrayInterface {
349342
}
350343

351344
#[repr(C)]
352-
#[derive(Clone, Copy)]
353345
pub struct PyUFuncObject {
354346
pub ob_base: PyObject,
355347
pub nin: c_int,
@@ -428,7 +420,6 @@ pub type PyUFunc_MaskedInnerLoopSelectionFunc = Option<
428420
pub struct NpyIter([u8; 0]);
429421

430422
#[repr(C)]
431-
#[derive(Clone, Copy)]
432423
pub struct PyArrayIterObject {
433424
pub ob_base: PyObject,
434425
pub nd_m1: c_int,
@@ -449,7 +440,6 @@ pub struct PyArrayIterObject {
449440
}
450441

451442
#[repr(C)]
452-
#[derive(Clone, Copy)]
453443
pub struct PyArrayMultiIterObject {
454444
pub ob_base: PyObject,
455445
pub numiter: c_int,
@@ -461,7 +451,6 @@ pub struct PyArrayMultiIterObject {
461451
}
462452

463453
#[repr(C)]
464-
#[derive(Clone, Copy)]
465454
pub struct PyArrayNeighborhoodIterObject {
466455
pub ob_base: PyObject,
467456
pub nd_m1: c_int,
@@ -487,7 +476,6 @@ pub struct PyArrayNeighborhoodIterObject {
487476
}
488477

489478
#[repr(C)]
490-
#[derive(Clone, Copy)]
491479
pub struct PyArrayMapIterObject {
492480
pub ob_base: PyObject,
493481
pub numiter: c_int,
@@ -571,7 +559,6 @@ pub struct npy_static_string {
571559
}
572560

573561
#[repr(C)]
574-
#[derive(Clone, Copy)]
575562
pub struct PyArray_StringDTypeObject {
576563
pub base: PyArray_Descr,
577564
pub na_object: *mut PyObject,

src/untyped_array.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
use std::slice;
55

66
use pyo3::{
7-
ffi, pyobject_native_type_extract, pyobject_native_type_named, types::PyAnyMethods,
8-
AsPyPointer, Bound, IntoPy, PyAny, PyObject, PyTypeInfo, Python,
7+
ffi, pyobject_native_type_named, types::PyAnyMethods, Bound, PyAny, PyTypeInfo, Python,
98
};
109

1110
use crate::array::{PyArray, PyArrayMethods};
@@ -70,21 +69,13 @@ unsafe impl PyTypeInfo for PyUntypedArray {
7069
unsafe { npyffi::PY_ARRAY_API.get_type_object(py, npyffi::NpyTypes::PyArray_Type) }
7170
}
7271

73-
fn is_type_of_bound(ob: &Bound<'_, PyAny>) -> bool {
72+
fn is_type_of(ob: &Bound<'_, PyAny>) -> bool {
7473
unsafe { npyffi::PyArray_Check(ob.py(), ob.as_ptr()) != 0 }
7574
}
7675
}
7776

7877
pyobject_native_type_named!(PyUntypedArray);
7978

80-
impl IntoPy<PyObject> for PyUntypedArray {
81-
fn into_py<'py>(self, py: Python<'py>) -> PyObject {
82-
unsafe { PyObject::from_borrowed_ptr(py, self.as_ptr()) }
83-
}
84-
}
85-
86-
pyobject_native_type_extract!(PyUntypedArray);
87-
8879
/// Implementation of functionality for [`PyUntypedArray`].
8980
#[doc(alias = "PyUntypedArray")]
9081
pub trait PyUntypedArrayMethods<'py>: Sealed {

tests/array.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ fn rank_zero_array_has_invalid_strides_dimensions() {
9191
let arr = PyArray::<f64, _>::zeros_bound(py, (), false);
9292

9393
assert_eq!(arr.ndim(), 0);
94-
assert_eq!(arr.strides(), &[]);
95-
assert_eq!(arr.shape(), &[]);
94+
assert_eq!(arr.strides(), &[] as &[isize]);
95+
assert_eq!(arr.shape(), &[] as &[usize]);
9696

9797
assert_eq!(arr.len(), 1);
9898
assert!(!arr.is_empty());
@@ -609,7 +609,14 @@ fn unicode_strings_with_explicit_dtype_works() {
609609
assert_eq!(array[1].0, [b'b' as _, b'a' as _, b'r' as _, 0, 0, 0]);
610610
assert_eq!(
611611
array[2].0,
612-
[b'f' as _, b'o' as _, b'o' as _, b'b' as _, b'a' as _, b'r' as _]
612+
[
613+
b'f' as u32,
614+
b'o' as _,
615+
b'o' as _,
616+
b'b' as _,
617+
b'a' as _,
618+
b'r' as _
619+
]
613620
);
614621
}
615622

0 commit comments

Comments
 (0)