Skip to content

Commit 480bf55

Browse files
authored
Merge pull request #235 from adamreichold/owned-data-writeable
Make arrays backed by owned data writeable.
2 parents 8651717 + 64ba7df commit 480bf55

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
- Unreleased
4+
- Make arrays produced via `IntoPyArray`, i.e. those owning Rust data, writeable ([#235](https://github.com/PyO3/rust-numpy/pull/235))
5+
36
- v0.15.0
47
- [Remove resolver from Cargo.toml](https://github.com/PyO3/rust-numpy/pull/202)
58
- [Bump PyO3 to 0.15](https://github.com/PyO3/rust-numpy/pull/212)

src/array.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
432432
ptr::null_mut(), // data
433433
0, // itemsize
434434
flag, // flag
435-
ptr::null_mut(), //obj
435+
ptr::null_mut(), // obj
436436
);
437437
Self::from_owned_ptr(py, ptr)
438438
}
@@ -458,11 +458,11 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
458458
dims.ndim_cint(),
459459
dims.as_dims_ptr(),
460460
T::npy_type() as i32,
461-
strides as *mut _, // strides
462-
data_ptr as _, // data
463-
mem::size_of::<T>() as i32, // itemsize
464-
0, // flag
465-
ptr::null_mut(), //obj
461+
strides as *mut _, // strides
462+
data_ptr as _, // data
463+
mem::size_of::<T>() as i32, // itemsize
464+
npyffi::NPY_ARRAY_WRITEABLE, // flag
465+
ptr::null_mut(), // obj
466466
);
467467
PY_ARRAY_API.PyArray_SetBaseObject(ptr as *mut npyffi::PyArrayObject, cell as _);
468468
Self::from_owned_ptr(py, ptr)

tests/to_py.rs

+10
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ fn into_pyarray_cant_resize() {
115115
})
116116
}
117117

118+
#[test]
119+
fn into_pyarray_can_write() {
120+
let a = vec![1, 2, 3];
121+
pyo3::Python::with_gil(|py| {
122+
let arr = a.into_pyarray(py);
123+
pyo3::py_run!(py, arr, "assert arr.flags['WRITEABLE']");
124+
pyo3::py_run!(py, arr, "arr[1] = 4");
125+
})
126+
}
127+
118128
/// Check that into_pyarray works for ndarray of which the pointer of the first element is
119129
/// not at the start. See https://github.com/PyO3/rust-numpy/issues/182 for more
120130
#[test]

0 commit comments

Comments
 (0)