Skip to content

Accept non-C-contiguous NumPy values in NDArray.__setitem__ - #763

Merged
FrancescAlted merged 1 commit into
Blosc:mainfrom
FabianIsensee:setitem-noncontiguous
Nov 6, 2026
Merged

FrancescAlted merged 1 commit into
Blosc:mainfrom
FabianIsensee:setitem-noncontiguous

Conversation

@FabianIsensee

Copy link
Copy Markdown
Contributor

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):

import blosc2, numpy as np
y = np.arange(20 * 30 * 40, dtype=np.float32).reshape(20, 30, 40)
a = blosc2.zeros((20, 30, 40), dtype=np.float32)
a[2:9, 3:17] = y[2:9, 3:17]  # ValueError: ndarray is not C-contiguous

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 when astype() happened to return a C-ordered copy, which it does not for Fortran-ordered values.

Fix. __setitem__ makes the value C-contiguous right before set_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

`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]>

@FrancescAlted FrancescAlted left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sure thing!

@FrancescAlted
FrancescAlted merged commit 3ea69b6 into Blosc:main Nov 6, 2026
37 checks passed
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.

2 participants