Skip to content

Fix panic in as_array/as_array_mut for arrays with a zero-length dimension#558

Open
teddytennant wants to merge 2 commits into
PyO3:mainfrom
teddytennant:fix-zero-size-array-panic
Open

Fix panic in as_array/as_array_mut for arrays with a zero-length dimension#558
teddytennant wants to merge 2 commits into
PyO3:mainfrom
teddytennant:fix-zero-size-array-panic

Conversation

@teddytennant

Copy link
Copy Markdown

NumPy reports zero strides for arrays that have a zero-length axis (e.g. shape [2, 0] → strides (0, 0)). as_view passed these straight into ndarray's from_shape_ptr, whose aliasing check rejects a zero stride on an axis of length > 1 and panics ("The strides must not allow any element to be referenced by two different indices") — even though the array holds no elements and cannot alias anything. This affected both as_array and as_array_mut.

Fix: in as_view's inner, when the shape has size 0, substitute clamped C-contiguous strides (computed from shape[i].max(1)). The data pointer is never dereferenced for an empty array, so the fabricated strides are sound for both immutable and mutable views. Keeps the existing infallible signature (no breaking Result change).

Adds a regression test covering [2, 0] (immutable + mutable), [0, 2], and 3-D [2, 0, 3].

Closes #527.

Copilot AI review requested due to automatic review settings July 8, 2026 01:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

panic in as_array_mut

2 participants