Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > writeable buffer and struct.pack_into and struct.unpck_from

Reply
Thread Tools

writeable buffer and struct.pack_into and struct.unpck_from

 
 
Tzury Bar Yochay
Guest
Posts: n/a
 
      09-20-2008
Hi,

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?
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      09-20-2008
En Sat, 20 Sep 2008 15:45:48 -0300, Tzury Bar Yochay
<(E-Mail Removed)> 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, 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

 
Reply With Quote
 
 
 
 
Aaron \Castironpi\ Brady
Guest
Posts: n/a
 
      09-21-2008
On Sep 20, 6:42*pm, "Gabriel Genellina" <(E-Mail Removed)>
wrote:
> En Sat, 20 Sep 2008 15:45:48 -0300, Tzury Bar Yochay *
> <(E-Mail Removed)> 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.

 
Reply With Quote
 
Tzury Bar Yochay
Guest
Posts: n/a
 
      09-21-2008
Thanks Gabriel,
I was missing the information how to create a writable buffer.
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      09-21-2008
On Sep 21, 3:16*pm, Tzury Bar Yochay <(E-Mail Removed)> wrote:
> Thanks Gabriel,
> I was missing the information how to create a writable buffer.


array.array objects also have the writable buffer nature:

>>> import array
>>> b = array.array('c', '\0' * 10)
>>> b

array('c', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
>>> import struct
>>> struct.pack_into('<hhhh', b, 0, 1, 2, -1, -3276
>>> b

array('c', '\x01\x00\x02\x00\xff\xff\x00\x80\x00\x00')

HTH,
John
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a writeable buffer? Steve Chaplin Python 0 11-20-2006 12:29 PM
writeable checkbox in datagrid from bit field in table hazz ASP .Net 4 04-27-2006 08:15 PM
How to create writeable pdf file? diggerodell@gmail.com Computer Support 3 03-25-2006 02:18 AM
Howto suppress a warning "insecure world writeable .." in Ruby 1.8.4 ? Nuralanur@aol.com Ruby 2 01-25-2006 02:20 PM
Is argv writeable? Alexander Malkis C++ 9 05-23-2004 03:02 PM



Advertisments