LP_c_ubyte buffer and length to python string?

Dan Stromberg <[email protected]> Sat, 23 Jul 2011 14:27:14 -0700
Newsgroups gmane.comp.python.ctypes
Message-ID <CAGGBd_rAGzgtu_07ZrwT84t=9iOa1wT8aF5fytFACt+EhikJvg@mail.gmail.com>
I've googled on this for hours, and found nothing that seems close to what I
want to do.

I have a C function that accepts a preallocated buffer and a pointer to an
integer.  It then stashes data in the buffer, and passes back the buffer's
"in use" length in the integer.

I'm creating the buffer with:

      ctypes_compressed_buffer_char_p =
ctypes.create_string_buffer(maximum_size)
      ctypes_compressed_buffer =
ctypes.cast(ctypes_compressed_buffer_char_p, ctypes.POINTER(ctypes.c_ubyte))

Then I create my length with:

      ctypes_compressed_size = ctypes.c_size_t(0)
      ctypes_compressed_size_pointer =
ctypes.cast(ctypes.addressof(ctypes_compressed_size), ctypes.c_void_p)

The length is actually a size_t, but passing a pointer to size_t in ctypes
was giving me type errors, hence the void *.

Anyway, the function executes without reporting any errors, so I seem to
have my result - in a ctypes format.  Printing its type reports that its an
LP_c_ubyte.

How can I convert the resulting LP_c_ubyte (array) and int to a Python 2.x
str and a 3.x bytes?

(So far this application works out of the box on 2.x and 3.x, sans 2to3 or
3to2 - I'd prefer to keep it that way if practical)

Thanks!

PS: The application is a Python backup program, and can be found at
http://stromberg.dnsalias.org/~dstromberg/backshift/  The code quoted above
is in an SVN external reference of said project, called xz_mod:
http://stromberg.dnsalias.org/svn/xz_mod/trunk/

------------------------------------------------------------------------------
Storage Efficiency Calculator
This modeling tool is based on patent-pending intellectual property that
has been used successfully in hundreds of IBM storage optimization engage-
ments, worldwide.  Store less, Store more with what you own, Move data to 
the right place. Try It Now! http://www.accelacomm.com/jaw/sfnl/114/51427378/

_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users