[Pythonmac-SIG] Best/Safest way to parse NumPy array in C extension?

Lou Pecora lou_boog2000 at yahoo.com
Tue Dec 12 17:24:13 CET 2006

I've done this two ways.  Which is better or safer?

Say I'm passing a NumPy array (float) to a C
extension.  One way to "parse" it is (ignoring checks
on return values, etc. for now),

  PyArrayObject *mat;
  PyArg_ParseTuple(args, "O!", &PyArray_Type, &mat);

or is this better,

  PyObject *Pymat;
  PyArrayObject *mat;
  PyArg_ParseTuple(args, "O", &Pymat);

The latter appears to have the constraint that the
array be contiguous.  Or is that an illusion and it's
saying it _expects_ a Python object that has
contiguous data?

I've done both.  Pointing C arrays to the resulting
PyArrays' data works fine, but I fear one way or the
other might be courting disaster.

BTW, I do other checks on dimension, type, etc., but I
left those out here for clarity.  (I hope.)

-- Lou Pecora
      My views are my own.

Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.

More information about the Pythonmac-SIG mailing list