diff --git a/src/array.rs b/src/array.rs index 7c706b848..45cddbd43 100644 --- a/src/array.rs +++ b/src/array.rs @@ -214,15 +214,15 @@ impl PyArray { /// # Example /// /// ``` - /// use numpy::PyArray1; + /// use numpy::{PyArray1, PyArrayMethods}; /// use pyo3::{Py, Python}; /// /// let array: Py> = Python::with_gil(|py| { - /// PyArray1::zeros(py, 5, false).to_owned() + /// PyArray1::zeros_bound(py, 5, false).unbind() /// }); /// /// Python::with_gil(|py| { - /// assert_eq!(array.as_ref(py).readonly().as_slice().unwrap(), [0.0; 5]); + /// assert_eq!(array.bind(py).readonly().as_slice().unwrap(), [0.0; 5]); /// }); /// ``` #[deprecated(since = "0.21.0", note = "use Bound::unbind() instead")] @@ -495,6 +495,18 @@ impl PyArray { ) } + /// Deprecated form of [`PyArray::zeros_bound`] + #[deprecated( + since = "0.21.0", + note = "will be replaced by `PyArray::zeros_bound` in the future" + )] + pub fn zeros<'py, ID>(py: Python<'py>, dims: ID, is_fortran: bool) -> &Self + where + ID: IntoDimension, + { + Self::zeros_bound(py, dims, is_fortran).into_gil_ref() + } + /// Construct a new NumPy array filled with zeros. /// /// If `is_fortran` is true, then it has Fortran/column-major order, @@ -508,11 +520,11 @@ impl PyArray { /// # Example /// /// ``` - /// use numpy::PyArray2; + /// use numpy::{PyArray2, PyArrayMethods}; /// use pyo3::Python; /// /// Python::with_gil(|py| { - /// let pyarray: &PyArray2 = PyArray2::zeros(py, [2, 2], true); + /// let pyarray = PyArray2::::zeros_bound(py, [2, 2], true); /// /// assert_eq!(pyarray.readonly().as_slice().unwrap(), [0; 4]); /// }); @@ -520,7 +532,7 @@ impl PyArray { /// /// [numpy-zeros]: https://numpy.org/doc/stable/reference/generated/numpy.zeros.html /// [PyArray_Zeros]: https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_Zeros - pub fn zeros<'py, ID>(py: Python<'py>, dims: ID, is_fortran: bool) -> &Self + pub fn zeros_bound(py: Python<'_>, dims: ID, is_fortran: bool) -> Bound<'_, Self> where ID: IntoDimension, { @@ -533,7 +545,7 @@ impl PyArray { T::get_dtype_bound(py).into_dtype_ptr(), if is_fortran { -1 } else { 0 }, ); - Self::from_owned_ptr(py, ptr) + Bound::from_owned_ptr(py, ptr).downcast_into_unchecked() } } @@ -1308,11 +1320,11 @@ impl PyArray { /// # Example /// /// ``` - /// use numpy::PyArray; + /// use numpy::{PyArray, PyArrayMethods, PyUntypedArrayMethods}; /// use pyo3::Python; /// /// Python::with_gil(|py| { - /// let pyarray = PyArray::::zeros(py, (10, 10), false); + /// let pyarray = PyArray::::zeros_bound(py, (10, 10), false); /// assert_eq!(pyarray.shape(), [10, 10]); /// /// unsafe { @@ -1838,11 +1850,11 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> { /// # Example /// /// ``` - /// use numpy::PyArray; + /// use numpy::{PyArray, PyArrayMethods, PyUntypedArrayMethods}; /// use pyo3::Python; /// /// Python::with_gil(|py| { - /// let pyarray = PyArray::::zeros(py, (10, 10), false); + /// let pyarray = PyArray::::zeros_bound(py, (10, 10), false); /// assert_eq!(pyarray.shape(), [10, 10]); /// /// unsafe { diff --git a/src/borrow/mod.rs b/src/borrow/mod.rs index 26b189936..0cc436567 100644 --- a/src/borrow/mod.rs +++ b/src/borrow/mod.rs @@ -15,11 +15,11 @@ //! ```rust //! # use std::panic::{catch_unwind, AssertUnwindSafe}; //! # -//! use numpy::PyArray1; +//! use numpy::{PyArray1, PyArrayMethods}; //! use ndarray::Zip; -//! use pyo3::Python; +//! use pyo3::{Python, Bound}; //! -//! fn add(x: &PyArray1, y: &PyArray1, z: &PyArray1) { +//! fn add(x: &Bound<'_, PyArray1>, y: &Bound<'_, PyArray1>, z: &Bound<'_, PyArray1>) { //! let x1 = x.readonly(); //! let y1 = y.readonly(); //! let mut z1 = z.readwrite(); @@ -41,19 +41,19 @@ //! } //! //! Python::with_gil(|py| { -//! let x = PyArray1::::zeros(py, 42, false); -//! let y = PyArray1::::zeros(py, 42, false); -//! let z = PyArray1::::zeros(py, 42, false); +//! let x = PyArray1::::zeros_bound(py, 42, false); +//! let y = PyArray1::::zeros_bound(py, 42, false); +//! let z = PyArray1::::zeros_bound(py, 42, false); //! //! // Will work as the three arrays are distinct. -//! add(x, y, z); +//! add(&x, &y, &z); //! //! // Will work as `x1` and `y1` are compatible borrows. -//! add(x, x, z); +//! add(&x, &x, &z); //! //! // Will fail at runtime due to conflict between `y1` and `z1`. //! let res = catch_unwind(AssertUnwindSafe(|| { -//! add(x, y, y); +//! add(&x, &y, &y); //! })); //! assert!(res.is_err()); //! }); @@ -91,15 +91,15 @@ //! ```rust //! # use std::panic::{catch_unwind, AssertUnwindSafe}; //! # -//! use numpy::PyArray2; -//! use pyo3::{types::IntoPyDict, Python}; +//! use numpy::{PyArray2, PyArrayMethods}; +//! use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python}; //! //! Python::with_gil(|py| { -//! let array = PyArray2::::zeros(py, (10, 10), false); -//! let locals = [("array", array)].into_py_dict(py); +//! let array = PyArray2::::zeros_bound(py, (10, 10), false); +//! let locals = [("array", array)].into_py_dict_bound(py); //! -//! let view1 = py.eval("array[:, ::3]", None, Some(locals)).unwrap().downcast::>().unwrap(); -//! let view2 = py.eval("array[:, 1::3]", None, Some(locals)).unwrap().downcast::>().unwrap(); +//! let view1 = py.eval_bound("array[:, ::3]", None, Some(&locals)).unwrap().downcast_into::>().unwrap(); +//! let view2 = py.eval_bound("array[:, 1::3]", None, Some(&locals)).unwrap().downcast_into::>().unwrap(); //! //! // A false conflict as the views do not actually share any elements. //! let res = catch_unwind(AssertUnwindSafe(|| { @@ -589,7 +589,7 @@ mod tests { #[test] fn test_debug_formatting() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (1, 2, 3), false); + let array = PyArray::::zeros_bound(py, (1, 2, 3), false); { let shared = array.readonly(); @@ -615,7 +615,7 @@ mod tests { #[should_panic(expected = "AlreadyBorrowed")] fn cannot_clone_exclusive_borrow_via_deref() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (3, 2, 1), false); + let array = PyArray::::zeros_bound(py, (3, 2, 1), false); let exclusive = array.readwrite(); let _shared = exclusive.clone(); @@ -625,14 +625,14 @@ mod tests { #[test] fn failed_resize_does_not_double_release() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, 10, false); + let array = PyArray::::zeros_bound(py, 10, false); // The view will make the internal reference check of `PyArray_Resize` fail. - let locals = [("array", array)].into_py_dict(py); + let locals = [("array", &array)].into_py_dict_bound(py); let _view = py - .eval("array[:]", None, Some(locals)) + .eval_bound("array[:]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); let exclusive = array.readwrite(); @@ -643,7 +643,7 @@ mod tests { #[test] fn ineffective_resize_does_not_conflict() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, 10, false); + let array = PyArray::::zeros_bound(py, 10, false); let exclusive = array.readwrite(); assert!(exclusive.resize(10).is_ok()); diff --git a/src/borrow/shared.rs b/src/borrow/shared.rs index feccadc7c..a92e6d35d 100644 --- a/src/borrow/shared.rs +++ b/src/borrow/shared.rs @@ -447,8 +447,9 @@ mod tests { use ndarray::Array; use pyo3::types::IntoPyDict; - use crate::array::{PyArray, PyArray1, PyArray2, PyArray3}; + use crate::array::{PyArray, PyArray1, PyArray2, PyArray3, PyArrayMethods}; use crate::convert::IntoPyArray; + use crate::untyped_array::PyUntypedArrayMethods; fn get_borrow_flags<'py>(py: Python<'py>) -> &'py BorrowFlagsInner { let shared = get_or_insert_shared(py).unwrap(); @@ -459,13 +460,13 @@ mod tests { #[test] fn without_base_object() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (1, 2, 3), false); + let array = PyArray::::zeros_bound(py, (1, 2, 3), false); let base = unsafe { (*array.as_array_ptr()).base }; assert!(base.is_null()); let base_address = base_address(py, array.as_array_ptr()); - assert_eq!(base_address, array as *const _ as *mut c_void); + assert_eq!(base_address, array.as_ptr().cast()); let data_range = data_range(array.as_array_ptr()); assert_eq!(data_range.0, array.data() as *mut c_char); @@ -494,25 +495,25 @@ mod tests { #[test] fn view_without_base_object() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (1, 2, 3), false); + let array = PyArray::::zeros_bound(py, (1, 2, 3), false); - let locals = [("array", array)].into_py_dict(py); + let locals = [("array", &array)].into_py_dict_bound(py); let view = py - .eval("array[:,:,0]", None, Some(locals)) + .eval_bound("array[:,:,0]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); assert_ne!( - view as *const _ as *mut c_void, - array as *const _ as *mut c_void + view.as_ptr().cast::(), + array.as_ptr().cast::() ); let base = unsafe { (*view.as_array_ptr()).base }; - assert_eq!(base as *mut c_void, array as *const _ as *mut c_void); + assert_eq!(base as *mut c_void, array.as_ptr().cast::()); let base_address = base_address(py, view.as_array_ptr()); - assert_ne!(base_address, view as *const _ as *mut c_void); - assert_eq!(base_address, base as *mut c_void); + assert_ne!(base_address, view.as_ptr().cast::()); + assert_eq!(base_address, base.cast::()); let data_range = data_range(view.as_array_ptr()); assert_eq!(data_range.0, array.data() as *mut c_char); @@ -556,43 +557,43 @@ mod tests { #[test] fn view_of_view_without_base_object() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (1, 2, 3), false); + let array = PyArray::::zeros_bound(py, (1, 2, 3), false); - let locals = [("array", array)].into_py_dict(py); + let locals = [("array", &array)].into_py_dict_bound(py); let view1 = py - .eval("array[:,:,0]", None, Some(locals)) + .eval_bound("array[:,:,0]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); assert_ne!( - view1 as *const _ as *mut c_void, - array as *const _ as *mut c_void + view1.as_ptr().cast::(), + array.as_ptr().cast::() ); - let locals = [("view1", view1)].into_py_dict(py); + let locals = [("view1", &view1)].into_py_dict(py); let view2 = py .eval("view1[:,0]", None, Some(locals)) .unwrap() .downcast::>() .unwrap(); assert_ne!( - view2 as *const _ as *mut c_void, - array as *const _ as *mut c_void + view2.as_ptr().cast::(), + array.as_ptr().cast::() ); assert_ne!( - view2 as *const _ as *mut c_void, - view1 as *const _ as *mut c_void + view2.as_ptr().cast::(), + view1.as_ptr().cast::() ); let base = unsafe { (*view2.as_array_ptr()).base }; - assert_eq!(base as *mut c_void, array as *const _ as *mut c_void); + assert_eq!(base as *mut c_void, array.as_ptr().cast::()); let base = unsafe { (*view1.as_array_ptr()).base }; - assert_eq!(base as *mut c_void, array as *const _ as *mut c_void); + assert_eq!(base as *mut c_void, array.as_ptr().cast::()); let base_address = base_address(py, view2.as_array_ptr()); - assert_ne!(base_address, view2 as *const _ as *mut c_void); - assert_ne!(base_address, view1 as *const _ as *mut c_void); + assert_ne!(base_address, view2.as_ptr().cast::()); + assert_ne!(base_address, view1.as_ptr().cast::()); assert_eq!(base_address, base as *mut c_void); let data_range = data_range(view2.as_array_ptr()); @@ -656,25 +657,25 @@ mod tests { #[test] fn view_with_negative_strides() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (1, 2, 3), false); + let array = PyArray::::zeros_bound(py, (1, 2, 3), false); - let locals = [("array", array)].into_py_dict(py); + let locals = [("array", &array)].into_py_dict_bound(py); let view = py - .eval("array[::-1,:,::-1]", None, Some(locals)) + .eval_bound("array[::-1,:,::-1]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); assert_ne!( - view as *const _ as *mut c_void, - array as *const _ as *mut c_void + view.as_ptr().cast::(), + array.as_ptr().cast::() ); let base = unsafe { (*view.as_array_ptr()).base }; - assert_eq!(base as *mut c_void, array as *const _ as *mut c_void); + assert_eq!(base.cast::(), array.as_ptr().cast::()); let base_address = base_address(py, view.as_array_ptr()); - assert_ne!(base_address, view as *const _ as *mut c_void); - assert_eq!(base_address, base as *mut c_void); + assert_ne!(base_address, view.as_ptr().cast::()); + assert_eq!(base_address, base.cast::()); let data_range = data_range(view.as_array_ptr()); assert_eq!(view.data(), unsafe { array.data().offset(2) }); @@ -688,13 +689,13 @@ mod tests { #[test] fn array_with_zero_dimensions() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (1, 0, 3), false); + let array = PyArray::::zeros_bound(py, (1, 0, 3), false); let base = unsafe { (*array.as_array_ptr()).base }; assert!(base.is_null()); let base_address = base_address(py, array.as_array_ptr()); - assert_eq!(base_address, array as *const _ as *mut c_void); + assert_eq!(base_address, array.as_ptr().cast::()); let data_range = data_range(array.as_array_ptr()); assert_eq!(data_range.0, array.data() as *mut c_char); @@ -705,13 +706,13 @@ mod tests { #[test] fn view_with_non_dividing_strides() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (10, 10), false); - let locals = [("array", array)].into_py_dict(py); + let array = PyArray::::zeros_bound(py, (10, 10), false); + let locals = [("array", array)].into_py_dict_bound(py); let view1 = py - .eval("array[:,::3]", None, Some(locals)) + .eval_bound("array[:,::3]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); let key1 = borrow_key(view1.as_array_ptr()); @@ -720,9 +721,9 @@ mod tests { assert_eq!(key1.gcd_strides, 8); let view2 = py - .eval("array[:,1::3]", None, Some(locals)) + .eval_bound("array[:,1::3]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); let key2 = borrow_key(view2.as_array_ptr()); @@ -731,9 +732,9 @@ mod tests { assert_eq!(key2.gcd_strides, 8); let view3 = py - .eval("array[:,::2]", None, Some(locals)) + .eval_bound("array[:,::2]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); let key3 = borrow_key(view3.as_array_ptr()); @@ -742,9 +743,9 @@ mod tests { assert_eq!(key3.gcd_strides, 16); let view4 = py - .eval("array[:,1::2]", None, Some(locals)) + .eval_bound("array[:,1::2]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); let key4 = borrow_key(view4.as_array_ptr()); @@ -764,8 +765,8 @@ mod tests { #[test] fn borrow_multiple_arrays() { Python::with_gil(|py| { - let array1 = PyArray::::zeros(py, 10, false); - let array2 = PyArray::::zeros(py, 10, false); + let array1 = PyArray::::zeros_bound(py, 10, false); + let array2 = PyArray::::zeros_bound(py, 10, false); let base1 = base_address(py, array1.as_array_ptr()); let base2 = base_address(py, array2.as_array_ptr()); @@ -809,7 +810,7 @@ mod tests { #[test] fn borrow_multiple_views() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, 10, false); + let array = PyArray::::zeros_bound(py, 10, false); let base = base_address(py, array.as_array_ptr()); let locals = [("array", array)].into_py_dict(py); diff --git a/src/datetime.rs b/src/datetime.rs index adcd77cf5..38036c5a4 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -254,7 +254,7 @@ mod tests { types::{PyDict, PyModule}, }; - use crate::array::PyArray1; + use crate::array::{PyArray1, PyArrayMethods}; #[test] fn from_python_to_rust() { @@ -283,7 +283,7 @@ mod tests { #[test] fn from_rust_to_python() { Python::with_gil(|py| { - let array = PyArray1::>::zeros(py, 1, false); + let array = PyArray1::>::zeros_bound(py, 1, false); *array.readwrite().get_mut(0).unwrap() = Timedelta::::from(5); diff --git a/src/untyped_array.rs b/src/untyped_array.rs index cf5bd385c..15eba035a 100644 --- a/src/untyped_array.rs +++ b/src/untyped_array.rs @@ -28,11 +28,12 @@ use crate::npyffi; /// ``` /// # use pyo3::prelude::*; /// use pyo3::exceptions::PyTypeError; -/// use numpy::{Element, PyUntypedArray, PyArray1, dtype}; +/// use numpy::{Element, PyUntypedArray, PyArray1, dtype_bound}; +/// use numpy::{PyUntypedArrayMethods, PyArrayMethods, PyArrayDescrMethods}; /// /// #[pyfunction] -/// fn entry_point(py: Python, array: &PyUntypedArray) -> PyResult<()> { -/// fn implementation(array: &PyArray1) -> PyResult<()> { +/// fn entry_point(py: Python<'_>, array: &Bound<'_, PyUntypedArray>) -> PyResult<()> { +/// fn implementation(array: &Bound<'_, PyArray1>) -> PyResult<()> { /// /* .. */ /// /// Ok(()) @@ -40,12 +41,12 @@ use crate::npyffi; /// /// let element_type = array.dtype(); /// -/// if element_type.is_equiv_to(dtype::(py)) { -/// let array: &PyArray1 = array.downcast()?; +/// if element_type.is_equiv_to(&dtype_bound::(py)) { +/// let array = array.downcast::>()?; /// /// implementation(array) -/// } else if element_type.is_equiv_to(dtype::(py)) { -/// let array: &PyArray1 = array.downcast()?; +/// } else if element_type.is_equiv_to(&dtype_bound::(py)) { +/// let array = array.downcast::>()?; /// /// implementation(array) /// } else { @@ -54,8 +55,8 @@ use crate::npyffi; /// } /// # /// # Python::with_gil(|py| { -/// # let array = PyArray1::::zeros(py, 42, false); -/// # entry_point(py, array) +/// # let array = PyArray1::::zeros_bound(py, 42, false); +/// # entry_point(py, array.as_untyped()) /// # }).unwrap(); /// ``` #[repr(transparent)] @@ -160,11 +161,11 @@ impl PyUntypedArray { /// # Example /// /// ``` - /// use numpy::PyArray3; + /// use numpy::{PyArray3, PyUntypedArrayMethods}; /// use pyo3::Python; /// /// Python::with_gil(|py| { - /// let arr = PyArray3::::zeros(py, [4, 5, 6], false); + /// let arr = PyArray3::::zeros_bound(py, [4, 5, 6], false); /// /// assert_eq!(arr.ndim(), 3); /// }); @@ -184,11 +185,11 @@ impl PyUntypedArray { /// # Example /// /// ``` - /// use numpy::PyArray3; + /// use numpy::{PyArray3, PyUntypedArrayMethods}; /// use pyo3::Python; /// /// Python::with_gil(|py| { - /// let arr = PyArray3::::zeros(py, [4, 5, 6], false); + /// let arr = PyArray3::::zeros_bound(py, [4, 5, 6], false); /// /// assert_eq!(arr.strides(), &[240, 48, 8]); /// }); @@ -216,11 +217,11 @@ impl PyUntypedArray { /// # Example /// /// ``` - /// use numpy::PyArray3; + /// use numpy::{PyArray3, PyUntypedArrayMethods}; /// use pyo3::Python; /// /// Python::with_gil(|py| { - /// let arr = PyArray3::::zeros(py, [4, 5, 6], false); + /// let arr = PyArray3::::zeros_bound(py, [4, 5, 6], false); /// /// assert_eq!(arr.shape(), &[4, 5, 6]); /// }); @@ -329,11 +330,11 @@ pub trait PyUntypedArrayMethods<'py>: sealed::Sealed { /// # Example /// /// ``` - /// use numpy::PyArray3; + /// use numpy::{PyArray3, PyUntypedArrayMethods}; /// use pyo3::Python; /// /// Python::with_gil(|py| { - /// let arr = PyArray3::::zeros(py, [4, 5, 6], false); + /// let arr = PyArray3::::zeros_bound(py, [4, 5, 6], false); /// /// assert_eq!(arr.ndim(), 3); /// }); @@ -353,11 +354,11 @@ pub trait PyUntypedArrayMethods<'py>: sealed::Sealed { /// # Example /// /// ``` - /// use numpy::PyArray3; + /// use numpy::{PyArray3, PyUntypedArrayMethods}; /// use pyo3::Python; /// /// Python::with_gil(|py| { - /// let arr = PyArray3::::zeros(py, [4, 5, 6], false); + /// let arr = PyArray3::::zeros_bound(py, [4, 5, 6], false); /// /// assert_eq!(arr.strides(), &[240, 48, 8]); /// }); @@ -385,11 +386,11 @@ pub trait PyUntypedArrayMethods<'py>: sealed::Sealed { /// # Example /// /// ``` - /// use numpy::PyArray3; + /// use numpy::{PyArray3, PyUntypedArrayMethods}; /// use pyo3::Python; /// /// Python::with_gil(|py| { - /// let arr = PyArray3::::zeros(py, [4, 5, 6], false); + /// let arr = PyArray3::::zeros_bound(py, [4, 5, 6], false); /// /// assert_eq!(arr.shape(), &[4, 5, 6]); /// }); diff --git a/tests/array.rs b/tests/array.rs index ed8f77222..65bc7621c 100644 --- a/tests/array.rs +++ b/tests/array.rs @@ -4,9 +4,11 @@ use std::mem::size_of; use half::{bf16, f16}; use ndarray::{array, s, Array1, Dim}; use numpy::{ - dtype_bound, get_array_module, npyffi::NPY_ORDER, pyarray, PyArray, PyArray1, PyArray2, - PyArrayDescr, PyArrayDescrMethods, PyArrayDyn, PyArrayMethods, PyFixedString, PyFixedUnicode, - PyUntypedArrayMethods, ToPyArray, + array::{PyArray0Methods, PyArrayMethods}, + dtype_bound, get_array_module, + npyffi::NPY_ORDER, + pyarray, PyArray, PyArray1, PyArray2, PyArrayDescr, PyArrayDescrMethods, PyArrayDyn, + PyFixedString, PyFixedUnicode, PyUntypedArrayMethods, ToPyArray, }; use pyo3::{ py_run, pyclass, pymethods, @@ -34,7 +36,7 @@ fn new_c_order() { Python::with_gil(|py| { let dims = [3, 5]; - let arr = PyArray::::zeros(py, dims, false); + let arr = PyArray::::zeros_bound(py, dims, false); assert!(arr.ndim() == 2); assert!(arr.dims() == dims); @@ -56,7 +58,7 @@ fn new_fortran_order() { Python::with_gil(|py| { let dims = [3, 5]; - let arr = PyArray::::zeros(py, dims, true); + let arr = PyArray::::zeros_bound(py, dims, true); assert!(arr.ndim() == 2); assert!(arr.dims() == dims); @@ -78,7 +80,7 @@ fn tuple_as_dim() { Python::with_gil(|py| { let dims = (3, 5); - let arr = PyArray::::zeros(py, dims, false); + let arr = PyArray::::zeros_bound(py, dims, false); assert!(arr.ndim() == 2); assert!(arr.dims() == [3, 5]); @@ -88,7 +90,7 @@ fn tuple_as_dim() { #[test] fn rank_zero_array_has_invalid_strides_dimensions() { Python::with_gil(|py| { - let arr = PyArray::::zeros(py, (), false); + let arr = PyArray::::zeros_bound(py, (), false); assert_eq!(arr.ndim(), 0); assert_eq!(arr.strides(), &[]); @@ -106,7 +108,7 @@ fn zeros() { Python::with_gil(|py| { let dims = [3, 4]; - let arr = PyArray::::zeros(py, dims, false); + let arr = PyArray::::zeros_bound(py, dims, false); assert!(arr.ndim() == 2); assert!(arr.dims() == dims); @@ -114,7 +116,7 @@ fn zeros() { let size = size_of::() as isize; assert!(arr.strides() == [dims[1] as isize * size, size]); - let arr = PyArray::::zeros(py, dims, true); + let arr = PyArray::::zeros_bound(py, dims, true); assert!(arr.ndim() == 2); assert!(arr.dims() == dims); @@ -137,7 +139,7 @@ fn arange() { #[test] fn as_array() { Python::with_gil(|py| { - let pyarr = PyArray::::zeros(py, [3, 2, 4], false).readonly(); + let pyarr = PyArray::::zeros_bound(py, [3, 2, 4], false).readonly(); let arr = pyarr.as_array(); assert_eq!(pyarr.shape(), arr.shape()); @@ -171,7 +173,7 @@ fn as_raw_array() { #[test] fn as_slice() { Python::with_gil(|py| { - let arr = PyArray::::zeros(py, [3, 2, 4], false); + let arr = PyArray::::zeros_bound(py, [3, 2, 4], false); assert_eq!(arr.readonly().as_slice().unwrap().len(), 3 * 2 * 4); let not_contiguous = not_contiguous_array(py); @@ -183,7 +185,7 @@ fn as_slice() { #[test] fn is_instance() { Python::with_gil(|py| { - let arr = PyArray2::::zeros(py, [3, 5], false); + let arr = PyArray2::::zeros_bound(py, [3, 5], false); assert!(arr.is_instance_of::>()); assert!(!arr.is_instance_of::()); diff --git a/tests/borrow.rs b/tests/borrow.rs index 576eb9fc1..24cf37ad3 100644 --- a/tests/borrow.rs +++ b/tests/borrow.rs @@ -4,13 +4,17 @@ use numpy::{ array::PyArrayMethods, npyffi::NPY_ARRAY_WRITEABLE, PyArray, PyArray1, PyArray2, PyReadonlyArray3, PyReadwriteArray3, PyUntypedArrayMethods, }; -use pyo3::{py_run, pyclass, pymethods, types::IntoPyDict, Py, PyAny, PyNativeType, Python}; +use pyo3::{ + py_run, pyclass, pymethods, + types::{IntoPyDict, PyAnyMethods}, + Py, Python, +}; #[test] fn distinct_borrows() { Python::with_gil(|py| { - let array1 = PyArray::::zeros(py, (1, 2, 3), false); - let array2 = PyArray::::zeros(py, (1, 2, 3), false); + let array1 = PyArray::::zeros_bound(py, (1, 2, 3), false); + let array2 = PyArray::::zeros_bound(py, (1, 2, 3), false); let exclusive1 = array1.readwrite(); let exclusive2 = array2.readwrite(); @@ -23,7 +27,7 @@ fn distinct_borrows() { #[test] fn multiple_shared_borrows() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (1, 2, 3), false); + let array = PyArray::::zeros_bound(py, (1, 2, 3), false); let shared1 = array.readonly(); let shared2 = array.readonly(); @@ -37,7 +41,7 @@ fn multiple_shared_borrows() { #[should_panic(expected = "AlreadyBorrowed")] fn exclusive_and_shared_borrows() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (1, 2, 3), false); + let array = PyArray::::zeros_bound(py, (1, 2, 3), false); let _exclusive = array.readwrite(); let _shared = array.readonly(); @@ -48,7 +52,7 @@ fn exclusive_and_shared_borrows() { #[should_panic(expected = "AlreadyBorrowed")] fn shared_and_exclusive_borrows() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (1, 2, 3), false); + let array = PyArray::::zeros_bound(py, (1, 2, 3), false); let _shared = array.readonly(); let _exclusive = array.readwrite(); @@ -58,7 +62,7 @@ fn shared_and_exclusive_borrows() { #[test] fn multiple_exclusive_borrows() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (1, 2, 3), false); + let array = PyArray::::zeros_bound(py, (1, 2, 3), false); let _exclusive = array.try_readwrite().unwrap(); @@ -70,7 +74,7 @@ fn multiple_exclusive_borrows() { #[test] fn exclusive_borrow_requires_writeable() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (1, 2, 3), false); + let array = PyArray::::zeros_bound(py, (1, 2, 3), false); unsafe { (*array.as_array_ptr()).flags &= !NPY_ARRAY_WRITEABLE; @@ -97,7 +101,7 @@ fn borrows_span_frames() { Python::with_gil(|py| { let borrower = Py::new(py, Borrower).unwrap(); - let array = PyArray::::zeros(py, (1, 2, 3), false); + let array = PyArray::::zeros_bound(py, (1, 2, 3), false); let _exclusive = array.readwrite(); @@ -108,7 +112,7 @@ fn borrows_span_frames() { #[test] fn borrows_span_threads() { Python::with_gil(|py| { - let array = (*PyArray::::zeros(py, (1, 2, 3), false).as_borrowed()).clone(); + let array = PyArray::::zeros_bound(py, (1, 2, 3), false); let _exclusive = array.readwrite(); @@ -131,7 +135,7 @@ fn borrows_span_threads() { #[test] fn shared_borrows_can_be_cloned() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (1, 2, 3), false); + let array = PyArray::::zeros_bound(py, (1, 2, 3), false); let shared1 = array.readonly(); let shared2 = shared1.clone(); @@ -145,20 +149,20 @@ fn shared_borrows_can_be_cloned() { #[should_panic(expected = "AlreadyBorrowed")] fn overlapping_views_conflict() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (1, 2, 3), false); - let locals = [("array", array)].into_py_dict(py); + let array = PyArray::::zeros_bound(py, (1, 2, 3), false); + let locals = [("array", array)].into_py_dict_bound(py); let view1 = py - .eval("array[0,0,0:2]", None, Some(locals)) + .eval_bound("array[0,0,0:2]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); assert_eq!(view1.shape(), [2]); let view2 = py - .eval("array[0,0,1:3]", None, Some(locals)) + .eval_bound("array[0,0,1:3]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); assert_eq!(view2.shape(), [2]); @@ -170,20 +174,20 @@ fn overlapping_views_conflict() { #[test] fn non_overlapping_views_do_not_conflict() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (1, 2, 3), false); - let locals = [("array", array)].into_py_dict(py); + let array = PyArray::::zeros_bound(py, (1, 2, 3), false); + let locals = [("array", array)].into_py_dict_bound(py); let view1 = py - .eval("array[0,0,0:1]", None, Some(locals)) + .eval_bound("array[0,0,0:1]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); assert_eq!(view1.shape(), [1]); let view2 = py - .eval("array[0,0,2:3]", None, Some(locals)) + .eval_bound("array[0,0,2:3]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); assert_eq!(view2.shape(), [1]); @@ -199,20 +203,20 @@ fn non_overlapping_views_do_not_conflict() { #[should_panic(expected = "AlreadyBorrowed")] fn conflict_due_to_overlapping_views() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, 3, false); - let locals = [("array", array)].into_py_dict(py); + let array = PyArray::::zeros_bound(py, 3, false); + let locals = [("array", array)].into_py_dict_bound(py); let view1 = py - .eval("array[0:2]", None, Some(locals)) + .eval_bound("array[0:2]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); assert_eq!(view1.shape(), [2]); let view2 = py - .eval("array[1:3]", None, Some(locals)) + .eval_bound("array[1:3]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); assert_eq!(view2.shape(), [2]); @@ -225,20 +229,20 @@ fn conflict_due_to_overlapping_views() { #[should_panic(expected = "AlreadyBorrowed")] fn conflict_due_to_reborrow_of_overlapping_views() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, 3, false); - let locals = [("array", array)].into_py_dict(py); + let array = PyArray::::zeros_bound(py, 3, false); + let locals = [("array", array)].into_py_dict_bound(py); let view1 = py - .eval("array[0:2]", None, Some(locals)) + .eval_bound("array[0:2]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); assert_eq!(view1.shape(), [2]); let view2 = py - .eval("array[1:3]", None, Some(locals)) + .eval_bound("array[1:3]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); assert_eq!(view2.shape(), [2]); @@ -253,27 +257,27 @@ fn conflict_due_to_reborrow_of_overlapping_views() { #[test] fn interleaved_views_do_not_conflict() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (23, 42, 3), false); - let locals = [("array", array)].into_py_dict(py); + let array = PyArray::::zeros_bound(py, (23, 42, 3), false); + let locals = [("array", array)].into_py_dict_bound(py); let view1 = py - .eval("array[:,:,0]", None, Some(locals)) + .eval_bound("array[:,:,0]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); assert_eq!(view1.shape(), [23, 42]); let view2 = py - .eval("array[:,:,1]", None, Some(locals)) + .eval_bound("array[:,:,1]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); assert_eq!(view2.shape(), [23, 42]); let view3 = py - .eval("array[:,:,2]", None, Some(locals)) + .eval_bound("array[:,:,2]", None, Some(&locals)) .unwrap() - .downcast::>() + .downcast_into::>() .unwrap(); assert_eq!(view2.shape(), [23, 42]); @@ -290,7 +294,7 @@ fn interleaved_views_do_not_conflict() { #[test] fn extract_readonly() { Python::with_gil(|py| { - let ob: &PyAny = PyArray::::zeros(py, (1, 2, 3), false); + let ob = PyArray::::zeros_bound(py, (1, 2, 3), false).into_any(); ob.extract::>().unwrap(); }); } @@ -298,7 +302,7 @@ fn extract_readonly() { #[test] fn extract_readwrite() { Python::with_gil(|py| { - let ob: &PyAny = PyArray::::zeros(py, (1, 2, 3), false); + let ob = PyArray::::zeros_bound(py, (1, 2, 3), false).into_any(); ob.extract::>().unwrap(); }); } @@ -306,7 +310,7 @@ fn extract_readwrite() { #[test] fn readonly_as_array_slice_get() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (1, 2, 3), false); + let array = PyArray::::zeros_bound(py, (1, 2, 3), false); let array = array.readonly(); assert_eq!(array.as_array().shape(), [1, 2, 3]); @@ -318,7 +322,7 @@ fn readonly_as_array_slice_get() { #[test] fn readwrite_as_array_slice() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, (1, 2, 3), false); + let array = PyArray::::zeros_bound(py, (1, 2, 3), false); let mut array = array.readwrite(); assert_eq!(array.as_array().shape(), [1, 2, 3]); @@ -333,7 +337,7 @@ fn readwrite_as_array_slice() { #[test] fn resize_using_exclusive_borrow() { Python::with_gil(|py| { - let array = PyArray::::zeros(py, 3, false); + let array = PyArray::::zeros_bound(py, 3, false); assert_eq!(array.shape(), [3]); let mut array = array.readwrite(); @@ -412,7 +416,7 @@ fn matrix_from_numpy() { }); Python::with_gil(|py| { - let array = PyArray::::zeros(py, (2, 2, 2), false); + let array = PyArray::::zeros_bound(py, (2, 2, 2), false); let array = array.readonly(); let matrix: Option> =