Skip to content

Commit cd6bb7f

Browse files
Icxoluadamreichold
authored andcommitted
deprecate PyArray::from_vec
1 parent ee7823d commit cd6bb7f

File tree

5 files changed

+66
-30
lines changed

5 files changed

+66
-30
lines changed

benches/array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ fn from_vec2(bencher: &mut Bencher, size: usize) {
141141
iter_with_gil(bencher, |py| {
142142
let vec2 = black_box(&vec2);
143143

144-
PyArray2::from_vec2(py, vec2).unwrap();
144+
PyArray2::from_vec2_bound(py, vec2).unwrap();
145145
});
146146
}
147147

@@ -166,7 +166,7 @@ fn from_vec3(bencher: &mut Bencher, size: usize) {
166166
iter_with_gil(bencher, |py| {
167167
let vec3 = black_box(&vec3);
168168

169-
PyArray3::from_vec3(py, vec3).unwrap();
169+
PyArray3::from_vec3_bound(py, vec3).unwrap();
170170
});
171171
}
172172

src/array.rs

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,23 +1099,33 @@ impl<T: Element> PyArray<T, Ix1> {
10991099
}
11001100
}
11011101

1102+
/// Deprecated form of [`PyArray<T, Ix1>::from_vec_bound`]
1103+
#[deprecated(
1104+
since = "0.21.0",
1105+
note = "will be replaced by `PyArray::from_vec_bound` in the future"
1106+
)]
1107+
#[inline(always)]
1108+
pub fn from_vec<'py>(py: Python<'py>, vec: Vec<T>) -> &'py Self {
1109+
Self::from_vec_bound(py, vec).into_gil_ref()
1110+
}
1111+
11021112
/// Construct a one-dimensional array from a [`Vec<T>`][Vec].
11031113
///
11041114
/// # Example
11051115
///
11061116
/// ```
1107-
/// use numpy::PyArray;
1117+
/// use numpy::{PyArray, PyArrayMethods};
11081118
/// use pyo3::Python;
11091119
///
11101120
/// Python::with_gil(|py| {
11111121
/// let vec = vec![1, 2, 3, 4, 5];
1112-
/// let pyarray = PyArray::from_vec(py, vec);
1122+
/// let pyarray = PyArray::from_vec_bound(py, vec);
11131123
/// assert_eq!(pyarray.readonly().as_slice().unwrap(), &[1, 2, 3, 4, 5]);
11141124
/// });
11151125
/// ```
11161126
#[inline(always)]
1117-
pub fn from_vec<'py>(py: Python<'py>, vec: Vec<T>) -> &'py Self {
1118-
vec.into_pyarray_bound(py).into_gil_ref()
1127+
pub fn from_vec_bound<'py>(py: Python<'py>, vec: Vec<T>) -> Bound<'py, Self> {
1128+
vec.into_pyarray_bound(py)
11191129
}
11201130

11211131
/// Deprecated form of [`PyArray<T, Ix1>::from_iter_bound`]
@@ -1156,6 +1166,15 @@ impl<T: Element> PyArray<T, Ix1> {
11561166
}
11571167

11581168
impl<T: Element> PyArray<T, Ix2> {
1169+
/// Deprecated form of [`PyArray<T, Ix2>::from_vec2_bound`]
1170+
#[deprecated(
1171+
since = "0.21.0",
1172+
note = "will be replaced by `PyArray::from_vec2_bound` in the future"
1173+
)]
1174+
pub fn from_vec2<'py>(py: Python<'py>, v: &[Vec<T>]) -> Result<&'py Self, FromVecError> {
1175+
Self::from_vec2_bound(py, v).map(Bound::into_gil_ref)
1176+
}
1177+
11591178
/// Construct a two-dimension array from a [`Vec<Vec<T>>`][Vec].
11601179
///
11611180
/// This function checks all dimensions of the inner vectors and returns
@@ -1164,20 +1183,23 @@ impl<T: Element> PyArray<T, Ix2> {
11641183
/// # Example
11651184
///
11661185
/// ```
1167-
/// use numpy::PyArray;
1186+
/// use numpy::{PyArray, PyArrayMethods};
11681187
/// use pyo3::Python;
11691188
/// use ndarray::array;
11701189
///
11711190
/// Python::with_gil(|py| {
11721191
/// let vec2 = vec![vec![11, 12], vec![21, 22]];
1173-
/// let pyarray = PyArray::from_vec2(py, &vec2).unwrap();
1192+
/// let pyarray = PyArray::from_vec2_bound(py, &vec2).unwrap();
11741193
/// assert_eq!(pyarray.readonly().as_array(), array![[11, 12], [21, 22]]);
11751194
///
11761195
/// let ragged_vec2 = vec![vec![11, 12], vec![21]];
1177-
/// assert!(PyArray::from_vec2(py, &ragged_vec2).is_err());
1196+
/// assert!(PyArray::from_vec2_bound(py, &ragged_vec2).is_err());
11781197
/// });
11791198
/// ```
1180-
pub fn from_vec2<'py>(py: Python<'py>, v: &[Vec<T>]) -> Result<&'py Self, FromVecError> {
1199+
pub fn from_vec2_bound<'py>(
1200+
py: Python<'py>,
1201+
v: &[Vec<T>],
1202+
) -> Result<Bound<'py, Self>, FromVecError> {
11811203
let len2 = v.first().map_or(0, |v| v.len());
11821204
let dims = [v.len(), len2];
11831205
// SAFETY: The result of `Self::new` is always safe to drop.
@@ -1191,12 +1213,21 @@ impl<T: Element> PyArray<T, Ix2> {
11911213
}
11921214
clone_elements(v, &mut data_ptr);
11931215
}
1194-
Ok(array.into_gil_ref())
1216+
Ok(array)
11951217
}
11961218
}
11971219
}
11981220

11991221
impl<T: Element> PyArray<T, Ix3> {
1222+
/// Deprecated form of [`PyArray<T, Ix3>::from_vec3_bound`]
1223+
#[deprecated(
1224+
since = "0.21.0",
1225+
note = "will be replaced by `PyArray::from_vec3_bound` in the future"
1226+
)]
1227+
pub fn from_vec3<'py>(py: Python<'py>, v: &[Vec<Vec<T>>]) -> Result<&'py Self, FromVecError> {
1228+
Self::from_vec3_bound(py, v).map(Bound::into_gil_ref)
1229+
}
1230+
12001231
/// Construct a three-dimensional array from a [`Vec<Vec<Vec<T>>>`][Vec].
12011232
///
12021233
/// This function checks all dimensions of the inner vectors and returns
@@ -1205,7 +1236,7 @@ impl<T: Element> PyArray<T, Ix3> {
12051236
/// # Example
12061237
///
12071238
/// ```
1208-
/// use numpy::PyArray;
1239+
/// use numpy::{PyArray, PyArrayMethods};
12091240
/// use pyo3::Python;
12101241
/// use ndarray::array;
12111242
///
@@ -1214,7 +1245,7 @@ impl<T: Element> PyArray<T, Ix3> {
12141245
/// vec![vec![111, 112], vec![121, 122]],
12151246
/// vec![vec![211, 212], vec![221, 222]],
12161247
/// ];
1217-
/// let pyarray = PyArray::from_vec3(py, &vec3).unwrap();
1248+
/// let pyarray = PyArray::from_vec3_bound(py, &vec3).unwrap();
12181249
/// assert_eq!(
12191250
/// pyarray.readonly().as_array(),
12201251
/// array![[[111, 112], [121, 122]], [[211, 212], [221, 222]]]
@@ -1224,10 +1255,13 @@ impl<T: Element> PyArray<T, Ix3> {
12241255
/// vec![vec![111, 112], vec![121, 122]],
12251256
/// vec![vec![211], vec![221, 222]],
12261257
/// ];
1227-
/// assert!(PyArray::from_vec3(py, &ragged_vec3).is_err());
1258+
/// assert!(PyArray::from_vec3_bound(py, &ragged_vec3).is_err());
12281259
/// });
12291260
/// ```
1230-
pub fn from_vec3<'py>(py: Python<'py>, v: &[Vec<Vec<T>>]) -> Result<&'py Self, FromVecError> {
1261+
pub fn from_vec3_bound<'py>(
1262+
py: Python<'py>,
1263+
v: &[Vec<Vec<T>>],
1264+
) -> Result<Bound<'py, Self>, FromVecError> {
12311265
let len2 = v.first().map_or(0, |v| v.len());
12321266
let len3 = v.first().map_or(0, |v| v.first().map_or(0, |v| v.len()));
12331267
let dims = [v.len(), len2, len3];
@@ -1248,7 +1282,7 @@ impl<T: Element> PyArray<T, Ix3> {
12481282
clone_elements(v, &mut data_ptr);
12491283
}
12501284
}
1251-
Ok(array.into_gil_ref())
1285+
Ok(array)
12521286
}
12531287
}
12541288
}
@@ -2319,7 +2353,7 @@ mod tests {
23192353
#[test]
23202354
fn test_dyn_to_owned_array() {
23212355
Python::with_gil(|py| {
2322-
let array = PyArray::from_vec2(py, &[vec![1, 2], vec![3, 4]])
2356+
let array = PyArray::from_vec2_bound(py, &[vec![1, 2], vec![3, 4]])
23232357
.unwrap()
23242358
.to_dyn()
23252359
.to_owned_array();

src/strings.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ use crate::npyffi::NPY_TYPES;
4747
///
4848
/// ```rust
4949
/// # use pyo3::Python;
50-
/// use numpy::{PyArray1, PyFixedString};
50+
/// use numpy::{PyArray1, PyUntypedArrayMethods, PyFixedString};
5151
///
5252
/// # Python::with_gil(|py| {
53-
/// let array = PyArray1::<PyFixedString<3>>::from_vec(py, vec![[b'f', b'o', b'o'].into()]);
53+
/// let array = PyArray1::<PyFixedString<3>>::from_vec_bound(py, vec![[b'f', b'o', b'o'].into()]);
5454
///
5555
/// assert!(array.dtype().to_string().contains("S3"));
5656
/// # });
@@ -110,10 +110,10 @@ unsafe impl<const N: usize> Element for PyFixedString<N> {
110110
///
111111
/// ```rust
112112
/// # use pyo3::Python;
113-
/// use numpy::{PyArray1, PyFixedUnicode};
113+
/// use numpy::{PyArray1, PyUntypedArrayMethods, PyFixedUnicode};
114114
///
115115
/// # Python::with_gil(|py| {
116-
/// let array = PyArray1::<PyFixedUnicode<3>>::from_vec(py, vec![[b'b' as _, b'a' as _, b'r' as _].into()]);
116+
/// let array = PyArray1::<PyFixedUnicode<3>>::from_vec_bound(py, vec![[b'b' as _, b'a' as _, b'r' as _].into()]);
117117
///
118118
/// assert!(array.dtype().to_string().contains("U3"));
119119
/// # });

src/untyped_array.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,14 @@ impl PyUntypedArray {
9999
/// # Example
100100
///
101101
/// ```
102+
/// use numpy::prelude::*;
102103
/// use numpy::{dtype_bound, PyArray};
103104
/// use pyo3::Python;
104105
///
105106
/// Python::with_gil(|py| {
106-
/// let array = PyArray::from_vec(py, vec![1_i32, 2, 3]);
107+
/// let array = PyArray::from_vec_bound(py, vec![1_i32, 2, 3]);
107108
///
108-
/// assert!(array.dtype().is_equiv_to(dtype_bound::<i32>(py).as_gil_ref()));
109+
/// assert!(array.dtype().is_equiv_to(&dtype_bound::<i32>(py)));
109110
/// });
110111
/// ```
111112
///
@@ -269,13 +270,14 @@ pub trait PyUntypedArrayMethods<'py>: Sealed {
269270
/// # Example
270271
///
271272
/// ```
273+
/// use numpy::prelude::*;
272274
/// use numpy::{dtype_bound, PyArray};
273275
/// use pyo3::Python;
274276
///
275277
/// Python::with_gil(|py| {
276-
/// let array = PyArray::from_vec(py, vec![1_i32, 2, 3]);
278+
/// let array = PyArray::from_vec_bound(py, vec![1_i32, 2, 3]);
277279
///
278-
/// assert!(array.dtype().is_equiv_to(dtype_bound::<i32>(py).as_gil_ref()));
280+
/// assert!(array.dtype().is_equiv_to(&dtype_bound::<i32>(py)));
279281
/// });
280282
/// ```
281283
///

tests/array.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ fn is_instance() {
195195
#[test]
196196
fn from_vec2() {
197197
Python::with_gil(|py| {
198-
let pyarray = PyArray::from_vec2(py, &[vec![1, 2, 3], vec![4, 5, 6]]).unwrap();
198+
let pyarray = PyArray::from_vec2_bound(py, &[vec![1, 2, 3], vec![4, 5, 6]]).unwrap();
199199

200200
assert_eq!(pyarray.readonly().as_array(), array![[1, 2, 3], [4, 5, 6]]);
201201
});
@@ -204,7 +204,7 @@ fn from_vec2() {
204204
#[test]
205205
fn from_vec2_ragged() {
206206
Python::with_gil(|py| {
207-
let pyarray = PyArray::from_vec2(py, &[vec![1, 2, 3], vec![4, 5]]);
207+
let pyarray = PyArray::from_vec2_bound(py, &[vec![1, 2, 3], vec![4, 5]]);
208208

209209
let err = pyarray.unwrap_err();
210210
assert_eq!(err.to_string(), "invalid length: 2, but expected 3");
@@ -214,7 +214,7 @@ fn from_vec2_ragged() {
214214
#[test]
215215
fn from_vec3() {
216216
Python::with_gil(|py| {
217-
let pyarray = PyArray::from_vec3(
217+
let pyarray = PyArray::from_vec3_bound(
218218
py,
219219
&[
220220
vec![vec![1, 2], vec![3, 4]],
@@ -234,7 +234,7 @@ fn from_vec3() {
234234
#[test]
235235
fn from_vec3_ragged() {
236236
Python::with_gil(|py| {
237-
let pyarray = PyArray::from_vec3(
237+
let pyarray = PyArray::from_vec3_bound(
238238
py,
239239
&[
240240
vec![vec![1, 2], vec![3, 4]],
@@ -246,7 +246,7 @@ fn from_vec3_ragged() {
246246
let err = pyarray.unwrap_err();
247247
assert_eq!(err.to_string(), "invalid length: 1, but expected 2");
248248

249-
let pyarray = PyArray::from_vec3(
249+
let pyarray = PyArray::from_vec3_bound(
250250
py,
251251
&[
252252
vec![vec![1, 2], vec![3, 4]],

0 commit comments

Comments
 (0)