[Numpy-discussion] numpy C API bugs?

Travis Oliphant oliphant.travis at ieee.org
Thu Mar 9 21:29:02 CST 2006

David Ian Brown wrote:
>> I have recently been engaged in updating the PyNIO module 
>> (http://www.pyngl.ucar.edu/Nio.html)
>> to work with the NumPy C API. It was originally coded using the 
>> Numeric's C API.
>> In the process I have encountered what appear to me to be a couple 
>> of  bugs in the interface.  They were relatively minor  and not  
>> difficult to work around, but you would probably like to know of them 
>> if you don't already.
>> In the NumPy version of arrayobject.h the macros PyArray_GETITEM and 
>> PyArray_SETITEM
>> are defined in way that does not work.  Here is the GETITEM macro:
>>  #define PyArray_GETITEM(obj,itemptr)                    \
>>        ((char *)itemptr,       \
>>                                           (PyArrayObject *)obj);

Thank you very much for catching this.  The introduction of 'f' was more 
recent then the writing of these macros and so this error crept in.    
I'm definitely glad to fix it.

> Correction: actually "Guide to NumPy" has the 'getitem' function 
> documented with the arguments ordered
> as  implemented in NumPy (but still opposite from Numeric). Only the 
> macro is documented opposite from
> its implementation.

I think further clarification is needed. 

First of all Numeric did not have macros.  They just had function 
pointers to getitem. 
Second, Numpy's function pointers to getitem and setitem are almost 
exactly the same.  The only difference is the *addition* of another 
argument to both functions to allow the array to be passed in (so that 
the size of the array and whether or not it's well-behaved can be known).  

So, the order is *not* different in NumPy and Numeric, there is just 
another argument needed.  

The new macros introduced do have a different order (and I don't think 
it's documented oppositely --- it's perhaps not documented well at all, 
however :-) and so may be confusing).

It is recommended to use the macros.  They are

PyArray_GETITEM(array, itemptr)

Return a Python object corresponding to the item in the array at the 
location itemptr.

PyArray_SETITEM(array, itemptr, obj)

Set object into the array at the memory pointed to by itemptr (an 
attempt is made to convert to the right type if that is possible). 

Thanks for the report.


More information about the Numpy-discussion mailing list