[Numpy-discussion] Contiguous memory zone with C API

Marc.Poinot at onera.fr Marc.Poinot at onera.fr
Mon Oct 21 07:22:33 EDT 2013


Hi all,

I try to change my old C API numpy code to the 'new' API. I used to hack some internal stuff in Numpy (yes it's bad...) and I wonder now how to change it.

Let's take an example: I have ocount numpy arrays with data allocated elsewhere than numpy, the data pointer of the PyArrayObject is set with the malloc'd zone.
Now I select one of these array to be the memory base for all of them, then I realloc each data pointer to make sure the ocount of them have a contiguous zone.
The code is now 'rejected' by the new API, how can I do that without hacking into the PyArray_Object?

   first=(PyArrayObject*)context->ctg_obj[0];
   psize=PyArray_NBYTES(first);
   for (j=1;j<ocount;j++)
   {
     current=(PyArrayObject*)context->ctg_obj[j];
     tsize=PyArray_NBYTES(current);
     psize+=tsize;
((PyArrayObject*)first)->data=realloc(PyArray_DATA(first),psize); /* *** how to do that with the API ? */
memcpy(PyArray_DATA(first)+psize-tsize,PyArray_DATA(current),tsize);
     free(PyArray_DATA(current));
((PyArrayObject*)current)->data=PyArray_DATA(first)+psize-tsize;
   }


I use that trick to make sure that separate numpy each representing a coordinate of a vector can be gather in a single array.
I've had a look at PyArray_resize but it requires a own_data flag which I do not have...
Any hint, remark?

-- 
-- ----------------------------------------------------------------------------
-- Marc POINOT [ONERA/DSNA] Tel:+33.1.46.73.42.84  Fax:+33.1.46.73.41.66
-- Avertissement/disclaimer http://www.onera.fr/en/emails-terms
-- ----------------------------------------------------------------------------
--




More information about the NumPy-Discussion mailing list