Constructing an ndarray around a ctypes array
Hi, Is there a way to create an ndarray from a ctypes array, such that they both use the same memory space. Thanks, James
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.
James, 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 Kilian
On Thu, Nov 27, 2008 at 13:18, James Dominy <james@nbn.ac.za> wrote:
Hi,
Is there a way to create an ndarray from a ctypes array, such that they both use the same memory space.
from numpy.ctypeslib import as_array -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
participants (3)
-
James Dominy -
killian koepsell -
Robert Kern