Repository navigation
Accept non-C-contiguous NumPy values in NDArray.__setitem__ - #763
Merged
Merged
Conversation
`a[5:50, 16:76] = y[5:50, 16:76]` raised "ValueError: ndarray is not C-contiguous": set_slice() takes the value through the buffer protocol as one dense C-order buffer, and a NumPy view with gaps (a box of a larger array), with negative strides, with permuted axes, or a Fortran-ordered array cannot be handed over that way. Values of another dtype only worked when astype() happened to return a C-ordered copy (not for Fortran-ordered ones). Make the value C-contiguous before set_slice() (a no-op for C-contiguous arrays, a copy otherwise). The non-unit-step and fancy-index paths already copy. Co-Authored-By: Claude Opus 5.5 <[email protected]>
FabianIsensee
added a commit
to FabianIsensee/python-blosc2
that referenced
this pull request
Nov 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi Francesc,
A small one, found while benchmarking partial overwrites for Blosc/c-blosc2#847.
Problem. Assigning a NumPy value that is not C-contiguous raises (on main and in 4.14.1):
NDArray.set_slice()gets the value through the buffer protocol as one dense C-order buffer (PyBUF_SIMPLE), which NumPy cannot provide for a view with gaps (a box of a larger array), with negative strides or with permuted axes, nor for a Fortran-ordered array. A value of another dtype only worked whenastype()happened to return a C-ordered copy, which it does not for Fortran-ordered values.Fix.
__setitem__makes the value C-contiguous right beforeset_slice():np.ascontiguousarray(value), a no-op for C-contiguous arrays and the copy users make by hand today otherwise. The non-unit-step and fancy-index paths already copy, so they are unchanged.Tests.
test_setitem_non_contiguous_value: strided, reversed, Fortran-ordered, axis-permuted and 1-D strided values, of the array's dtype and of another one, in memory and on disk, against NumPy. 14 of the 20 cases fail on main. The full suite passes (2 b2view HTTP tests need aiohttp, which my environment lacks).🤖 Generated with Claude Code