[Numpy-tickets] [NumPy] #918: SETITEM and GETITEM macros end with semicolons

NumPy numpy-tickets@scipy....
Thu Sep 25 00:57:04 CDT 2008

#918: SETITEM and GETITEM macros end with semicolons
 Reporter:  jason       |       Owner:  somebody
     Type:  defect      |      Status:  new     
 Priority:  normal      |   Milestone:  1.2.0   
Component:  numpy.core  |     Version:  1.1     
 Severity:  major       |    Keywords:          
 The macros PyArray_SETITEM (and PyArray_GETITEM) in ndarrayobject.h end in
 semicolons, which causes problems as detailed below.  Can we remove the
 semicolon, but wrap the entire macro body in parentheses?  Are there other
 macros that end in semicolons that shouldn't?

 (In the email below, the replies are from Chuck Harris)
 On Wed, Sep 24, 2008 at 9:25 PM, <jason-sage@creativetrax.com> wrote:

     I'm working on getting the Sage matrices for real/complex doubles to
     numpy as a backend.  In this, I'm using the PyArray_SETITEM macro from
     within Cython.  However, Cython wraps the macro in a function call to
     convert the output to a Python value:

      __pyx_1 = PyInt_FromLong(PyArray_SETITEM(__pyx_v_self->_matrix_numpy,
     PyArray_GETPTR2(__pyx_v_self->_matrix_numpy, __pyx_v_i, __pyx_v_j),

     However, after preprocessing, because of the semicolon at the end of
     SETITEM macro, we get:

 Hmm, not good. IMHO, macros should be ended with semicolons *when used*,
 not in themselves.

     *)(__pyx_v_value), (char *)(((void *)((((PyArrayObject
     *)(__pyx_v_self->_matrix_numpy))->data) + (__pyx_v_i)*(((PyArrayObject
     *)(__pyx_v_self->_matrix_numpy))->strides)[0] +
     *)(__pyx_v_self->_matrix_numpy))->strides)[1]))), (PyArrayObject

     Note that at the end, we have a "););".  The file refuses to compile.
     Presumably, since SETITEM returns a value, wrapping the return value
     a function call seems to be a reasonable thing to do.  Would there be
     problem in eliminating the semicolon and instead wrapping the entire
     function body in parenthesis?

 We should do this if possible. Open a ticket.

     I noticed that GETITEM also ended in a semicolon, though I didn't have
     the same problem as above since Cython didn't automatically wrap it in
     function call (I'm not even sure if it returns something).

     On a side note, is the above the best way (i.e., fastest way given an
     arbitrary numpy array) to set/get
     an element?

 Don't know.


Ticket URL: <http://scipy.org/scipy/numpy/ticket/918>
NumPy <http://projects.scipy.org/scipy/numpy>
The fundamental package needed for scientific computing with Python.

More information about the Numpy-tickets mailing list