ctypes and buffers

Carl Banks pavlovevidence at gmail.com
Sat Sep 18 22:00:09 EDT 2010


I am creating a ctypes buffer from an existing non-ctypes object that
supports buffer protocol using the following code:


from ctypes import *

PyObject_AsReadBuffer = pythonapi.PyObject_AsReadBuffer
PyObject_AsReadBuffer.argtypes =
[py_object,POINTER(c_void_p),POINTER(c_size_t)]
PyObject_AsReadBuffer.restype = None

def ctypes_buffer_from_buffer(buf):
    cbuf = c_void_p()
    size = c_size_t()
    PyObject_AsReadBuffer(buf,byref(cbuf),byref(size))
    return cbuf


It works, but is there a standard way to do this in ctypes?  I
couldn't find anything in the documentation.  Python 2.6 for now.
Thanks.


Carl Banks



More information about the Python-list mailing list