9

In Python I'm accessing a binary file by reading it into a string and then using struct.unpack(...). Now I want to write to that string using struct.pack_into(...), but I get the error "Cannot use string as modifiable buffer". What would be a suitable buffer for use with the struct module?

mdm
  • 5,528
  • 5
  • 29
  • 28
  • Which version of python are you using? I've played with structs a bit but haven' seen that before. – chrism1 Nov 14 '09 at 00:06

3 Answers3

7

As noted in another answer, struct_pack is probably all you need and should use. However, objects of type array support the buffer protocol and can be modified:

>>> import array, struct
>>> a = array.array('c', ' ' * 1000)
>>> c = 'a'; i = 1
>>> struct.pack_into('ci', a, -0, c, i)
>>> a
array('c', 'a\x00\x00\x00\x01\x00\x00\x00  ...

The original buffer protocol was a bit of a hack primarily for C extensions. It has been deprecated and replaced by a new C-level buffer API and memoryview objects in Python 3 (and in the upcoming 2.7).

Ned Deily
  • 83,389
  • 16
  • 128
  • 151
6

If you aren't trying to pack it into a specific object, just use struct.pack to return a string.

Otherwise, ctypes.create_string_buffer is one way to obtain a mutable buffer.

Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
  • The buffer is the memory for a simple virtual machine, so I need to pack into a specific object. The memory is small but copying 128-512k for a single memory access is not an option. – mdm Nov 14 '09 at 00:24
-1

Two possibilities leap immediately to mind:

  • You can use the Python stringio module to make a read/write buffer with file semantics.

  • You can use the Python array module to get a buffer you can treat like a list, but which will contain just binary bytes.

steveha
  • 74,789
  • 21
  • 92
  • 117