[docs] possible error on http://docs.python.org/library/struct.html

Michael Shannon feral.and.abberrant at gmail.com
Thu Sep 23 03:22:54 CEST 2010


I was trying the example 7.3.2.3 - and when I tried:
>>> unpack( 'hhl', '\x00\x01\x00\x02\x00\x00\x00\x03')

I got the following error:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' does not support the buffer interface

However, if I declared the second string to be a byte array:
>>> unpack( 'hhl', b'\x00\x01\x00\x02\x00\x00\x00\x03')
(256, 512, 50331648)
... it works :-)


I'm running:
aMac:~ mike$ python3 -V
Python 3.1.2
aMac:~ mike$ uname -a
Darwin dd-wrt.local 10.4.0 Darwin Kernel Version 10.4.0: Fri Apr 23 18:27:12
PDT 2010; root:xnu-1504.7.4~1/RELEASE_X86_64 x86_64

So to summarize; I think that the example 7.3.2.3 needs a 'b' char added to
the beginning of the second string, to declare it to be a byte array.

cheers,

m i k e

ps - here's some other *possibly relevant" examples of how it works on this
platform:
>>> calcsize( 'hhl' )
8
>>> unpack( 'hhl', b'\x01\x00\x02\x00\x03\x00\x00\x00')
(1, 2, 3)
>>> unpack( '<hhl', b'\x00\x01\x00\x02\x00\x00\x00\x03')
(256, 512, 50331648)
>>> unpack( '>hhl', b'\x00\x01\x00\x02\x00\x00\x00\x03')
(1, 2, 3)
>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/docs/attachments/20100922/429bff13/attachment-0001.html>


More information about the docs mailing list