Skip to content

fix(numpy): support strides that are not a multiple of itemsize in at() - #6241

Open
kroplabeskidu wants to merge 1 commit into
pybind:masterfrom
kroplabeskidu:fix-array-at-unaligned-strides
Open

kroplabeskidu wants to merge 1 commit into
pybind:masterfrom
kroplabeskidu:fix-array-at-unaligned-strides

Conversation

@kroplabeskidu

Copy link
Copy Markdown

Description

Item 18 from #6159. array_t::at() and mutable_at() divide the byte offset by itemsize() and then index a T *. That only works if the stride is a multiple of sizeof(T). With a field of a packed structured array, it reads or writes the wrong bytes:

packed = np.zeros(3, dtype=[("a", np.uint16), ("b", np.uint8)])
packed["a"] = [10, 20, 30]
m.at_t(packed["a"], 1)  # was 5120, now 20

Both now use data(index...) and mutable_data(index...), which apply the byte offset directly and have the same bounds, negative-index and writeable checks.

index_at() is unchanged. It returns an item count, which still can't represent such strides.

Suggested changelog entry:

  • array_t::at() and mutable_at() now return the right element when a stride is not a multiple of the item size.

This branch has not been deployed

No deployments
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.

1 participant