ctypes and buffers

C

Carl Banks

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
 
T

Thomas Jollans

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

If I understand what you are doing correctly, you're referencing a Python
buffer object and returning a pointer wrapped in some ctypes thingy. hmm. I
see some problems in your code:

* You're not passing the size along. In the name of sanity, why on earth
not?! The pointer cannot be safely used without knowing how long the area
of memory referenced is!

* You're using the old buffer protocol. You might rather implement this with
the one introduced with Python 3.0, and supported in 2.6 as well.
 
C

Carl Banks

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

If I understand what you are doing correctly, you're referencing a Python
buffer object and returning a pointer wrapped in some ctypes thingy. hmm. I
see some problems in your code:

 * You're not passing the size along. In the name of sanity, why on earth
   not?! The pointer cannot be safely used without knowing how long the area
   of memory referenced is!

How D'Olivero of you to overreact this way.

A. No, and
B. I already know the size of the object

 * You're using the old buffer protocol. You might rather implement this with
   the one introduced with Python 3.0, and supported in 2.6 as well.

Hmm, I didn't know they got that into 2.6. Maybe I'll do that,
thanks.


Carl Banks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top