Skip to content

Commit 3adc8fa

Browse files
committed
deprecate PyArray::zeros
1 parent 596ed14 commit 3adc8fa

File tree

7 files changed

+188
-168
lines changed

7 files changed

+188
-168
lines changed

src/array.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,15 @@ impl<T, D> PyArray<T, D> {
214214
/// # Example
215215
///
216216
/// ```
217-
/// use numpy::PyArray1;
217+
/// use numpy::{PyArray1, PyArrayMethods};
218218
/// use pyo3::{Py, Python};
219219
///
220220
/// let array: Py<PyArray1<f64>> = Python::with_gil(|py| {
221-
/// PyArray1::zeros(py, 5, false).to_owned()
221+
/// PyArray1::zeros_bound(py, 5, false).unbind()
222222
/// });
223223
///
224224
/// Python::with_gil(|py| {
225-
/// assert_eq!(array.as_ref(py).readonly().as_slice().unwrap(), [0.0; 5]);
225+
/// assert_eq!(array.bind(py).readonly().as_slice().unwrap(), [0.0; 5]);
226226
/// });
227227
/// ```
228228
#[deprecated(since = "0.21.0", note = "use Bound::unbind() instead")]
@@ -499,6 +499,18 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
499499
)
500500
}
501501

502+
/// Deprecated form of [`PyArray<T, D>::zeros_bound`]
503+
#[deprecated(
504+
since = "0.21.0",
505+
note = "will be replaced by `PyArray::zeros_bound` in the future"
506+
)]
507+
pub fn zeros<'py, ID>(py: Python<'py>, dims: ID, is_fortran: bool) -> &Self
508+
where
509+
ID: IntoDimension<Dim = D>,
510+
{
511+
Self::zeros_bound(py, dims, is_fortran).into_gil_ref()
512+
}
513+
502514
/// Construct a new NumPy array filled with zeros.
503515
///
504516
/// If `is_fortran` is true, then it has Fortran/column-major order,
@@ -512,19 +524,19 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
512524
/// # Example
513525
///
514526
/// ```
515-
/// use numpy::PyArray2;
527+
/// use numpy::{PyArray2, PyArrayMethods};
516528
/// use pyo3::Python;
517529
///
518530
/// Python::with_gil(|py| {
519-
/// let pyarray: &PyArray2<usize> = PyArray2::zeros(py, [2, 2], true);
531+
/// let pyarray = PyArray2::<usize>::zeros_bound(py, [2, 2], true);
520532
///
521533
/// assert_eq!(pyarray.readonly().as_slice().unwrap(), [0; 4]);
522534
/// });
523535
/// ```
524536
///
525537
/// [numpy-zeros]: https://numpy.org/doc/stable/reference/generated/numpy.zeros.html
526538
/// [PyArray_Zeros]: https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_Zeros
527-
pub fn zeros<'py, ID>(py: Python<'py>, dims: ID, is_fortran: bool) -> &Self
539+
pub fn zeros_bound<ID>(py: Python<'_>, dims: ID, is_fortran: bool) -> Bound<'_, Self>
528540
where
529541
ID: IntoDimension<Dim = D>,
530542
{
@@ -537,7 +549,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
537549
T::get_dtype_bound(py).into_dtype_ptr(),
538550
if is_fortran { -1 } else { 0 },
539551
);
540-
Self::from_owned_ptr(py, ptr)
552+
Bound::from_owned_ptr(py, ptr).downcast_into_unchecked()
541553
}
542554
}
543555

@@ -1313,11 +1325,11 @@ impl<T: Element, D> PyArray<T, D> {
13131325
/// # Example
13141326
///
13151327
/// ```
1316-
/// use numpy::PyArray;
1328+
/// use numpy::{PyArray, PyArrayMethods, PyUntypedArrayMethods};
13171329
/// use pyo3::Python;
13181330
///
13191331
/// Python::with_gil(|py| {
1320-
/// let pyarray = PyArray::<f64, _>::zeros(py, (10, 10), false);
1332+
/// let pyarray = PyArray::<f64, _>::zeros_bound(py, (10, 10), false);
13211333
/// assert_eq!(pyarray.shape(), [10, 10]);
13221334
///
13231335
/// unsafe {
@@ -1843,11 +1855,11 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> {
18431855
/// # Example
18441856
///
18451857
/// ```
1846-
/// use numpy::PyArray;
1858+
/// use numpy::{PyArray, PyArrayMethods, PyUntypedArrayMethods};
18471859
/// use pyo3::Python;
18481860
///
18491861
/// Python::with_gil(|py| {
1850-
/// let pyarray = PyArray::<f64, _>::zeros(py, (10, 10), false);
1862+
/// let pyarray = PyArray::<f64, _>::zeros_bound(py, (10, 10), false);
18511863
/// assert_eq!(pyarray.shape(), [10, 10]);
18521864
///
18531865
/// unsafe {

src/borrow/mod.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
//! ```rust
1616
//! # use std::panic::{catch_unwind, AssertUnwindSafe};
1717
//! #
18-
//! use numpy::PyArray1;
18+
//! use numpy::{PyArray1, PyArrayMethods};
1919
//! use ndarray::Zip;
20-
//! use pyo3::Python;
20+
//! use pyo3::{Python, Bound};
2121
//!
22-
//! fn add(x: &PyArray1<f64>, y: &PyArray1<f64>, z: &PyArray1<f64>) {
22+
//! fn add(x: &Bound<'_, PyArray1<f64>>, y: &Bound<'_, PyArray1<f64>>, z: &Bound<'_, PyArray1<f64>>) {
2323
//! let x1 = x.readonly();
2424
//! let y1 = y.readonly();
2525
//! let mut z1 = z.readwrite();
@@ -41,19 +41,19 @@
4141
//! }
4242
//!
4343
//! Python::with_gil(|py| {
44-
//! let x = PyArray1::<f64>::zeros(py, 42, false);
45-
//! let y = PyArray1::<f64>::zeros(py, 42, false);
46-
//! let z = PyArray1::<f64>::zeros(py, 42, false);
44+
//! let x = PyArray1::<f64>::zeros_bound(py, 42, false);
45+
//! let y = PyArray1::<f64>::zeros_bound(py, 42, false);
46+
//! let z = PyArray1::<f64>::zeros_bound(py, 42, false);
4747
//!
4848
//! // Will work as the three arrays are distinct.
49-
//! add(x, y, z);
49+
//! add(&x, &y, &z);
5050
//!
5151
//! // Will work as `x1` and `y1` are compatible borrows.
52-
//! add(x, x, z);
52+
//! add(&x, &x, &z);
5353
//!
5454
//! // Will fail at runtime due to conflict between `y1` and `z1`.
5555
//! let res = catch_unwind(AssertUnwindSafe(|| {
56-
//! add(x, y, y);
56+
//! add(&x, &y, &y);
5757
//! }));
5858
//! assert!(res.is_err());
5959
//! });
@@ -91,15 +91,15 @@
9191
//! ```rust
9292
//! # use std::panic::{catch_unwind, AssertUnwindSafe};
9393
//! #
94-
//! use numpy::PyArray2;
95-
//! use pyo3::{types::IntoPyDict, Python};
94+
//! use numpy::{PyArray2, PyArrayMethods};
95+
//! use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python};
9696
//!
9797
//! Python::with_gil(|py| {
98-
//! let array = PyArray2::<f64>::zeros(py, (10, 10), false);
99-
//! let locals = [("array", array)].into_py_dict(py);
98+
//! let array = PyArray2::<f64>::zeros_bound(py, (10, 10), false);
99+
//! let locals = [("array", array)].into_py_dict_bound(py);
100100
//!
101-
//! let view1 = py.eval("array[:, ::3]", None, Some(locals)).unwrap().downcast::<PyArray2<f64>>().unwrap();
102-
//! let view2 = py.eval("array[:, 1::3]", None, Some(locals)).unwrap().downcast::<PyArray2<f64>>().unwrap();
101+
//! let view1 = py.eval_bound("array[:, ::3]", None, Some(&locals)).unwrap().downcast_into::<PyArray2<f64>>().unwrap();
102+
//! let view2 = py.eval_bound("array[:, 1::3]", None, Some(&locals)).unwrap().downcast_into::<PyArray2<f64>>().unwrap();
103103
//!
104104
//! // A false conflict as the views do not actually share any elements.
105105
//! let res = catch_unwind(AssertUnwindSafe(|| {
@@ -589,7 +589,7 @@ mod tests {
589589
#[test]
590590
fn test_debug_formatting() {
591591
Python::with_gil(|py| {
592-
let array = PyArray::<f64, _>::zeros(py, (1, 2, 3), false);
592+
let array = PyArray::<f64, _>::zeros_bound(py, (1, 2, 3), false);
593593

594594
{
595595
let shared = array.readonly();
@@ -615,7 +615,7 @@ mod tests {
615615
#[should_panic(expected = "AlreadyBorrowed")]
616616
fn cannot_clone_exclusive_borrow_via_deref() {
617617
Python::with_gil(|py| {
618-
let array = PyArray::<f64, _>::zeros(py, (3, 2, 1), false);
618+
let array = PyArray::<f64, _>::zeros_bound(py, (3, 2, 1), false);
619619

620620
let exclusive = array.readwrite();
621621
let _shared = exclusive.clone();
@@ -625,14 +625,14 @@ mod tests {
625625
#[test]
626626
fn failed_resize_does_not_double_release() {
627627
Python::with_gil(|py| {
628-
let array = PyArray::<f64, _>::zeros(py, 10, false);
628+
let array = PyArray::<f64, _>::zeros_bound(py, 10, false);
629629

630630
// The view will make the internal reference check of `PyArray_Resize` fail.
631-
let locals = [("array", array)].into_py_dict(py);
631+
let locals = [("array", &array)].into_py_dict_bound(py);
632632
let _view = py
633-
.eval("array[:]", None, Some(locals))
633+
.eval_bound("array[:]", None, Some(&locals))
634634
.unwrap()
635-
.downcast::<PyArray1<f64>>()
635+
.downcast_into::<PyArray1<f64>>()
636636
.unwrap();
637637

638638
let exclusive = array.readwrite();
@@ -643,7 +643,7 @@ mod tests {
643643
#[test]
644644
fn ineffective_resize_does_not_conflict() {
645645
Python::with_gil(|py| {
646-
let array = PyArray::<f64, _>::zeros(py, 10, false);
646+
let array = PyArray::<f64, _>::zeros_bound(py, 10, false);
647647

648648
let exclusive = array.readwrite();
649649
assert!(exclusive.resize(10).is_ok());

0 commit comments

Comments
 (0)