Skip to content

Commit

Permalink
deprecate PyArray::zeros
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu authored and adamreichold committed Mar 25, 2024
1 parent f979966 commit 9d7882f
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 168 deletions.
34 changes: 23 additions & 11 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,15 @@ impl<T, D> PyArray<T, D> {
/// # Example
///
/// ```
/// use numpy::PyArray1;
/// use numpy::{PyArray1, PyArrayMethods};
/// use pyo3::{Py, Python};
///
/// let array: Py<PyArray1<f64>> = 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")]
Expand Down Expand Up @@ -499,6 +499,18 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
)
}

/// Deprecated form of [`PyArray<T, D>::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<Dim = D>,
{
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,
Expand All @@ -512,19 +524,19 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
/// # Example
///
/// ```
/// use numpy::PyArray2;
/// use numpy::{PyArray2, PyArrayMethods};
/// use pyo3::Python;
///
/// Python::with_gil(|py| {
/// let pyarray: &PyArray2<usize> = PyArray2::zeros(py, [2, 2], true);
/// let pyarray = PyArray2::<usize>::zeros_bound(py, [2, 2], true);
///
/// assert_eq!(pyarray.readonly().as_slice().unwrap(), [0; 4]);
/// });
/// ```
///
/// [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<ID>(py: Python<'_>, dims: ID, is_fortran: bool) -> Bound<'_, Self>
where
ID: IntoDimension<Dim = D>,
{
Expand All @@ -537,7 +549,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
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()
}
}

Expand Down Expand Up @@ -1317,11 +1329,11 @@ impl<T: Element, D> PyArray<T, D> {
/// # Example
///
/// ```
/// use numpy::PyArray;
/// use numpy::{PyArray, PyArrayMethods, PyUntypedArrayMethods};
/// use pyo3::Python;
///
/// Python::with_gil(|py| {
/// let pyarray = PyArray::<f64, _>::zeros(py, (10, 10), false);
/// let pyarray = PyArray::<f64, _>::zeros_bound(py, (10, 10), false);
/// assert_eq!(pyarray.shape(), [10, 10]);
///
/// unsafe {
Expand Down Expand Up @@ -1847,11 +1859,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::<f64, _>::zeros(py, (10, 10), false);
/// let pyarray = PyArray::<f64, _>::zeros_bound(py, (10, 10), false);
/// assert_eq!(pyarray.shape(), [10, 10]);
///
/// unsafe {
Expand Down
44 changes: 22 additions & 22 deletions src/borrow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64>, y: &PyArray1<f64>, z: &PyArray1<f64>) {
//! fn add(x: &Bound<'_, PyArray1<f64>>, y: &Bound<'_, PyArray1<f64>>, z: &Bound<'_, PyArray1<f64>>) {
//! let x1 = x.readonly();
//! let y1 = y.readonly();
//! let mut z1 = z.readwrite();
Expand All @@ -41,19 +41,19 @@
//! }
//!
//! Python::with_gil(|py| {
//! let x = PyArray1::<f64>::zeros(py, 42, false);
//! let y = PyArray1::<f64>::zeros(py, 42, false);
//! let z = PyArray1::<f64>::zeros(py, 42, false);
//! let x = PyArray1::<f64>::zeros_bound(py, 42, false);
//! let y = PyArray1::<f64>::zeros_bound(py, 42, false);
//! let z = PyArray1::<f64>::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());
//! });
Expand Down Expand Up @@ -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::<f64>::zeros(py, (10, 10), false);
//! let locals = [("array", array)].into_py_dict(py);
//! let array = PyArray2::<f64>::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::<PyArray2<f64>>().unwrap();
//! let view2 = py.eval("array[:, 1::3]", None, Some(locals)).unwrap().downcast::<PyArray2<f64>>().unwrap();
//! let view1 = py.eval_bound("array[:, ::3]", None, Some(&locals)).unwrap().downcast_into::<PyArray2<f64>>().unwrap();
//! let view2 = py.eval_bound("array[:, 1::3]", None, Some(&locals)).unwrap().downcast_into::<PyArray2<f64>>().unwrap();
//!
//! // A false conflict as the views do not actually share any elements.
//! let res = catch_unwind(AssertUnwindSafe(|| {
Expand Down Expand Up @@ -628,7 +628,7 @@ mod tests {
#[test]
fn test_debug_formatting() {
Python::with_gil(|py| {
let array = PyArray::<f64, _>::zeros(py, (1, 2, 3), false);
let array = PyArray::<f64, _>::zeros_bound(py, (1, 2, 3), false);

{
let shared = array.readonly();
Expand All @@ -654,7 +654,7 @@ mod tests {
#[should_panic(expected = "AlreadyBorrowed")]
fn cannot_clone_exclusive_borrow_via_deref() {
Python::with_gil(|py| {
let array = PyArray::<f64, _>::zeros(py, (3, 2, 1), false);
let array = PyArray::<f64, _>::zeros_bound(py, (3, 2, 1), false);

let exclusive = array.readwrite();
let _shared = exclusive.clone();
Expand All @@ -664,14 +664,14 @@ mod tests {
#[test]
fn failed_resize_does_not_double_release() {
Python::with_gil(|py| {
let array = PyArray::<f64, _>::zeros(py, 10, false);
let array = PyArray::<f64, _>::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::<PyArray1<f64>>()
.downcast_into::<PyArray1<f64>>()
.unwrap();

let exclusive = array.readwrite();
Expand All @@ -682,7 +682,7 @@ mod tests {
#[test]
fn ineffective_resize_does_not_conflict() {
Python::with_gil(|py| {
let array = PyArray::<f64, _>::zeros(py, 10, false);
let array = PyArray::<f64, _>::zeros_bound(py, 10, false);

let exclusive = array.readwrite();
assert!(exclusive.resize(10).is_ok());
Expand Down
Loading

0 comments on commit 9d7882f

Please sign in to comment.