Skip to content

Commit 4ad2b21

Browse files
authored
Merge pull request #89 from potocpav/fix-panic
Fix panic when calling `to_owned_array` on `PyArrayDyn`
2 parents 66816a3 + d93f8b7 commit 4ad2b21

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/array.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,13 @@ impl<T: TypeNum, D: Dimension> PyArray<T, D> {
274274
}
275275

276276
fn ndarray_shape(&self) -> StrideShape<D> {
277-
// FIXME may be done more simply
278277
let shape: Shape<_> = Dim(self.dims()).into();
279-
let mut st = D::default();
280278
let size = mem::size_of::<T>();
281-
for (i, &s) in self.strides().iter().enumerate() {
282-
st[i] = s as usize / size;
283-
}
279+
let st = D::from_dimension(&Dim(
280+
self.strides().iter()
281+
.map(|&s| s as usize / size)
282+
.collect::<Vec<_>>()
283+
)).unwrap();
284284
shape.strides(st)
285285
}
286286

@@ -997,3 +997,10 @@ fn test_get_unchecked() {
997997
assert_eq!(*array.uget([1]), 2);
998998
}
999999
}
1000+
1001+
#[test]
1002+
fn test_dyn_to_owned_array() {
1003+
let gil = pyo3::Python::acquire_gil();
1004+
let array = PyArray::from_vec2(gil.python(), &vec![vec![1,2], vec![3,4]]).unwrap();
1005+
array.into_dyn().to_owned_array();
1006+
}

0 commit comments

Comments
 (0)