首页 > 技术文章 > Python ctype sizeof incorrect!

raffeale 2016-06-29 19:02 原文

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_

 

 

class ctypes.Structure(*args, **kw)

Abstract base class for structures in native byte order.

Concrete structure and union types must be created by subclassing one of these types, and at least define a _fields_ class variable. ctypes will create descriptors which allow reading and writing the fields by direct attribute accesses. These are the

_fields_

A sequence defining the structure fields. The items must be 2-tuples or 3-tuples. The first item is the name of the field, the second item specifies the type of the field; it can be any ctypes data type.

For integer type fields like c_int, a third optional item can be given. It must be a small positive integer defining the bit width of the field.

Field names must be unique within one structure or union. This is not checked, only one field can be accessed when names are repeated.

It is possible to define the _fields_ class variable after the class statement that defines the Structure subclass, this allows creating data types that directly or indirectly reference themselves:

class List(Structure):
    pass
List._fields_ = [("pnext", POINTER(List)),
                 ...
                ]

The _fields_ class variable must, however, be defined before the type is first used (an instance is created, sizeof() is called on it, and so on). Later assignments to the _fields_ class variable will raise an AttributeError.

It is possible to defined sub-subclasses of structure types, they inherit the fields of the base class plus the _fields_ defined in the sub-subclass, if any.

_pack_

An optional small integer that allows overriding the alignment of structure fields in the instance. _pack_ must already be defined when _fields_ is assigned, otherwise it will have no effect.

推荐阅读