Skip to content

Commit 25ef24d

Browse files
committed
Merge remote-tracking branch 'origin/release-0.16'
2 parents ca2be4e + 0831a1a commit 25ef24d

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- The destructive `PyArray::resize` method is now unsafe if used without an instance of `PyReadwriteArray`. ([#302](https://github.com/PyO3/rust-numpy/pull/302))
77
- The `inner`, `dot` and `einsum` functions can also return a scalar instead of a zero-dimensional array to match NumPy's types ([#285](https://github.com/PyO3/rust-numpy/pull/285))
88
- Deprecate `PyArray::from_exact_iter` after optimizing `PyArray::from_iter`. ([#292](https://github.com/PyO3/rust-numpy/pull/292))
9+
- Fix returning invalid slices from `PyArray::{strides,shape}` for rank zero arrays. ([#303](https://github.com/PyO3/rust-numpy/pull/303))
910

1011
- v0.16.2
1112
- Fix build on platforms where `c_char` is `u8` like Linux/AArch64. ([#296](https://github.com/PyO3/rust-numpy/pull/296))

src/array.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ impl<T, D> PyArray<T, D> {
302302
// C API: https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_STRIDES
303303
pub fn strides(&self) -> &[isize] {
304304
let n = self.ndim();
305+
if n == 0 {
306+
cold();
307+
return &[];
308+
}
305309
let ptr = self.as_array_ptr();
306310
unsafe {
307311
let p = (*ptr).strides;
@@ -323,6 +327,10 @@ impl<T, D> PyArray<T, D> {
323327
// C API: https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_DIMS
324328
pub fn shape(&self) -> &[usize] {
325329
let n = self.ndim();
330+
if n == 0 {
331+
cold();
332+
return &[];
333+
}
326334
let ptr = self.as_array_ptr();
327335
unsafe {
328336
let p = (*ptr).dimensions as *mut usize;

tests/array.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ fn tuple_as_dim() {
8282
});
8383
}
8484

85+
#[test]
86+
fn rank_zero_array_has_invalid_strides_dimensions() {
87+
Python::with_gil(|py| {
88+
let arr = PyArray::<f64, _>::zeros(py, (), false);
89+
90+
assert_eq!(arr.ndim(), 0);
91+
assert_eq!(arr.strides(), &[]);
92+
assert_eq!(arr.shape(), &[]);
93+
})
94+
}
95+
8596
#[test]
8697
fn zeros() {
8798
Python::with_gil(|py| {

0 commit comments

Comments
 (0)