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

Jochen Schroeder cycomanic@gmail....
Mon Mar 23 15:03:48 CDT 2009


On 23/03/09 15:40, Sturla Molden wrote:
> 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 )
> 

Might I suggest that restype is going to be removed from the  documentation, 
it also cost me quite some time trying to get ndpointer to work with restype 
when I first tried it until I finally came to the conclusion that an
approach like the above is necessary and ndpointer does not work with
restype. 

Cheers
Jochen


More information about the Numpy-discussion mailing list