Skip to content

Commit f156397

Browse files
authored
Merge pull request #140 from PyO3/unsafe-mut
Introduce PyReadOnlyArray
2 parents 323a3e7 + dd54730 commit f156397

File tree

13 files changed

+338
-131
lines changed

13 files changed

+338
-131
lines changed

.gitignore

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ rusty-tags.*
88
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
99
Cargo.lock
1010

11-
# cargo fmt
12-
*.bk
13-
.tox/
11+
# Some Python specific files
12+
.tox/
13+
build/
14+
*.so
15+
*.egg-info/
16+
**/dist/
17+
__pycache__

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changelog
22
- v0.10.0
33
- Remove `ErrorKind` and introduce some concrete error types
4+
- `PyArray::as_slice`, `PyArray::as_slice_mut`, `PyArray::as_array`, and `PyArray::as_array_mut` is now unsafe.
5+
- Introduce `PyArray::as_cell_slice`, `PyArray::to_vec`, and `PyArray::to_owned_array`
46

57
- v0.9.0
68
- Update PyO3 to 0.10.0

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ default = []
2323
# This is no longer needed but setuptools-rust assumes this feature
2424
python3 = ["pyo3/python3"]
2525

26+
27+
[workspace]
28+
members = ["examples/*"]

README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ fn main_<'py>(py: Python<'py>) -> PyResult<()> {
8383
let pyarray: &PyArray1<i32> = py
8484
.eval("np.absolute(np.array([-1, -2, -3], dtype='int32'))", Some(&dict), None)?
8585
.extract()?;
86-
let slice = pyarray.as_slice()?;
86+
let readonly = pyarray.readonly();
87+
let slice = readonly.as_slice().unwrap();
8788
assert_eq!(slice, &[1, 2, 3]);
8889
Ok(())
8990
}
@@ -109,38 +110,38 @@ features = ["extension-module"]
109110

110111
```rust
111112
use ndarray::{ArrayD, ArrayViewD, ArrayViewMutD};
112-
use numpy::{IntoPyArray, PyArrayDyn};
113-
use pyo3::prelude::{pymodule, Py, PyModule, PyResult, Python};
113+
use numpy::{IntoPyArray, PyArrayDyn, PyReadonlyArrayDyn};
114+
use pyo3::prelude::{pymodule, PyModule, PyResult, Python};
114115

115116
#[pymodule]
116-
fn rust_ext(_py: Python, m: &PyModule) -> PyResult<()> {
117+
fn rust_ext(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
117118
// immutable example
118-
fn axpy(a: f64, x: ArrayViewD<f64>, y: ArrayViewD<f64>) -> ArrayD<f64> {
119+
fn axpy(a: f64, x: ArrayViewD<'_, f64>, y: ArrayViewD<'_, f64>) -> ArrayD<f64> {
119120
a * &x + &y
120121
}
121122

122123
// mutable example (no return)
123-
fn mult(a: f64, mut x: ArrayViewMutD<f64>) {
124+
fn mult(a: f64, mut x: ArrayViewMutD<'_, f64>) {
124125
x *= a;
125126
}
126127

127128
// wrapper of `axpy`
128129
#[pyfn(m, "axpy")]
129-
fn axpy_py(
130-
py: Python,
130+
fn axpy_py<'py>(
131+
py: Python<'py>,
131132
a: f64,
132-
x: &PyArrayDyn<f64>,
133-
y: &PyArrayDyn<f64>,
134-
) -> Py<PyArrayDyn<f64>> {
133+
x: PyReadonlyArrayDyn<f64>,
134+
y: PyReadonlyArrayDyn<f64>,
135+
) -> &'py PyArrayDyn<f64> {
135136
let x = x.as_array();
136137
let y = y.as_array();
137-
axpy(a, x, y).into_pyarray(py).to_owned()
138+
axpy(a, x, y).into_pyarray(py)
138139
}
139140

140141
// wrapper of `mult`
141142
#[pyfn(m, "mult")]
142-
fn mult_py(_py: Python, a: f64, x: &PyArrayDyn<f64>) -> PyResult<()> {
143-
let x = x.as_array_mut();
143+
fn mult_py(_py: Python<'_>, a: f64, x: &PyArrayDyn<f64>) -> PyResult<()> {
144+
let x = unsafe { x.as_array_mut() };
144145
mult(a, x);
145146
Ok(())
146147
}

examples/.gitignore

Lines changed: 0 additions & 5 deletions
This file was deleted.

examples/simple-extension/src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ndarray::{ArrayD, ArrayViewD, ArrayViewMutD};
2-
use numpy::{IntoPyArray, PyArrayDyn};
3-
use pyo3::prelude::{pymodule, Py, PyModule, PyResult, Python};
2+
use numpy::{IntoPyArray, PyArrayDyn, PyReadonlyArrayDyn};
3+
use pyo3::prelude::{pymodule, PyModule, PyResult, Python};
44

55
#[pymodule]
66
fn rust_ext(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
@@ -16,21 +16,21 @@ fn rust_ext(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
1616

1717
// wrapper of `axpy`
1818
#[pyfn(m, "axpy")]
19-
fn axpy_py(
20-
py: Python<'_>,
19+
fn axpy_py<'py>(
20+
py: Python<'py>,
2121
a: f64,
22-
x: &PyArrayDyn<f64>,
23-
y: &PyArrayDyn<f64>,
24-
) -> Py<PyArrayDyn<f64>> {
22+
x: PyReadonlyArrayDyn<f64>,
23+
y: PyReadonlyArrayDyn<f64>,
24+
) -> &'py PyArrayDyn<f64> {
2525
let x = x.as_array();
2626
let y = y.as_array();
27-
axpy(a, x, y).into_pyarray(py).to_owned()
27+
axpy(a, x, y).into_pyarray(py)
2828
}
2929

3030
// wrapper of `mult`
3131
#[pyfn(m, "mult")]
3232
fn mult_py(_py: Python<'_>, a: f64, x: &PyArrayDyn<f64>) -> PyResult<()> {
33-
let x = x.as_array_mut();
33+
let x = unsafe { x.as_array_mut() };
3434
mult(a, x);
3535
Ok(())
3636
}

0 commit comments

Comments
 (0)