[Python-Dev] Memoryviews should expose the underlying memory address

Richard Oudkerk shibturn at gmail.com
Thu Sep 20 19:57:37 CEST 2012


On 20/09/2012 5:53pm, David Beazley wrote:
> How?  I must be missing something very obvious.

I would not call it obvious, but you can do

     >>> m = memoryview(bytearray(5))
     >>> ctypes.addressof(ctypes.c_char.from_buffer(m))
     149979304

However, this only works for writable memoryviews.  For
read-only memoryviews you could do

     >>> obj = ctypes.py_object(m)
     >>> address = ctypes.c_void_p()
     >>> length = ctypes.c_ssize_t()
     >>> ctypes.pythonapi.PyObject_AsReadBuffer(obj, 
ctypes.byref(address), ctypes.byref(length))
     0
     >>> address, length
     (c_void_p(149979304), c_long(5))




More information about the Python-Dev mailing list