writeable buffer and struct.pack_into and struct.unpck_from

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tzury Bar Yochay

    writeable buffer and struct.pack_into and struct.unpck_from

    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?
  • Gabriel Genellina

    #2
    Re: writeable buffer and struct.pack_int o and struct.unpck_fr om

    En Sat, 20 Sep 2008 15:45:48 -0300, Tzury Bar Yochay
    <Afro.Systems@g mail.comescribi ó:
    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_int o is intended to "fill" a buffer you got from somewhere,
    probably other language or process. ctypes.create_s tring_buffer may be
    used to create a writable buffer in python code.

    pyfrom ctypes import create_string_b uffer
    pyb = create_string_b uffer(10)
    pyb.raw
    '\x00\x00\x00\x 00\x00\x00\x00\ x00\x00\x00'
    pyfrom struct import *
    pypack_into("hh h", b, 0, 1, 2, -1)
    pyb.raw
    '\x01\x00\x02\x 00\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

    Comment

    • Aaron \Castironpi\ Brady

      #3
      Re: writeable buffer and struct.pack_int o and struct.unpck_fr om

      On Sep 20, 6:42 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
      wrote:
      En Sat, 20 Sep 2008 15:45:48 -0300, Tzury Bar Yochay  
      <Afro.Syst...@g mail.comescribi ó:
      >
      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_int o is intended to "fill" a buffer you got from somewhere,  
      probably other language or process. ctypes.create_s tring_buffer may be  
      used to create a writable buffer in python code.
      >
      pyfrom ctypes import create_string_b uffer
      pyb = create_string_b uffer(10)
      pyb.raw
      '\x00\x00\x00\x 00\x00\x00\x00\ x00\x00\x00'
      pyfrom struct import *
      pypack_into("hh h", b, 0, 1, 2, -1)
      pyb.raw
      '\x01\x00\x02\x 00\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_buff er" returns a lot of zeros
      however.

      Comment

      • Tzury Bar Yochay

        #4
        Re: writeable buffer and struct.pack_int o and struct.unpck_fr om

        Thanks Gabriel,
        I was missing the information how to create a writable buffer.

        Comment

        • John Machin

          #5
          Re: writeable buffer and struct.pack_int o and struct.unpck_fr om

          On Sep 21, 3:16 pm, Tzury Bar Yochay <Afro.Syst...@g mail.comwrote:
          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\x 00\x00\x00\x00\ x00\x00\x00')
          >>import struct
          >>struct.pack_i nto('<hhhh', b, 0, 1, 2, -1, -32768)
          >>b
          array('c', '\x01\x00\x02\x 00\xff\xff\x00\ x80\x00\x00')

          HTH,
          John

          Comment

          Working...