[Cython] Be more forgiving about memoryview strides

Sebastian Berg sebastian at sipsolutions.net
Thu Feb 28 16:13:17 CET 2013


Hey,

Maybe someone here already saw it (I don't have a track account, or I
would just create a ticket), but it would be nice if Cython was more
forgiving about contiguous requirements on strides. In the future this
would make it easier for numpy to go forward with changing the
contiguous flags to be more reasonable for its purpose, and second also
to allow old (and maybe for the moment remaining) corner cases in numpy
to slip past (as well as possibly the same for other programs...). An
example is (see also https://github.com/numpy/numpy/issues/2956 and the
PR linked there for more details):

def add_one(array):
    cdef double[::1] a = array
    a[0] += 1.
    return array

giving:

>>> add_one(np.ascontiguousarray(np.arange(10.)[::100]))
ValueError: Buffer and memoryview are not contiguous in the same
dimension.

This could easily be changed if MemoryViews check the strides as "can be
interpreted as contiguous". That means that if shape[i] == 1, then
strides[i] are arbitrary (you can just change them if you like). This is
also the case for 0-sized arrays, which are arguably always contiguous,
no matter their strides are!

Regards,

Sebastian

PS: A similar thing exists with np.ndarray[...] interface if the user
accesses array.strides. They get the arrays strides not the buffers.
This is not quite related, but if it would be easy to use the buffer's
strides in that case, it may make it easier if we want to change the
flags in numpy in the long term, since one could clean up strides for
forced contiguous buffer requests.



More information about the cython-devel mailing list