zoukankan      html  css  js  c++  java
  • 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) * (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.

  • 相关阅读:
    2017-2018-1 20155232 嵌入式C语言——时钟
    实践——ELF文件格式分析
    实践——程序破解
    实践——模块
    《Linux内核分析》期终总结
    《Linux内核分析》第八周:进程的切换和系统的一般执行过程
    《Linux内核设计与实现》读书笔记六
    《深入理解计算机系统》读书笔记五
    《Linux内核分析》第七周: 可执行程序的装载
    《Linux内核设计与实现》读书笔记四
  • 原文地址:https://www.cnblogs.com/raffeale/p/5627966.html
Copyright © 2011-2022 走看看