Skip to content

Commit 6244177

Browse files
Icxoluadamreichold
authored andcommitted
deprecate PyArray::borrow_from_array
1 parent 271b634 commit 6244177

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

src/array.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use pyo3::{
2020
ffi, pyobject_native_type_base,
2121
types::{DerefToPyAny, PyAnyMethods, PyModule},
2222
AsPyPointer, Bound, DowncastError, FromPyObject, IntoPy, Py, PyAny, PyErr, PyNativeType,
23-
PyObject, PyResult, PyTypeInfo, Python, ToPyObject,
23+
PyObject, PyResult, PyTypeInfo, Python,
2424
};
2525

2626
use crate::borrow::{PyReadonlyArray, PyReadwriteArray};
@@ -430,6 +430,24 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
430430
Self::new_with_data(py, dims, strides, data_ptr, container.cast())
431431
}
432432

433+
/// Deprecated form of [`PyArray<T, D>::borrow_from_array_bound`]
434+
///
435+
/// # Safety
436+
/// Same as [`PyArray<T, D>::borrow_from_array_bound`]
437+
#[deprecated(
438+
since = "0.21.0",
439+
note = "will be replaced by `PyArray::borrow_from_array_bound` in the future"
440+
)]
441+
pub unsafe fn borrow_from_array<'py, S>(
442+
array: &ArrayBase<S, D>,
443+
container: &'py PyAny,
444+
) -> &'py Self
445+
where
446+
S: Data<Elem = T>,
447+
{
448+
Self::borrow_from_array_bound(array, (*container.as_borrowed()).clone()).into_gil_ref()
449+
}
450+
433451
/// Creates a NumPy array backed by `array` and ties its ownership to the Python object `container`.
434452
///
435453
/// # Safety
@@ -451,19 +469,19 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
451469
/// #[pymethods]
452470
/// impl Owner {
453471
/// #[getter]
454-
/// fn array<'py>(this: &'py PyCell<Self>) -> &'py PyArray1<f64> {
472+
/// fn array<'py>(this: Bound<'py, Self>) -> Bound<'py, PyArray1<f64>> {
455473
/// let array = &this.borrow().array;
456474
///
457475
/// // SAFETY: The memory backing `array` will stay valid as long as this object is alive
458476
/// // as we do not modify `array` in any way which would cause it to be reallocated.
459-
/// unsafe { PyArray1::borrow_from_array(array, this) }
477+
/// unsafe { PyArray1::borrow_from_array_bound(array, this.into_any()) }
460478
/// }
461479
/// }
462480
/// ```
463-
pub unsafe fn borrow_from_array<'py, S>(
481+
pub unsafe fn borrow_from_array_bound<'py, S>(
464482
array: &ArrayBase<S, D>,
465-
container: &'py PyAny,
466-
) -> &'py Self
483+
container: Bound<'py, PyAny>,
484+
) -> Bound<'py, Self>
467485
where
468486
S: Data<Elem = T>,
469487
{
@@ -472,16 +490,13 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
472490

473491
let py = container.py();
474492

475-
mem::forget(container.to_object(py));
476-
477493
Self::new_with_data(
478494
py,
479495
dims,
480496
strides.as_ptr(),
481497
data_ptr,
482-
container as *const PyAny as *mut PyAny,
498+
container.into_ptr().cast(),
483499
)
484-
.into_gil_ref()
485500
}
486501

487502
/// Construct a new NumPy array filled with zeros.

tests/array.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use numpy::{
1111
use pyo3::{
1212
py_run, pyclass, pymethods,
1313
types::{IntoPyDict, PyAnyMethods, PyDict, PyList},
14-
IntoPy, Py, PyAny, PyCell, PyResult, Python,
14+
Bound, IntoPy, Py, PyAny, PyResult, Python,
1515
};
1616

1717
fn get_np_locals<'py>(py: Python<'py>) -> &'py PyDict {
@@ -401,10 +401,10 @@ fn borrow_from_array_works() {
401401
#[pymethods]
402402
impl Owner {
403403
#[getter]
404-
fn array(this: &PyCell<Self>) -> &PyArray1<f64> {
404+
fn array(this: Bound<'_, Self>) -> Bound<'_, PyArray1<f64>> {
405405
let array = &this.borrow().array;
406406

407-
unsafe { PyArray1::borrow_from_array(array, this) }
407+
unsafe { PyArray1::borrow_from_array_bound(array, this.into_any()) }
408408
}
409409
}
410410

0 commit comments

Comments
 (0)