Python ctype sizeof incorrect!

Posted Raffeale

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python ctype sizeof incorrect!相关的知识,希望对你有一定的参考价值。

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) * 技术分享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) * 技术分享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.

以上是关于Python ctype sizeof incorrect!的主要内容,如果未能解决你的问题,请参考以下文章

Python ctypes:如何将 ctypes 数组传递给 DLL?

Python ctypes 模块

Python 外部函数调用库ctypes简介

Python:SWIG 与 ctypes

来自缓冲区的 ctypes - Python

python(ctypes)中的回调问题