[Cython] Upcoming issues with NumPy deprecated APIs and Cython's sizeof checks

Lisandro Dalcin dalcinl at gmail.com
Mon Jan 30 22:03:03 CET 2012

I'm testing my code with numpy-dev. They are trying to discourage use
of deprecated APIs, this includes direct access to the ndarray struct.
In order to update your code, you have to pass -DNPY_NO_DEPRECATED_API
to the C compiler (or #define it before including NumPy headers).

However, they have implemented this feature by exposing the ndarray
type with just the Python object header:

Obviously, this interact bad with Cython's sizeof check, I'm getting
this runtime warning:

RuntimeWarning: numpy.ndarray size changed, may indicate binary

I think there is nothing Cython can do about this (other than
special-casing NumPy to disable this VERY useful warning).

 I've tried the patch below with success, but I'm not convinced...
Does any of you have a suggestion for NumPy folks about how to improve

diff --git a/numpy/core/include/numpy/ndarraytypes.h
index 0288272..1fcbf52 100644
--- a/numpy/core/include/numpy/ndarraytypes.h
+++ b/numpy/core/include/numpy/ndarraytypes.h
@@ -695,6 +695,7 @@ typedef struct tagPyArrayObject_fields {
 typedef struct tagPyArrayObject {
+        char _npy_array_fields[sizeof(PyArrayObject_fields)-sizeof(PyObject)];
 } PyArrayObject;

Lisandro Dalcin
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
3000 Santa Fe, Argentina
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169

More information about the cython-devel mailing list