diff --git a/README.md b/README.md index 7bac0c8e..32e72eeb 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ numpy = "0.22" ```rust use numpy::{PyArray1, PyArrayMethods}; -use pyo3::{types::{IntoPyDict, PyAnyMethods}, PyResult, Python}; +use pyo3::{types::{IntoPyDict, PyAnyMethods}, PyResult, Python, ffi::c_str}; fn main() -> PyResult<()> { Python::with_gil(|py| { @@ -107,7 +107,7 @@ fn main() -> PyResult<()> { let locals = [("np", np)].into_py_dict(py)?; let pyarray = py - .eval("np.absolute(np.array([-1, -2, -3], dtype='int32'))", Some(&locals), None)? + .eval(c_str!("np.absolute(np.array([-1, -2, -3], dtype='int32'))"), Some(&locals), None)? .downcast_into::>()?; let readonly = pyarray.readonly(); diff --git a/src/array.rs b/src/array.rs index 0add26c1..81ad3104 100644 --- a/src/array.rs +++ b/src/array.rs @@ -1055,17 +1055,18 @@ pub trait PyArrayMethods<'py, T, D>: PyUntypedArrayMethods<'py> { /// /// ``` /// use numpy::{PyArray2, PyArrayMethods}; - /// use pyo3::{Python, types::PyAnyMethods}; + /// use pyo3::{Python, types::PyAnyMethods, ffi::c_str}; /// + /// # fn main() -> pyo3::PyResult<()> { /// Python::with_gil(|py| { /// let pyarray= py - /// .eval("__import__('numpy').array([[0, 1], [2, 3]], dtype='int64')", None, None) - /// .unwrap() - /// .downcast_into::>() - /// .unwrap(); + /// .eval(c_str!("__import__('numpy').array([[0, 1], [2, 3]], dtype='int64')"), None, None)? + /// .downcast_into::>()?; /// - /// assert_eq!(pyarray.to_vec().unwrap(), vec![0, 1, 2, 3]); - /// }); + /// assert_eq!(pyarray.to_vec()?, vec![0, 1, 2, 3]); + /// # Ok(()) + /// }) + /// # } /// ``` fn to_vec(&self) -> Result, NotContiguousError> where diff --git a/src/borrow/mod.rs b/src/borrow/mod.rs index 58b88eb1..34ec086a 100644 --- a/src/borrow/mod.rs +++ b/src/borrow/mod.rs @@ -63,16 +63,17 @@ //! //! ```rust //! use numpy::{PyArray1, PyArrayMethods}; -//! use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python}; +//! use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python, ffi::c_str}; //! +//! # fn main() -> pyo3::PyResult<()> { //! Python::with_gil(|py| { //! let array = PyArray1::arange(py, 0.0, 10.0, 1.0); -//! let locals = [("array", array)].into_py_dict(py).unwrap(); +//! let locals = [("array", array)].into_py_dict(py)?; //! -//! let view1 = py.eval("array[:5]", None, Some(&locals)).unwrap().downcast_into::>().unwrap(); -//! let view2 = py.eval("array[5:]", None, Some(&locals)).unwrap().downcast_into::>().unwrap(); -//! let view3 = py.eval("array[::2]", None, Some(&locals)).unwrap().downcast_into::>().unwrap(); -//! let view4 = py.eval("array[1::2]", None, Some(&locals)).unwrap().downcast_into::>().unwrap(); +//! let view1 = py.eval(c_str!("array[:5]"), None, Some(&locals))?.downcast_into::>()?; +//! let view2 = py.eval(c_str!("array[5:]"), None, Some(&locals))?.downcast_into::>()?; +//! let view3 = py.eval(c_str!("array[::2]"), None, Some(&locals))?.downcast_into::>()?; +//! let view4 = py.eval(c_str!("array[1::2]"), None, Some(&locals))?.downcast_into::>()?; //! //! { //! let _view1 = view1.readwrite(); @@ -83,7 +84,9 @@ //! let _view3 = view3.readwrite(); //! let _view4 = view4.readwrite(); //! } -//! }); +//! # Ok(()) +//! }) +//! # } //! ``` //! //! The third example shows that some views are incorrectly rejected since the borrows are over-approximated. @@ -92,14 +95,15 @@ //! # use std::panic::{catch_unwind, AssertUnwindSafe}; //! # //! use numpy::{PyArray2, PyArrayMethods}; -//! use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python}; +//! use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python, ffi::c_str}; //! +//! # fn main() -> pyo3::PyResult<()> { //! Python::with_gil(|py| { //! let array = PyArray2::::zeros(py, (10, 10), false); -//! let locals = [("array", array)].into_py_dict(py).unwrap(); +//! let locals = [("array", array)].into_py_dict(py)?; //! -//! let view1 = py.eval("array[:, ::3]", None, Some(&locals)).unwrap().downcast_into::>().unwrap(); -//! let view2 = py.eval("array[:, 1::3]", None, Some(&locals)).unwrap().downcast_into::>().unwrap(); +//! let view1 = py.eval(c_str!("array[:, ::3]"), None, Some(&locals))?.downcast_into::>()?; +//! let view2 = py.eval(c_str!("array[:, 1::3]"), None, Some(&locals))?.downcast_into::>()?; //! //! // A false conflict as the views do not actually share any elements. //! let res = catch_unwind(AssertUnwindSafe(|| { @@ -107,7 +111,9 @@ //! let _view2 = view2.readwrite(); //! })); //! assert!(res.is_err()); -//! }); +//! # Ok(()) +//! }) +//! # } //! ``` //! //! # Rationale diff --git a/src/datetime.rs b/src/datetime.rs index acd4f149..3eef346d 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -11,25 +11,22 @@ //! //! ``` //! use numpy::{datetime::{units, Datetime, Timedelta}, PyArray1, PyArrayMethods}; -//! use pyo3::{Python, types::PyAnyMethods}; +//! use pyo3::{Python, types::PyAnyMethods, ffi::c_str}; //! # use pyo3::types::PyDict; //! +//! # fn main() -> pyo3::PyResult<()> { //! Python::with_gil(|py| { //! # let locals = py -//! # .eval("{ 'np': __import__('numpy') }", None, None) -//! # .unwrap() -//! # .downcast_into::() -//! # .unwrap(); +//! # .eval(c_str!("{ 'np': __import__('numpy') }"), None, None)? +//! # .downcast_into::()?; //! # //! let array = py //! .eval( -//! "np.array([np.datetime64('2017-04-21')])", +//! c_str!("np.array([np.datetime64('2017-04-21')])"), //! None, //! Some(&locals), -//! ) -//! .unwrap() -//! .downcast_into::>>() -//! .unwrap(); +//! )? +//! .downcast_into::>>()?; //! //! assert_eq!( //! array.get_owned(0).unwrap(), @@ -38,19 +35,19 @@ //! //! let array = py //! .eval( -//! "np.array([np.datetime64('2022-03-29')]) - np.array([np.datetime64('2017-04-21')])", +//! c_str!("np.array([np.datetime64('2022-03-29')]) - np.array([np.datetime64('2017-04-21')])"), //! None, //! Some(&locals), -//! ) -//! .unwrap() -//! .downcast_into::>>() -//! .unwrap(); +//! )? +//! .downcast_into::>>()?; //! //! assert_eq!( //! array.get_owned(0).unwrap(), //! Timedelta::::from(1_803) //! ); -//! }); +//! # Ok(()) +//! }) +//! # } //! ``` //! //! [datetime]: https://numpy.org/doc/stable/reference/arrays.datetime.html diff --git a/src/dtype.rs b/src/dtype.rs index ac96a277..fd45bb5e 100644 --- a/src/dtype.rs +++ b/src/dtype.rs @@ -30,19 +30,20 @@ pub use num_complex::{Complex32, Complex64}; /// /// ``` /// use numpy::{dtype, get_array_module, PyArrayDescr, PyArrayDescrMethods}; -/// use numpy::pyo3::{types::{IntoPyDict, PyAnyMethods}, Python}; +/// use numpy::pyo3::{types::{IntoPyDict, PyAnyMethods}, Python, ffi::c_str}; /// +/// # fn main() -> pyo3::PyResult<()> { /// Python::with_gil(|py| { -/// let locals = [("np", get_array_module(py).unwrap())].into_py_dict(py).unwrap(); +/// let locals = [("np", get_array_module(py)?)].into_py_dict(py)?; /// /// let dt = py -/// .eval("np.array([1, 2, 3.0]).dtype", Some(&locals), None) -/// .unwrap() -/// .downcast_into::() -/// .unwrap(); +/// .eval(c_str!("np.array([1, 2, 3.0]).dtype"), Some(&locals), None)? +/// .downcast_into::()?; /// /// assert!(dt.is_equiv_to(&dtype::(py))); -/// }); +/// # Ok(()) +/// }) +/// # } /// ``` /// /// [dtype]: https://numpy.org/doc/stable/reference/generated/numpy.dtype.html diff --git a/src/untyped_array.rs b/src/untyped_array.rs index a1d4fe51..aca2dea5 100644 --- a/src/untyped_array.rs +++ b/src/untyped_array.rs @@ -111,19 +111,20 @@ pub trait PyUntypedArrayMethods<'py>: Sealed { /// /// ``` /// use numpy::{PyArray1, PyUntypedArrayMethods}; - /// use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python}; + /// use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python, ffi::c_str}; /// + /// # fn main() -> pyo3::PyResult<()> { /// Python::with_gil(|py| { /// let array = PyArray1::arange(py, 0, 10, 1); /// assert!(array.is_contiguous()); /// /// let view = py - /// .eval("array[::2]", None, Some(&[("array", array)].into_py_dict(py).unwrap())) - /// .unwrap() - /// .downcast_into::>() - /// .unwrap(); + /// .eval(c_str!("array[::2]"), None, Some(&[("array", array)].into_py_dict(py)?))? + /// .downcast_into::>()?; /// assert!(!view.is_contiguous()); - /// }); + /// # Ok(()) + /// }) + /// # } /// ``` fn is_contiguous(&self) -> bool { unsafe {