@@ -292,6 +292,21 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
292
292
D :: from_dimension ( & Dim ( self . shape ( ) ) ) . expect ( DIMENSIONALITY_MISMATCH_ERR )
293
293
}
294
294
295
+ /// Deprecated form of [`PyArray<T, D>::new_bound`]
296
+ ///
297
+ /// # Safety
298
+ /// Same as [`PyArray<T, D>::new_bound`]
299
+ #[ deprecated(
300
+ since = "0.21.0" ,
301
+ note = "will be replaced by `PyArray::new_bound` in the future"
302
+ ) ]
303
+ pub unsafe fn new < ' py , ID > ( py : Python < ' py > , dims : ID , is_fortran : bool ) -> & Self
304
+ where
305
+ ID : IntoDimension < Dim = D > ,
306
+ {
307
+ Self :: new_bound ( py, dims, is_fortran) . into_gil_ref ( )
308
+ }
309
+
295
310
/// Creates a new uninitialized NumPy array.
296
311
///
297
312
/// If `is_fortran` is true, then it has Fortran/column-major order,
@@ -311,12 +326,12 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
311
326
/// # Example
312
327
///
313
328
/// ```
314
- /// use numpy::PyArray3;
329
+ /// use numpy::{ PyArray3, PyArrayMethods, PyUntypedArrayMethods} ;
315
330
/// use pyo3::Python;
316
331
///
317
332
/// Python::with_gil(|py| {
318
333
/// let arr = unsafe {
319
- /// let arr = PyArray3::<i32>::new (py, [4, 5, 6], false);
334
+ /// let arr = PyArray3::<i32>::new_bound (py, [4, 5, 6], false);
320
335
///
321
336
/// for i in 0..4 {
322
337
/// for j in 0..5 {
@@ -332,7 +347,11 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
332
347
/// assert_eq!(arr.shape(), &[4, 5, 6]);
333
348
/// });
334
349
/// ```
335
- pub unsafe fn new < ' py , ID > ( py : Python < ' py > , dims : ID , is_fortran : bool ) -> & Self
350
+ pub unsafe fn new_bound < ' py , ID > (
351
+ py : Python < ' py > ,
352
+ dims : ID ,
353
+ is_fortran : bool ,
354
+ ) -> Bound < ' py , Self >
336
355
where
337
356
ID : IntoDimension < Dim = D > ,
338
357
{
@@ -345,7 +364,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
345
364
dims : ID ,
346
365
strides : * const npy_intp ,
347
366
flag : c_int ,
348
- ) -> & Self
367
+ ) -> Bound < ' py , Self >
349
368
where
350
369
ID : IntoDimension < Dim = D > ,
351
370
{
@@ -362,7 +381,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
362
381
ptr:: null_mut ( ) , // obj
363
382
) ;
364
383
365
- Self :: from_owned_ptr ( py, ptr)
384
+ Bound :: from_owned_ptr ( py, ptr) . downcast_into_unchecked ( )
366
385
}
367
386
368
387
unsafe fn new_with_data < ' py , ID > (
@@ -371,7 +390,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
371
390
strides : * const npy_intp ,
372
391
data_ptr : * const T ,
373
392
container : * mut PyAny ,
374
- ) -> & ' py Self
393
+ ) -> Bound < ' py , Self >
375
394
where
376
395
ID : IntoDimension < Dim = D > ,
377
396
{
@@ -394,7 +413,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
394
413
container as * mut ffi:: PyObject ,
395
414
) ;
396
415
397
- Self :: from_owned_ptr ( py, ptr)
416
+ Bound :: from_owned_ptr ( py, ptr) . downcast_into_unchecked ( )
398
417
}
399
418
400
419
pub ( crate ) unsafe fn from_raw_parts < ' py > (
@@ -403,7 +422,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
403
422
strides : * const npy_intp ,
404
423
data_ptr : * const T ,
405
424
container : PySliceContainer ,
406
- ) -> & ' py Self {
425
+ ) -> Bound < ' py , Self > {
407
426
let container = Bound :: new ( py, container)
408
427
. expect ( "Failed to create slice container" )
409
428
. into_ptr ( ) ;
@@ -462,6 +481,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
462
481
data_ptr,
463
482
container as * const PyAny as * mut PyAny ,
464
483
)
484
+ . into_gil_ref ( )
465
485
}
466
486
467
487
/// Construct a new NumPy array filled with zeros.
@@ -568,6 +588,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
568
588
data_ptr,
569
589
PySliceContainer :: from ( arr) ,
570
590
)
591
+ . into_gil_ref ( )
571
592
}
572
593
}
573
594
@@ -972,6 +993,7 @@ impl<D: Dimension> PyArray<PyObject, D> {
972
993
data_ptr,
973
994
PySliceContainer :: from ( arr) ,
974
995
)
996
+ . into_gil_ref ( )
975
997
}
976
998
}
977
999
}
@@ -1002,10 +1024,10 @@ impl<T: Element> PyArray<T, Ix1> {
1002
1024
/// ```
1003
1025
pub fn from_slice < ' py > ( py : Python < ' py > , slice : & [ T ] ) -> & ' py Self {
1004
1026
unsafe {
1005
- let array = PyArray :: new ( py, [ slice. len ( ) ] , false ) ;
1027
+ let array = PyArray :: new_bound ( py, [ slice. len ( ) ] , false ) ;
1006
1028
let mut data_ptr = array. data ( ) ;
1007
1029
clone_elements ( slice, & mut data_ptr) ;
1008
- array
1030
+ array. into_gil_ref ( )
1009
1031
}
1010
1032
}
1011
1033
@@ -1080,7 +1102,7 @@ impl<T: Element> PyArray<T, Ix2> {
1080
1102
let dims = [ v. len ( ) , len2] ;
1081
1103
// SAFETY: The result of `Self::new` is always safe to drop.
1082
1104
unsafe {
1083
- let array = Self :: new ( py, dims, false ) ;
1105
+ let array = Self :: new_bound ( py, dims, false ) ;
1084
1106
let mut data_ptr = array. data ( ) ;
1085
1107
for v in v {
1086
1108
if v. len ( ) != len2 {
@@ -1089,7 +1111,7 @@ impl<T: Element> PyArray<T, Ix2> {
1089
1111
}
1090
1112
clone_elements ( v, & mut data_ptr) ;
1091
1113
}
1092
- Ok ( array)
1114
+ Ok ( array. into_gil_ref ( ) )
1093
1115
}
1094
1116
}
1095
1117
}
@@ -1131,7 +1153,7 @@ impl<T: Element> PyArray<T, Ix3> {
1131
1153
let dims = [ v. len ( ) , len2, len3] ;
1132
1154
// SAFETY: The result of `Self::new` is always safe to drop.
1133
1155
unsafe {
1134
- let array = Self :: new ( py, dims, false ) ;
1156
+ let array = Self :: new_bound ( py, dims, false ) ;
1135
1157
let mut data_ptr = array. data ( ) ;
1136
1158
for v in v {
1137
1159
if v. len ( ) != len2 {
@@ -1146,7 +1168,7 @@ impl<T: Element> PyArray<T, Ix3> {
1146
1168
clone_elements ( v, & mut data_ptr) ;
1147
1169
}
1148
1170
}
1149
- Ok ( array)
1171
+ Ok ( array. into_gil_ref ( ) )
1150
1172
}
1151
1173
}
1152
1174
}
@@ -1159,14 +1181,14 @@ impl<T: Element, D> PyArray<T, D> {
1159
1181
/// # Example
1160
1182
///
1161
1183
/// ```
1162
- /// use numpy::PyArray;
1184
+ /// use numpy::{ PyArray, PyArrayMethods} ;
1163
1185
/// use pyo3::Python;
1164
1186
///
1165
1187
/// Python::with_gil(|py| {
1166
1188
/// let pyarray_f = PyArray::arange(py, 2.0, 5.0, 1.0);
1167
- /// let pyarray_i = unsafe { PyArray::<i64, _>::new (py, [3], false) };
1189
+ /// let pyarray_i = unsafe { PyArray::<i64, _>::new_bound (py, [3], false) };
1168
1190
///
1169
- /// assert!(pyarray_f.copy_to(pyarray_i).is_ok());
1191
+ /// assert!(pyarray_f.copy_to(pyarray_i.as_gil_ref() ).is_ok());
1170
1192
///
1171
1193
/// assert_eq!(pyarray_i.readonly().as_slice().unwrap(), &[2, 3, 4]);
1172
1194
/// });
@@ -1691,14 +1713,14 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
1691
1713
/// # Example
1692
1714
///
1693
1715
/// ```
1694
- /// use numpy::PyArray;
1716
+ /// use numpy::{ PyArray, PyArrayMethods} ;
1695
1717
/// use pyo3::Python;
1696
1718
///
1697
1719
/// Python::with_gil(|py| {
1698
1720
/// let pyarray_f = PyArray::arange(py, 2.0, 5.0, 1.0);
1699
- /// let pyarray_i = unsafe { PyArray::<i64, _>::new (py, [3], false) };
1721
+ /// let pyarray_i = unsafe { PyArray::<i64, _>::new_bound (py, [3], false) };
1700
1722
///
1701
- /// assert!(pyarray_f.copy_to(pyarray_i).is_ok());
1723
+ /// assert!(pyarray_f.copy_to(pyarray_i.as_gil_ref() ).is_ok());
1702
1724
///
1703
1725
/// assert_eq!(pyarray_i.readonly().as_slice().unwrap(), &[2, 3, 4]);
1704
1726
/// });
0 commit comments