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.

Title: ctypes Structure data size is incorrect
Type: behavior Stage:
Components: ctypes Versions: Python 2.7, Python 2.6
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Tomer.Levi, alex, amaury.forgeotdarc
Priority: normal Keywords:

Created on 2013-06-15 16:42 by Tomer.Levi, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg191213 - (view) Author: Tomer Levi (Tomer.Levi) Date: 2013-06-15 16:42
Whenever you create a ctypes.Structure with mixed ctypes, the size of all components is calculated by the size of largest one. This is especially irritating when trying to use Structure on bytearray.
The problem repeated for me in Python2.6 and 2.7 (both 32bit and 64bit versions) [My computer is 64bit]

#Creating a Structure that should take up 5 bytes
class Test(ctypes.Structure):
    _fields_ = [("test", ctypes.c_byte),
                ("test2", ctypes.c_uint32),]

#Initiating the Structure

--- OUTPUT ----

Traceback (most recent call last):

  File "<ipython-input-45-cd4b7501baee>", line 1, in <module>

ValueError: Buffer size too small (5 instead of at least 8 bytes)
msg191214 - (view) Author: Alex Gaynor (alex) * (Python committer) Date: 2013-06-15 16:45
This is how padding works in the C ABI, not a bug.
msg191340 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2013-06-17 14:30
You can also use "_pack_ = 1" to override the default alignment rules:
Date User Action Args
2022-04-11 14:57:46adminsetgithub: 62425
2013-06-17 14:30:39amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg191340
2013-06-15 16:45:39alexsetstatus: open -> closed

nosy: + alex
messages: + msg191214

resolution: not a bug
2013-06-15 16:42:24Tomer.Levicreate