On 4/21/07, Travis Oliphant firstname.lastname@example.org wrote:
Bill Baxter wrote:
What's the right way to make a new numpy array that's a copy of some C data?
There doesn't seem to be any API like PyArray_NewFromDescr that /copies/ the void*data pointer for you. Do I have to write my own loops for this? I can do that, it just seems like it should be a library function already, so I'm guessing I'm just overlooking it. There seem to be lots of APIs that will wrap pre-existing memory, but the ones that allocate for you do not seem to copy.
What do you mean by /copies/ the void * data pointer for you? Do you mean the API would
- Create new memory for the array
- Copy the data from another void * pointer to the memory just created
for the new array?
If that is what you mean, then you are right there is no such API. I'm not sure that there needs to be one. It is a two-liner using memcpy.
Yes, I was thinking about memcpy() -- but how about non-contiguous data ? or other non well behaved ndarrays (non-aligned, byte-swapped, ...?) ?
A related question -- I'm only trying to copy in order to save myself a little hassle regarding how to clean up the allocated chunks. If there's some simple way to trigger a particular deallocation function to be called at the right time, then that would be the ideal, really.
No, there is no place to store that information in NumPy. Either the ndarray dealloc function frees the memory it created or it doesn't free any memory. I think the best thing to do in this case would be to create a memory object wrapping the pointer and then point the ndarray to it as the source of memory.
Yes, I think one would probably want to create a custom-made python class that provides the memory as buffer or so. This is what you meant - right ? And that class could then define /any/ function to be called once the ref.count goes to zero - right? Could someone with C-API knowledge put a sample together !? This would also be quite useful to be used with a SWIG output typemap.