This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: struct.unpack() expects wrong number of bytes
Type: behavior Stage: test needed
Components: Library (Lib) Versions: Python 3.1, Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, brian.curtin, codingrobot
Priority: normal Keywords:

Created on 2010-02-25 18:22 by codingrobot, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
struct_test.py codingrobot, 2010-02-25 18:49 simple test
Messages (4)
msg100096 - (view) Author: Nick (codingrobot) Date: 2010-02-25 18:22
the code I'm trying to execute (block is long enough):
unpack("2IB2I", block)

executing this raises an exception:
struct.error: unpack requires a bytes argument of length 20

Setting native byte-order with '@' causes the same error.
Specifying the correct byte-order either with '>' or '<', or
native with '=' helps and unpack expects the correct number (17) of bytes.
msg100097 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2010-02-25 18:26
Can you provide a complete test case or example?
msg100098 - (view) Author: Nick (codingrobot) Date: 2010-02-25 18:49
tested on macosx 10.6.2 64-bit and debian amd64 with both python2.6 and python3.1

the output is always the same:

b'\x01\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00'
20
(1, 1, 2, 1, 1)
(1, 1, 2, 16777216, 16777216)
(1, 1, 2, 16777216, 16777216)
------------------
b'\x01\x00\x00\x00\x01\x00\x00\x00\x02\x01\x00\x00\x00\x01\x00\x00\x00'
17
(1, 1, 2, 1, 1)
(1, 1, 2, 1, 1)
msg100101 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2010-02-25 20:21
It's not about byte ordering. '@' and '=' also change the alignment!

The documentation says it well:
- '@' (or no marker) uses the 'native' alignment, which (on every platform I know) aligns 4-bytes ints to multiple of 4 bytes.
- '=' uses the 'standard' alignment which is: "no alignment is required for any type"
History
Date User Action Args
2022-04-11 14:56:58adminsetgithub: 52267
2010-02-25 20:21:50amaury.forgeotdarcsetstatus: open -> closed

nosy: + amaury.forgeotdarc
messages: + msg100101

resolution: not a bug
2010-02-25 18:49:15codingrobotsetfiles: + struct_test.py

messages: + msg100098
2010-02-25 18:26:17brian.curtinsetnosy: + brian.curtin

messages: + msg100097
stage: test needed
2010-02-25 18:22:28codingrobotcreate