15
15
//! ```rust
16
16
//! # use std::panic::{catch_unwind, AssertUnwindSafe};
17
17
//! #
18
- //! use numpy::PyArray1;
18
+ //! use numpy::{ PyArray1, PyArrayMethods} ;
19
19
//! use ndarray::Zip;
20
- //! use pyo3::Python;
20
+ //! use pyo3::{ Python, Bound} ;
21
21
//!
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> >) {
23
23
//! let x1 = x.readonly();
24
24
//! let y1 = y.readonly();
25
25
//! let mut z1 = z.readwrite();
41
41
//! }
42
42
//!
43
43
//! 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);
47
47
//!
48
48
//! // Will work as the three arrays are distinct.
49
- //! add(x, y, z);
49
+ //! add(& x, & y, & z);
50
50
//!
51
51
//! // Will work as `x1` and `y1` are compatible borrows.
52
- //! add(x, x, z);
52
+ //! add(& x, & x, & z);
53
53
//!
54
54
//! // Will fail at runtime due to conflict between `y1` and `z1`.
55
55
//! let res = catch_unwind(AssertUnwindSafe(|| {
56
- //! add(x, y, y);
56
+ //! add(& x, & y, & y);
57
57
//! }));
58
58
//! assert!(res.is_err());
59
59
//! });
91
91
//! ```rust
92
92
//! # use std::panic::{catch_unwind, AssertUnwindSafe};
93
93
//! #
94
- //! use numpy::PyArray2;
95
- //! use pyo3::{types::IntoPyDict, Python};
94
+ //! use numpy::{ PyArray2, PyArrayMethods} ;
95
+ //! use pyo3::{types::{ IntoPyDict, PyAnyMethods} , Python};
96
96
//!
97
97
//! 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);
100
100
//!
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();
103
103
//!
104
104
//! // A false conflict as the views do not actually share any elements.
105
105
//! let res = catch_unwind(AssertUnwindSafe(|| {
@@ -589,7 +589,7 @@ mod tests {
589
589
#[ test]
590
590
fn test_debug_formatting ( ) {
591
591
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 ) ;
593
593
594
594
{
595
595
let shared = array. readonly ( ) ;
@@ -615,7 +615,7 @@ mod tests {
615
615
#[ should_panic( expected = "AlreadyBorrowed" ) ]
616
616
fn cannot_clone_exclusive_borrow_via_deref ( ) {
617
617
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 ) ;
619
619
620
620
let exclusive = array. readwrite ( ) ;
621
621
let _shared = exclusive. clone ( ) ;
@@ -625,14 +625,14 @@ mod tests {
625
625
#[ test]
626
626
fn failed_resize_does_not_double_release ( ) {
627
627
Python :: with_gil ( |py| {
628
- let array = PyArray :: < f64 , _ > :: zeros ( py, 10 , false ) ;
628
+ let array = PyArray :: < f64 , _ > :: zeros_bound ( py, 10 , false ) ;
629
629
630
630
// 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) ;
632
632
let _view = py
633
- . eval ( "array[:]" , None , Some ( locals) )
633
+ . eval_bound ( "array[:]" , None , Some ( & locals) )
634
634
. unwrap ( )
635
- . downcast :: < PyArray1 < f64 > > ( )
635
+ . downcast_into :: < PyArray1 < f64 > > ( )
636
636
. unwrap ( ) ;
637
637
638
638
let exclusive = array. readwrite ( ) ;
@@ -643,7 +643,7 @@ mod tests {
643
643
#[ test]
644
644
fn ineffective_resize_does_not_conflict ( ) {
645
645
Python :: with_gil ( |py| {
646
- let array = PyArray :: < f64 , _ > :: zeros ( py, 10 , false ) ;
646
+ let array = PyArray :: < f64 , _ > :: zeros_bound ( py, 10 , false ) ;
647
647
648
648
let exclusive = array. readwrite ( ) ;
649
649
assert ! ( exclusive. resize( 10 ) . is_ok( ) ) ;
0 commit comments