[Numpy-discussion] Numpy array from ctypes pointer object?

Mark Heslep mark at mitre.org
Wed Jul 12 15:03:32 CDT 2006

Travis Oliphant wrote:
> The problem here is that from Python NumPy has no way to create an 
> ndarray from a pointer.   Doing this creates a situtation where it is 
> unclear who owns the memory.  It is probably best to wrap the pointer 
> into some kind of object exposing the buffer protocol and then pass 
> that to frombuffer (or ndarray.__new__).
Yep thats where I just ended up:

from ctypes import *
import numpy as N
func = pythonapi.PyBuffer_FromMemory
func.restype = py_object
buffer = func( im.imageData, size_of_the_data )                 
<----imageData = ctypes.LP_c_ubyte object
return N.frombuffer( buffer, N.uint8 )

Works!   Im curious though:  the several projects recently using ctypes 
and numpy to wrap libraries (Pygame SDL, OpenGL, svm) must have come 
across the issue of using a creating a numpy array from a ctypes 
pointer.  Ill have to look further.

> When an ndarray is using memory that is not its own, it expects 
> another object to be "in charge" of that memory and the ndarray will  
> point its base attribute to it and increment its reference count.   
> What should the object that is "in charge" of the memory be?
> Perhaps a suitable utility function could be created that can work 
> with ctypes to create ndarrays from ctypes memory locations and either 
> own or disown the data.
I suppose that is still the case w/ PyBuffer_From.. above. That is, the 
underlying im.imageData pointer can not be released before buffer.

> This needs to be thought through a bit, however.
> -Travis
>> The attributes in nparray.__array_interface_ are not writable, so no 
>> joy there.
>> On the C side the PyArray_SimpleNewFromData( ..dimensions, ...data 
>> ptr) C API does the job nicely.  Is there a ctypes paradigm for 
>> SimpleNew...?
>> Mark
>> ------------------------------------------------------------------------- 
>> Using Tomcat but need to do more? Need to support web services, 
>> security?
>> Get stuff done quickly with pre-integrated technology to make your 
>> job easier
>> Download IBM WebSphere Application Server v.1.0.1 based on Apache 
>> Geronimo
>> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
>> _______________________________________________
>> Numpy-discussion mailing list
>> Numpy-discussion at lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/numpy-discussion

More information about the Numpy-discussion mailing list