[Cython] change strides attribute

Sebastian Berg sebastian at sipsolutions.net
Fri Dec 7 11:36:02 CET 2012


The current numpy contiguous flags are relatively restrictive. This
means that for example an array of `shape == (1,3,1)` and `itemsize ==
1` is C-Contiguous if `strides == (3,1,1)` and F-Contiguous if `strides
== (1,1,3)`. It would simplify flags settings and avoid copies here and
there and generally be what most seem to expect to ignore the
`strides[i]` if `shape[i] == 1` (or all if the array has 0 size). Which
means that for example `strides == (0,1,-32)` (so arbitrary but
`strides[1]`) would be both C- and F-contiguous.

However trying to change this, runs into problems with user code relying
on `strides[-1] == itemsize` (C-Contiguous) or `strides[0] == itemsize`
(F-Contiguous). But there seems to be no way to really deprecate or
protect Cython generated C-Code against such a change, because even if
the contiguous buffer requested by Cython has nice strides, a user
writing `arr.strides` accesses the original array which may not have and
there is no way to give compile time warnings by deprecating flags. So I
was wondering (I don't know cython that well) if `arr.strides` could not
use `buffer.strides` internally to allow redefinition of contiguous
flags in numpy in some future without breaking existing Cython user
code. Maybe it is not worth it to go through much trouble for these
flags but it seems nicer to me for code simplicity and just generally to
be more consistent to redefine them at some point.



More information about the cython-devel mailing list