ctypes and buffers

Carl Banks pavlovevidence at gmail.com
Sun Sep 19 04:00:09 CEST 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 =
PyObject_AsReadBuffer.restype = None

def ctypes_buffer_from_buffer(buf):
    cbuf = c_void_p()
    size = c_size_t()
    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.

Carl Banks

More information about the Python-list mailing list