homepage

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: ctypes Structure data size is incorrect
Type: behavior Stage:
Components: ctypes Versions: Python 2.7, Python 2.6
process
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]
Example:
#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
Test.from_buffer(bytearray(5))
--- OUTPUT ----
Traceback (most recent call last):
 File "<ipython-input-45-cd4b7501baee>", line 1, in <module>
 Test.from_buffer(bytearray(5))
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:
http://docs.python.org/3/library/ctypes.html#ctypes.Structure._pack_ 
History
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

AltStyle によって変換されたページ (->オリジナル) /