[Cython] Be more forgiving about memoryview strides

Robert Bradshaw robertwb at gmail.com
Thu Feb 28 18:50:34 CET 2013


On Thu, Feb 28, 2013 at 7:13 AM, Sebastian Berg
<sebastian at sipsolutions.net> wrote:
> 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!

I was under the impression that the primary value for contiguous is
that it a foo[::1] can be interpreted as a foo*. Letting strides be
arbitrary completely breaks this, right?

> 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.
>
> _______________________________________________
> cython-devel mailing list
> cython-devel at python.org
> http://mail.python.org/mailman/listinfo/cython-devel


More information about the cython-devel mailing list