6363//!
6464//! ```rust
6565//! use numpy::{PyArray1, PyArrayMethods};
66- //! use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python};
66+ //! use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python, ffi::c_str };
6767//!
68+ //! # fn main() -> pyo3::PyResult<()> {
6869//! Python::with_gil(|py| {
6970//! let array = PyArray1::arange(py, 0.0, 10.0, 1.0);
70- //! let locals = [("array", array)].into_py_dict(py).unwrap() ;
71+ //! let locals = [("array", array)].into_py_dict(py)? ;
7172//!
72- //! let view1 = py.eval("array[:5]", None, Some(&locals)).unwrap(). downcast_into::<PyArray1<f64>>().unwrap() ;
73- //! let view2 = py.eval("array[5:]", None, Some(&locals)).unwrap(). downcast_into::<PyArray1<f64>>().unwrap() ;
74- //! let view3 = py.eval("array[::2]", None, Some(&locals)).unwrap(). downcast_into::<PyArray1<f64>>().unwrap() ;
75- //! let view4 = py.eval("array[1::2]", None, Some(&locals)).unwrap(). downcast_into::<PyArray1<f64>>().unwrap() ;
73+ //! let view1 = py.eval(c_str!( "array[:5]") , None, Some(&locals))?. downcast_into::<PyArray1<f64>>()? ;
74+ //! let view2 = py.eval(c_str!( "array[5:]") , None, Some(&locals))?. downcast_into::<PyArray1<f64>>()? ;
75+ //! let view3 = py.eval(c_str!( "array[::2]") , None, Some(&locals))?. downcast_into::<PyArray1<f64>>()? ;
76+ //! let view4 = py.eval(c_str!( "array[1::2]") , None, Some(&locals))?. downcast_into::<PyArray1<f64>>()? ;
7677//!
7778//! {
7879//! let _view1 = view1.readwrite();
8384//! let _view3 = view3.readwrite();
8485//! let _view4 = view4.readwrite();
8586//! }
86- //! });
87+ //! # Ok(())
88+ //! })
89+ //! # }
8790//! ```
8891//!
8992//! The third example shows that some views are incorrectly rejected since the borrows are over-approximated.
9295//! # use std::panic::{catch_unwind, AssertUnwindSafe};
9396//! #
9497//! use numpy::{PyArray2, PyArrayMethods};
95- //! use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python};
98+ //! use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python, ffi::c_str };
9699//!
100+ //! # fn main() -> pyo3::PyResult<()> {
97101//! Python::with_gil(|py| {
98102//! let array = PyArray2::<f64>::zeros(py, (10, 10), false);
99- //! let locals = [("array", array)].into_py_dict(py).unwrap() ;
103+ //! let locals = [("array", array)].into_py_dict(py)? ;
100104//!
101- //! let view1 = py.eval("array[:, ::3]", None, Some(&locals)).unwrap(). downcast_into::<PyArray2<f64>>().unwrap() ;
102- //! let view2 = py.eval("array[:, 1::3]", None, Some(&locals)).unwrap(). downcast_into::<PyArray2<f64>>().unwrap() ;
105+ //! let view1 = py.eval(c_str!( "array[:, ::3]") , None, Some(&locals))?. downcast_into::<PyArray2<f64>>()? ;
106+ //! let view2 = py.eval(c_str!( "array[:, 1::3]") , None, Some(&locals))?. downcast_into::<PyArray2<f64>>()? ;
103107//!
104108//! // A false conflict as the views do not actually share any elements.
105109//! let res = catch_unwind(AssertUnwindSafe(|| {
106110//! let _view1 = view1.readwrite();
107111//! let _view2 = view2.readwrite();
108112//! }));
109113//! assert!(res.is_err());
110- //! });
114+ //! # Ok(())
115+ //! })
116+ //! # }
111117//! ```
112118//!
113119//! # Rationale
@@ -289,7 +295,7 @@ where
289295 ///
290296 /// ```rust
291297 /// # use pyo3::prelude::*;
292- /// use pyo3::py_run;
298+ /// use pyo3::{ py_run, ffi::c_str} ;
293299 /// use numpy::{get_array_module, PyReadonlyArray2};
294300 /// use nalgebra::{MatrixView, Const, Dyn};
295301 ///
@@ -305,17 +311,20 @@ where
305311 /// matrix.map(|matrix| matrix.sum())
306312 /// }
307313 ///
314+ /// # fn main() -> pyo3::PyResult<()> {
308315 /// Python::with_gil(|py| {
309- /// let np = py.eval("__import__('numpy')", None, None).unwrap() ;
310- /// let sum_standard_layout = wrap_pyfunction!(sum_standard_layout)(py).unwrap() ;
311- /// let sum_dynamic_strides = wrap_pyfunction!(sum_dynamic_strides)(py).unwrap() ;
316+ /// let np = py.eval(c_str!( "__import__('numpy')") , None, None)? ;
317+ /// let sum_standard_layout = wrap_pyfunction!(sum_standard_layout)(py)? ;
318+ /// let sum_dynamic_strides = wrap_pyfunction!(sum_dynamic_strides)(py)? ;
312319 ///
313320 /// py_run!(py, np sum_standard_layout, r"assert sum_standard_layout(np.ones((2, 2), order='F')) == 4.");
314321 /// py_run!(py, np sum_standard_layout, r"assert sum_standard_layout(np.ones((2, 2, 2))[:,:,0]) is None");
315322 ///
316323 /// py_run!(py, np sum_dynamic_strides, r"assert sum_dynamic_strides(np.ones((2, 2), order='F')) == 4.");
317324 /// py_run!(py, np sum_dynamic_strides, r"assert sum_dynamic_strides(np.ones((2, 2, 2))[:,:,0]) == 4.");
318- /// });
325+ /// # Ok(())
326+ /// })
327+ /// # }
319328 /// ```
320329 #[ doc( alias = "nalgebra" ) ]
321330 pub fn try_as_matrix < R , C , RStride , CStride > (
0 commit comments