Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
Wrong striding used when assigning to multidimensional memory view slice #2941
I find that assigning a single value to all elements of a multidimensional memory view slice fails, unless the slice is taken along the first dimension. Example:
import numpy as np def bug(): cdef int[:, ::1] a a = 2*np.ones((2, 2), dtype=np.intc) a[:, :1] = 1 print(np.asarray(a))
When compiled and run, this prints the array
The same error is seen for memory views of larger dimension. Assignment to slices of all but the first dimension fails in this way. If I leave out the contiguousity on the declaration (i.e. use
Surely this must be a rather critical bug? I couldn't find quite this usage documented, as here either all or just a single element is assigned to. If you are not supposed to do arbitrary slice assignments with memory views, I would think that the cythonization should error out or at least emit a warning.
Tested on Python 3.7.3 with Cython 0.29.7, Python 3.7.1 with Cython 0.29.2, Python 3.5.2 with Cython 0.23.4 and Python 2.7.12 with Cython 0.23.4, all on Linux Mint 18.3 and using gcc 5.4.0.
I realise now that the probable cause is that though
Is this the expected / wanted behavior? In general Cython cannot know the shape of such a slice at compile time, so shouldn't it just emit code that deals with the general case? I.e. effectively emit code that transforms
for i in range(a.shape): for j in range(1): a[i, j] = 1