Skip to content

Commit

Permalink
deprecate PyArray::D>::borrow_from_array
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Mar 23, 2024
1 parent b2c94f6 commit 31accc2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
35 changes: 25 additions & 10 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -426,6 +426,24 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
Self::new_with_data(py, dims, strides, data_ptr, container.cast())
}

/// Deprecated form of [`PyArray<T, D>::borrow_from_array_bound`]
///
/// # Safety
/// Same as [`PyArray<T, D>::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<S, D>,
container: &'py PyAny,
) -> &'py Self
where
S: Data<Elem = T>,
{
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
Expand All @@ -447,19 +465,19 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
/// #[pymethods]
/// impl Owner {
/// #[getter]
/// fn array<'py>(this: &'py PyCell<Self>) -> &'py PyArray1<f64> {
/// fn array<'py>(this: Bound<'py, Self>) -> Bound<'py, PyArray1<f64>> {
/// 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<S, D>,
container: &'py PyAny,
) -> &'py Self
container: Bound<'py, PyAny>,
) -> Bound<'py, Self>
where
S: Data<Elem = T>,
{
Expand All @@ -468,16 +486,13 @@ impl<T: Element, D: Dimension> PyArray<T, D> {

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.
Expand Down
8 changes: 4 additions & 4 deletions tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -401,10 +401,10 @@ fn borrow_from_array_works() {
#[pymethods]
impl Owner {
#[getter]
fn array(this: &PyCell<Self>) -> &PyArray1<f64> {
fn array(this: Bound<'_, Self>) -> Bound<'_, PyArray1<f64>> {
let array = &this.borrow().array;

unsafe { PyArray1::borrow_from_array(array, this) }
unsafe { PyArray1::borrow_from_array_bound(array, this.into_any()) }
}
}

Expand Down

0 comments on commit 31accc2

Please sign in to comment.