[Numpy-discussion] numpy.ctypeslib.ndpointer and the restype attribute

Sturla Molden sturla@molden...
Mon Mar 23 09:40:42 CDT 2009

Jens Rantil wrote:
> Hi all,
> So I have a C-function in a DLL loaded through ctypes. This particular 
> function returns a pointer to a double. In fact I know that this 
> pointer points to the first element in an array of, say for simplicity, 
> 200 elements.
> How do I convert this pointer to a NumPy array that uses this data (ie. 
> no copy of data in memory)? I am able to create a numpy array using a 
> copy of the data.

def fromaddress(address, nbytes, dtype=double):

    class Dummy(object): pass

    d = Dummy()

    d.__array_interface__ = {

         'data' : (address, False),

         'typestr' : numpy.uint8.str,

         'descr' : numpy.uint8.descr,

         'shape' : (nbytes,),

         'strides' : None,

         'version' : 3


    return numpy.asarray(d).view( dtype=dtype )

More information about the Numpy-discussion mailing list