@@ -292,6 +292,21 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
292292 D :: from_dimension ( & Dim ( self . shape ( ) ) ) . expect ( DIMENSIONALITY_MISMATCH_ERR )
293293 }
294294
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+
295310 /// Creates a new uninitialized NumPy array.
296311 ///
297312 /// If `is_fortran` is true, then it has Fortran/column-major order,
@@ -311,12 +326,12 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
311326 /// # Example
312327 ///
313328 /// ```
314- /// use numpy::PyArray3;
329+ /// use numpy::{ PyArray3, PyArrayMethods, PyUntypedArrayMethods} ;
315330 /// use pyo3::Python;
316331 ///
317332 /// Python::with_gil(|py| {
318333 /// 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);
320335 ///
321336 /// for i in 0..4 {
322337 /// for j in 0..5 {
@@ -332,7 +347,11 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
332347 /// assert_eq!(arr.shape(), &[4, 5, 6]);
333348 /// });
334349 /// ```
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 >
336355 where
337356 ID : IntoDimension < Dim = D > ,
338357 {
@@ -345,7 +364,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
345364 dims : ID ,
346365 strides : * const npy_intp ,
347366 flag : c_int ,
348- ) -> & Self
367+ ) -> Bound < ' py , Self >
349368 where
350369 ID : IntoDimension < Dim = D > ,
351370 {
@@ -362,7 +381,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
362381 ptr:: null_mut ( ) , // obj
363382 ) ;
364383
365- Self :: from_owned_ptr ( py, ptr)
384+ Bound :: from_owned_ptr ( py, ptr) . downcast_into_unchecked ( )
366385 }
367386
368387 unsafe fn new_with_data < ' py , ID > (
@@ -371,7 +390,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
371390 strides : * const npy_intp ,
372391 data_ptr : * const T ,
373392 container : * mut PyAny ,
374- ) -> & ' py Self
393+ ) -> Bound < ' py , Self >
375394 where
376395 ID : IntoDimension < Dim = D > ,
377396 {
@@ -394,7 +413,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
394413 container as * mut ffi:: PyObject ,
395414 ) ;
396415
397- Self :: from_owned_ptr ( py, ptr)
416+ Bound :: from_owned_ptr ( py, ptr) . downcast_into_unchecked ( )
398417 }
399418
400419 pub ( crate ) unsafe fn from_raw_parts < ' py > (
@@ -403,7 +422,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
403422 strides : * const npy_intp ,
404423 data_ptr : * const T ,
405424 container : PySliceContainer ,
406- ) -> & ' py Self {
425+ ) -> Bound < ' py , Self > {
407426 let container = Bound :: new ( py, container)
408427 . expect ( "Failed to create slice container" )
409428 . into_ptr ( ) ;
@@ -462,6 +481,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
462481 data_ptr,
463482 container as * const PyAny as * mut PyAny ,
464483 )
484+ . into_gil_ref ( )
465485 }
466486
467487 /// Construct a new NumPy array filled with zeros.
@@ -568,6 +588,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
568588 data_ptr,
569589 PySliceContainer :: from ( arr) ,
570590 )
591+ . into_gil_ref ( )
571592 }
572593 }
573594
@@ -972,6 +993,7 @@ impl<D: Dimension> PyArray<PyObject, D> {
972993 data_ptr,
973994 PySliceContainer :: from ( arr) ,
974995 )
996+ . into_gil_ref ( )
975997 }
976998 }
977999}
@@ -1002,10 +1024,10 @@ impl<T: Element> PyArray<T, Ix1> {
10021024 /// ```
10031025 pub fn from_slice < ' py > ( py : Python < ' py > , slice : & [ T ] ) -> & ' py Self {
10041026 unsafe {
1005- let array = PyArray :: new ( py, [ slice. len ( ) ] , false ) ;
1027+ let array = PyArray :: new_bound ( py, [ slice. len ( ) ] , false ) ;
10061028 let mut data_ptr = array. data ( ) ;
10071029 clone_elements ( slice, & mut data_ptr) ;
1008- array
1030+ array. into_gil_ref ( )
10091031 }
10101032 }
10111033
@@ -1080,7 +1102,7 @@ impl<T: Element> PyArray<T, Ix2> {
10801102 let dims = [ v. len ( ) , len2] ;
10811103 // SAFETY: The result of `Self::new` is always safe to drop.
10821104 unsafe {
1083- let array = Self :: new ( py, dims, false ) ;
1105+ let array = Self :: new_bound ( py, dims, false ) ;
10841106 let mut data_ptr = array. data ( ) ;
10851107 for v in v {
10861108 if v. len ( ) != len2 {
@@ -1089,7 +1111,7 @@ impl<T: Element> PyArray<T, Ix2> {
10891111 }
10901112 clone_elements ( v, & mut data_ptr) ;
10911113 }
1092- Ok ( array)
1114+ Ok ( array. into_gil_ref ( ) )
10931115 }
10941116 }
10951117}
@@ -1131,7 +1153,7 @@ impl<T: Element> PyArray<T, Ix3> {
11311153 let dims = [ v. len ( ) , len2, len3] ;
11321154 // SAFETY: The result of `Self::new` is always safe to drop.
11331155 unsafe {
1134- let array = Self :: new ( py, dims, false ) ;
1156+ let array = Self :: new_bound ( py, dims, false ) ;
11351157 let mut data_ptr = array. data ( ) ;
11361158 for v in v {
11371159 if v. len ( ) != len2 {
@@ -1146,7 +1168,7 @@ impl<T: Element> PyArray<T, Ix3> {
11461168 clone_elements ( v, & mut data_ptr) ;
11471169 }
11481170 }
1149- Ok ( array)
1171+ Ok ( array. into_gil_ref ( ) )
11501172 }
11511173 }
11521174}
@@ -1159,14 +1181,14 @@ impl<T: Element, D> PyArray<T, D> {
11591181 /// # Example
11601182 ///
11611183 /// ```
1162- /// use numpy::PyArray;
1184+ /// use numpy::{ PyArray, PyArrayMethods} ;
11631185 /// use pyo3::Python;
11641186 ///
11651187 /// Python::with_gil(|py| {
11661188 /// 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) };
11681190 ///
1169- /// assert!(pyarray_f.copy_to(pyarray_i).is_ok());
1191+ /// assert!(pyarray_f.copy_to(pyarray_i.as_gil_ref() ).is_ok());
11701192 ///
11711193 /// assert_eq!(pyarray_i.readonly().as_slice().unwrap(), &[2, 3, 4]);
11721194 /// });
@@ -1691,14 +1713,14 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
16911713 /// # Example
16921714 ///
16931715 /// ```
1694- /// use numpy::PyArray;
1716+ /// use numpy::{ PyArray, PyArrayMethods} ;
16951717 /// use pyo3::Python;
16961718 ///
16971719 /// Python::with_gil(|py| {
16981720 /// 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) };
17001722 ///
1701- /// assert!(pyarray_f.copy_to(pyarray_i).is_ok());
1723+ /// assert!(pyarray_f.copy_to(pyarray_i.as_gil_ref() ).is_ok());
17021724 ///
17031725 /// assert_eq!(pyarray_i.readonly().as_slice().unwrap(), &[2, 3, 4]);
17041726 /// });
0 commit comments