ctypes and buffers

Discussion in 'Python' started by Carl Banks, Sep 19, 2010.

  1. Carl Banks

    Carl Banks Guest

    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
     
    Carl Banks, Sep 19, 2010
    #1
    1. Advertising

  2. On Sunday 19 September 2010, it occurred to Carl Banks to exclaim:
    > 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.

    >
    > 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
     
    Thomas Jollans, Sep 19, 2010
    #2
    1. Advertising

  3. Carl Banks

    Carl Banks Guest

    On Sep 19, 5:10 am, Thomas Jollans <> wrote:
    > On Sunday 19 September 2010, it occurred to Carl Banks to exclaim:
    > > 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
     
    Carl Banks, Sep 19, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike
    Replies:
    4
    Views:
    5,061
    Kevin McMurtrie
    Aug 3, 2004
  2. Henk Punt
    Replies:
    0
    Views:
    403
    Henk Punt
    Jul 23, 2004
  3. Replies:
    3
    Views:
    269
  4. Replies:
    0
    Views:
    506
  5. Pakal
    Replies:
    1
    Views:
    1,748
    Pakal
    Jan 10, 2010
Loading...

Share This Page