[Numpy-discussion] Best way to inherit from PyArrayIterObject at the C level ?

David Cournapeau david@ar.media.kyoto-u.ac...
Wed May 27 08:08:00 CDT 2009


Hi,

    I have been scratching my head on the following problem. I am
designing a new array iterator, in C, to walk into a neighborhood of an
array. I would like this iterator to 'inherit' from PyArrayIterObject,
so that I can design some API which accept both PyArrayIterObject and
PyArrayNeighIterObject (through pointer casting). I have tried something
like:

typedef struct {
    /* first item is the base class, so that casting a
PyArrayNeighIterObject* to PyArrayIterObject* works */
    PyArrayIterObject base;

    /* PyArrayNeighIterObject specific members */
    ....
} PyArrayNeighIterObject;

But this forces me to cast a PyArrayNeighIterObject* to
PyArrayIterObject whenever I want to access members of the base
instance. The alternative is to copy the PyArrayIterObject members by
hand, as is currently done in numpy itself (for broadcasting iterator
PyArrayMapIterObject). But since my iterator lives outside numpy, this
is really error-prone IMHO - there will be crashes whenever the
PyArrayIterObject struct changes in an ABI incompatible way, and this
may be quite hard to debug

Is there a better way ?

David


More information about the Numpy-discussion mailing list