diff --git a/src/array.rs b/src/array.rs index 971126dd3..7a6c085e8 100644 --- a/src/array.rs +++ b/src/array.rs @@ -20,7 +20,7 @@ use pyo3::{ ffi, pyobject_native_type_base, types::{DerefToPyAny, PyAnyMethods, PyModule}, AsPyPointer, Bound, DowncastError, FromPyObject, IntoPy, Py, PyAny, PyErr, PyNativeType, - PyObject, PyResult, PyTypeInfo, Python, ToPyObject, + PyObject, PyResult, PyTypeInfo, Python, }; use crate::borrow::{PyReadonlyArray, PyReadwriteArray}; @@ -426,6 +426,24 @@ impl PyArray { Self::new_with_data(py, dims, strides, data_ptr, container.cast()) } + /// Deprecated form of [`PyArray::borrow_from_array_bound`] + /// + /// # Safety + /// Same as [`PyArray::borrow_from_array_bound`] + #[deprecated( + since = "0.21.0", + note = "will be replaced by `PyArray::borrow_from_array_bound` in the future" + )] + pub unsafe fn borrow_from_array<'py, S>( + array: &ArrayBase, + container: &'py PyAny, + ) -> &'py Self + where + S: Data, + { + Self::borrow_from_array_bound(array, (*container.as_borrowed()).clone()).into_gil_ref() + } + /// Creates a NumPy array backed by `array` and ties its ownership to the Python object `container`. /// /// # Safety @@ -447,19 +465,19 @@ impl PyArray { /// #[pymethods] /// impl Owner { /// #[getter] - /// fn array<'py>(this: &'py PyCell) -> &'py PyArray1 { + /// fn array<'py>(this: Bound<'py, Self>) -> Bound<'py, PyArray1> { /// let array = &this.borrow().array; /// /// // SAFETY: The memory backing `array` will stay valid as long as this object is alive /// // as we do not modify `array` in any way which would cause it to be reallocated. - /// unsafe { PyArray1::borrow_from_array(array, this) } + /// unsafe { PyArray1::borrow_from_array_bound(array, this.into_any()) } /// } /// } /// ``` - pub unsafe fn borrow_from_array<'py, S>( + pub unsafe fn borrow_from_array_bound<'py, S>( array: &ArrayBase, - container: &'py PyAny, - ) -> &'py Self + container: Bound<'py, PyAny>, + ) -> Bound<'py, Self> where S: Data, { @@ -468,16 +486,13 @@ impl PyArray { let py = container.py(); - mem::forget(container.to_object(py)); - Self::new_with_data( py, dims, strides.as_ptr(), data_ptr, - container as *const PyAny as *mut PyAny, + container.into_ptr().cast(), ) - .into_gil_ref() } /// Construct a new NumPy array filled with zeros. diff --git a/tests/array.rs b/tests/array.rs index d7270b711..ed8f77222 100644 --- a/tests/array.rs +++ b/tests/array.rs @@ -5,13 +5,13 @@ 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, PyFixedString, PyFixedUnicode, + PyArrayDescr, PyArrayDescrMethods, PyArrayDyn, PyArrayMethods, PyFixedString, PyFixedUnicode, PyUntypedArrayMethods, ToPyArray, }; use pyo3::{ py_run, pyclass, pymethods, types::{IntoPyDict, PyAnyMethods, PyDict, PyList}, - IntoPy, Py, PyAny, PyCell, PyResult, Python, + Bound, IntoPy, Py, PyAny, PyResult, Python, }; fn get_np_locals<'py>(py: Python<'py>) -> &'py PyDict { @@ -401,10 +401,10 @@ fn borrow_from_array_works() { #[pymethods] impl Owner { #[getter] - fn array(this: &PyCell) -> &PyArray1 { + fn array(this: Bound<'_, Self>) -> Bound<'_, PyArray1> { let array = &this.borrow().array; - unsafe { PyArray1::borrow_from_array(array, this) } + unsafe { PyArray1::borrow_from_array_bound(array, this.into_any()) } } }