Skip to content

Commit a6ab468

Browse files
committed
Fix doc tests and examples
1 parent 2ed632a commit a6ab468

File tree

6 files changed

+46
-37
lines changed

6 files changed

+46
-37
lines changed

README.md

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,37 @@ numpy = "0.3"
4545
``` rust
4646
extern crate numpy;
4747
extern crate pyo3;
48-
use numpy::{IntoPyResult, PyArray, get_array_module};
49-
use pyo3::prelude::{ObjectProtocol, PyDict, PyResult, Python};
50-
51-
fn main() -> Result<(), ()> {
52-
let gil = Python::acquire_gil();
53-
main_(gil.python()).map_err(|e| {
54-
eprintln!("error! :{:?}", e);
55-
// we can't display python error type via ::std::fmt::Display
56-
// so print error here manually
57-
e.print_and_set_sys_last_vars(gil.python());
58-
})
59-
}
48+
use numpy::{IntoPyResult, PyArray, ToPyArray};
49+
use pyo3::prelude::{pymodinit, PyModule, PyResult, Python};
50+
51+
#[pymodinit]
52+
fn rust_ext(_py: Python, m: &PyModule) -> PyResult<()> {
53+
// immutable example
54+
fn axpy(a: f64, x: ArrayViewD<f64>, y: ArrayViewD<f64>) -> ArrayD<f64> {
55+
a * &x + &y
56+
}
57+
58+
// mutable example (no return)
59+
fn mult(a: f64, mut x: ArrayViewMutD<f64>) {
60+
x *= a;
61+
}
62+
63+
// wrapper of `axpy`
64+
#[pyfn(m, "axpy")]
65+
fn axpy_py(py: Python, a: f64, x: &PyArray<f64>, y: &PyArray<f64>) -> PyResult<PyArray<f64>> {
66+
let x = x.as_array().into_pyresult("x must be f64 array")?;
67+
let y = y.as_array().into_pyresult("y must be f64 array")?;
68+
Ok(axpy(a, x, y).to_pyarray(py).to_owned(py))
69+
}
70+
71+
// wrapper of `mult`
72+
#[pyfn(m, "mult")]
73+
fn mult_py(_py: Python, a: f64, x: &PyArray<f64>) -> PyResult<()> {
74+
let x = x.as_array_mut().into_pyresult("x must be f64 array")?;
75+
mult(a, x);
76+
Ok(())
77+
}
6078

61-
fn main_<'py>(py: Python<'py>) -> PyResult<()> {
62-
let np = get_array_module(py)?;
63-
let dict = PyDict::new(py);
64-
dict.set_item("np", np)?;
65-
let pyarray: &PyArray<i32> = py
66-
.eval("np.array([1, 2, 3], dtype='int32')", Some(&dict), None)?
67-
.extract()?;
68-
let slice = pyarray.as_slice().into_pyresult("Array Cast failed")?;
69-
assert_eq!(slice, &[1, 2, 3]);
7079
Ok(())
7180
}
7281
```

example/extensions/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ crate-type = ["cdylib"]
99

1010
[dependencies]
1111
numpy = { path = "../.." }
12-
ndarray = ">= 0.11"
12+
ndarray = "0.12"
1313

1414
[dependencies.pyo3]
1515
version = "0.4.1"

example/extensions/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extern crate numpy;
33
extern crate pyo3;
44

55
use ndarray::{ArrayD, ArrayViewD, ArrayViewMutD};
6-
use numpy::{IntoPyArray, IntoPyResult, PyArray};
6+
use numpy::{IntoPyResult, PyArray, ToPyArray};
77
use pyo3::prelude::{pymodinit, PyModule, PyResult, Python};
88

99
#[pymodinit]
@@ -23,7 +23,7 @@ fn rust_ext(_py: Python, m: &PyModule) -> PyResult<()> {
2323
fn axpy_py(py: Python, a: f64, x: &PyArray<f64>, y: &PyArray<f64>) -> PyResult<PyArray<f64>> {
2424
let x = x.as_array().into_pyresult("x must be f64 array")?;
2525
let y = y.as_array().into_pyresult("y must be f64 array")?;
26-
Ok(axpy(a, x, y).into_pyarray(py).to_owned(py))
26+
Ok(axpy(a, x, y).to_pyarray(py).to_owned(py))
2727
}
2828

2929
// wrapper of `mult`

src/array.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl<T: TypeNum> PyArray<T> {
401401
/// # extern crate pyo3; extern crate numpy; #[macro_use] extern crate ndarray; fn main() {
402402
/// use numpy::PyArray;
403403
/// let gil = pyo3::Python::acquire_gil();
404-
/// let pyarray = PyArray::from_ndarray(gil.python(), array![[1, 2], [3, 4]]);
404+
/// let pyarray = PyArray::from_ndarray(gil.python(), &array![[1, 2], [3, 4]]);
405405
/// assert_eq!(pyarray.as_array().unwrap(), array![[1, 2], [3, 4]].into_dyn());
406406
/// # }
407407
/// ```
@@ -476,7 +476,7 @@ impl<T: TypeNum> PyArray<T> {
476476
/// # extern crate pyo3; extern crate numpy; #[macro_use] extern crate ndarray; fn main() {
477477
/// use numpy::PyArray;
478478
/// let gil = pyo3::Python::acquire_gil();
479-
/// let pyarray = PyArray::<i32>::new(gil.python(), [4, 5, 6]);
479+
/// let pyarray = PyArray::<i32>::new(gil.python(), [4, 5, 6], false);
480480
/// assert_eq!(pyarray.shape(), &[4, 5, 6]);
481481
/// # }
482482
/// ```
@@ -540,7 +540,7 @@ impl<T: TypeNum> PyArray<T> {
540540
/// # Example
541541
/// ```
542542
/// # extern crate pyo3; extern crate numpy; fn main() {
543-
/// use numpy::{PyArray, IntoPyArray};
543+
/// use numpy::PyArray;
544544
/// let gil = pyo3::Python::acquire_gil();
545545
/// let pyarray = PyArray::<f64>::arange(gil.python(), 2.0, 4.0, 0.5);
546546
/// assert_eq!(pyarray.as_slice().unwrap(), &[2.0, 2.5, 3.0, 3.5]);
@@ -558,10 +558,10 @@ impl<T: TypeNum> PyArray<T> {
558558
/// # Example
559559
/// ```
560560
/// # extern crate pyo3; extern crate numpy; fn main() {
561-
/// use numpy::{PyArray, IntoPyArray};
561+
/// use numpy::PyArray;
562562
/// let gil = pyo3::Python::acquire_gil();
563563
/// let pyarray_f = PyArray::<f64>::arange(gil.python(), 2.0, 5.0, 1.0);
564-
/// let pyarray_i = PyArray::<i64>::new(gil.python(), [3]);
564+
/// let pyarray_i = PyArray::<i64>::new(gil.python(), [3], false);
565565
/// assert!(pyarray_f.copy_to(pyarray_i).is_ok());
566566
/// assert_eq!(pyarray_i.as_slice().unwrap(), &[2, 3, 4]);
567567
/// # }
@@ -580,10 +580,10 @@ impl<T: TypeNum> PyArray<T> {
580580
/// # Example
581581
/// ```
582582
/// # extern crate pyo3; extern crate numpy; fn main() {
583-
/// use numpy::{PyArray, IntoPyArray};
583+
/// use numpy::PyArray;
584584
/// let gil = pyo3::Python::acquire_gil();
585585
/// let pyarray_f = PyArray::<f64>::arange(gil.python(), 2.0, 5.0, 1.0);
586-
/// let pyarray_i = PyArray::<i64>::new(gil.python(), [3]);
586+
/// let pyarray_i = PyArray::<i64>::new(gil.python(), [3], false);
587587
/// assert!(pyarray_f.move_to(pyarray_i).is_ok());
588588
/// assert_eq!(pyarray_i.as_slice().unwrap(), &[2, 3, 4]);
589589
/// # }
@@ -602,7 +602,7 @@ impl<T: TypeNum> PyArray<T> {
602602
/// # Example
603603
/// ```
604604
/// # extern crate pyo3; extern crate numpy; fn main() {
605-
/// use numpy::{PyArray, IntoPyArray};
605+
/// use numpy::PyArray;
606606
/// let gil = pyo3::Python::acquire_gil();
607607
/// let pyarray_f = PyArray::<f64>::arange(gil.python(), 2.0, 5.0, 1.0);
608608
/// let pyarray_i = pyarray_f.cast::<i32>(false).unwrap();
@@ -639,7 +639,7 @@ impl<T: TypeNum> PyArray<T> {
639639
/// # #[macro_use] extern crate ndarray; extern crate pyo3; extern crate numpy; fn main() {
640640
/// use numpy::PyArray;
641641
/// let gil = pyo3::Python::acquire_gil();
642-
/// let array = PyArray::from_vec(gil.python(), (0..9).collect());
642+
/// let array = PyArray::from_exact_iter(gil.python(), 0..9);
643643
/// let array = array.reshape([3, 3]).unwrap();
644644
/// assert_eq!(array.as_array().unwrap(), array![[0, 1, 2], [3, 4, 5], [6, 7, 8]].into_dyn());
645645
/// assert!(array.reshape([5]).is_err());

src/convert.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use super::*;
1212
/// # Example
1313
/// ```
1414
/// # extern crate pyo3; extern crate numpy; fn main() {
15-
/// use numpy::{PyArray, IntoPyArray};
15+
/// use numpy::{PyArray, ToPyArray};
1616
/// let gil = pyo3::Python::acquire_gil();
17-
/// let py_array = vec![1, 2, 3].into_pyarray(gil.python());
17+
/// let py_array = vec![1, 2, 3].to_pyarray(gil.python());
1818
/// assert_eq!(py_array.as_slice().unwrap(), &[1, 2, 3]);
1919
/// # }
2020
/// ```

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
//! extern crate numpy;
1717
//! extern crate pyo3;
1818
//! use pyo3::prelude::Python;
19-
//! use numpy::{IntoPyArray, PyArray};
19+
//! use numpy::{ToPyArray, PyArray};
2020
//! fn main() {
2121
//! let gil = Python::acquire_gil();
2222
//! let py = gil.python();
23-
//! let py_array = array![[1i64, 2], [3, 4]].into_pyarray(py);
23+
//! let py_array = array![[1i64, 2], [3, 4]].to_pyarray(py);
2424
//! assert_eq!(
2525
//! py_array.as_array().unwrap(),
2626
//! array![[1i64, 2], [3, 4]].into_dyn(),

0 commit comments

Comments
 (0)