[Numpy-discussion] C-API: Subclassing array for numpy 1.7 (with #define NPY_NO_DEPRECATED_API 0x00000007)

Marc Gronle marc.gronle at ito.uni-stuttgart.de
Sun Mar 24 10:53:40 EDT 2013

Hello together,

we embedded Python 3 in a C++ environment. In this application I created a
new class, that is a subclass from numpy array.
Until now (numpy 1.6 or numpy 1.7 without the deprecation define
(NPY_NO_DEPRECATED_API) the typedef for my class describing the object
was something like

typedef struct
    PyArrayObject numpyArray;
    int myMember1;
    int myMember2;
}  PySubclassObject

This always worked for me and a call to PyArray_NDIM(obj) returned the
number of dimensions, where obj is of type PySubclassObject*.

If I am now using numpy 1.7 this also works if the line #define
NPY_NO_DEPRECATED_API 0x00000007 is not set.
However, if I removed all deprecated content, there occurrs the first
runtime error when creating an instance of PySubclassObject.

This is due to the fact, that PyArrayObject now only is a tiny typedef of
the following form

typedef struct tagPyArrayObject {
} PyArrayObject;

Previously this has been something like

typedef struct tagPyArrayObject {
    char *data;
    int nd;
    npy_intp *dimensions;
} PyArrayObject;

Usually, when creating only one np.array, there is extra space allocated
depending on the size of PyArrayObject_fields. However, in my subclass
I don't know how to add that extra space between the members numpyArray and
myMember1. Finally, when calling
PyArray_NDIM(obj) like above, the obj-pointer is casted in the macro to
PyArrayObject_fields*. This yields an access conflict with myMember1,
and the members of PyArrayObject_fields.

I hope this description was clear enough to understand my problem. Has
anybody an idea what I need to change such that the subclassing also works
for the new
numpy structure?

Thanks for any answer.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130324/e3dcd841/attachment.html>

More information about the NumPy-Discussion mailing list