writeable buffer and struct.pack_into and struct.unpck_from

Discussion in 'Python' started by Tzury Bar Yochay, Sep 20, 2008.

  1. 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?
     
    Tzury Bar Yochay, Sep 20, 2008
    #1
    1. Advertising

  2. En Sat, 20 Sep 2008 15:45:48 -0300, Tzury Bar Yochay
    <> 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
     
    Gabriel Genellina, Sep 21, 2008
    #2
    1. Advertising

  3. On Sep 20, 6:42 pm, "Gabriel Genellina" <>
    wrote:
    > En Sat, 20 Sep 2008 15:45:48 -0300, Tzury Bar Yochay  
    > <> 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.
     
    Aaron \Castironpi\ Brady, Sep 21, 2008
    #3
  4. Thanks Gabriel,
    I was missing the information how to create a writable buffer.
     
    Tzury Bar Yochay, Sep 21, 2008
    #4
  5. Tzury Bar Yochay

    John Machin Guest

    On Sep 21, 3:16 pm, Tzury Bar Yochay <> 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, -32768)
    >>> b

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

    HTH,
    John
     
    John Machin, Sep 21, 2008
    #5
    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. Steve Chaplin

    How to create a writeable buffer?

    Steve Chaplin, Nov 20, 2006, in forum: Python
    Replies:
    0
    Views:
    845
    Steve Chaplin
    Nov 20, 2006
  2. Hans Müller
    Replies:
    1
    Views:
    558
    Emile van Sebille
    Mar 19, 2010
  3. palmeira

    write binary with struct.pack_into

    palmeira, Oct 6, 2012, in forum: Python
    Replies:
    3
    Views:
    321
    Alexander Blinne
    Oct 6, 2012
  4. Dennis Lee Bieber

    Re: write binary with struct.pack_into

    Dennis Lee Bieber, Oct 6, 2012, in forum: Python
    Replies:
    2
    Views:
    176
    Chris Angelico
    Oct 6, 2012
  5. Chris Angelico

    Re: write binary with struct.pack_into

    Chris Angelico, Oct 6, 2012, in forum: Python
    Replies:
    0
    Views:
    218
    Chris Angelico
    Oct 6, 2012
Loading...

Share This Page