[SciPy-dev] Constructing an ndarray around a ctypes array

killian koepsell koepsell@gmail....
Fri Nov 28 22:50:44 CST 2008

On Thu, Nov 27, 2008 at 11:18 AM, James Dominy <james@nbn.ac.za> wrote:
> Is there a way to create an ndarray from a ctypes array, such that they both use
> the same memory space.


you can use the function PyBuffer_FromMemory or PyBuffer_FromReadWriteMemory
if you want to have write access to the memory space from python. I
use the following
python function:

def array_from_memory(pointer,shape,dtype):
    import ctypes as C
    import numpy as np
    from_memory = C.pythonapi.PyBuffer_FromReadWriteMemory
    from_memory.restype = C.py_object
    arr = np.empty(shape=shape,dtype=dtype)
    arr.data = from_memory(pointer,arr.nbytes)
    return arr


More information about the Scipy-dev mailing list