On Sep 20, 6:42 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Sat, 20 Sep 2008 15:45:48 -0300, Tzury Bar Yochay  
> <[EMAIL PROTECTED]> escribió:
>
> > I can't find in the documentation the way to use these two functions.
>
> > can someone share a simple code that utilize these two functions?
>
> struct.pack_into is intended to "fill" a buffer you got from somewhere,  
> probably other language or process. ctypes.create_string_buffer may be  
> used to create a writable buffer in python code.
>
> py> from ctypes import create_string_buffer
> py> b = create_string_buffer(10)
> py> b.raw
> '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
> py> from struct import *
> py> pack_into("hhh", b, 0, 1, 2, -1)
> py> b.raw
> '\x01\x00\x02\x00\xff\xff\x00\x00\x00\x00'
>
> unpack_from does the opposite.
> Before Python 2.5,

That was when Python broke string immutability with the ctypes module.

> you had to use pack to create a string object, and then  
> copy its contents into the destination buffer; using pack_into avoids the  
> memory copy.
>
> --
> Gabriel Genellina

'mmap' seems to have always offered random-access writes.  A search
through the implementation for "tp_as_buffer" returns a lot of zeros
however.

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to