如何从ctypes结构或联合字段获取'type'字段描述符

6 投票
2 回答
5140 浏览
提问于 2025-04-16 17:58

我有一个结构体,里面有不同类型的字段。我想要遍历这些字段,检查它们的数据类型,然后给每个字段设置一个合适的值。

我可以通过字段的.size和.offset属性获取字段的大小和偏移量。那么,我该如何获取字段的'type'属性呢?使用type(value)并不能显示该字段的ctypes数据类型。如果我用print value,我能看到ctypes的数据类型,但似乎没有直接访问这个属性的方法。

我该如何直接访问字段类型描述符呢?

from ctypes import *

class A(Structure):
    _fields_ = [("one", c_long),
                ("two", c_char),
                ("three", c_byte)]

>>> A.one
<Field type=c_long, ofs=0, size=4>
>>> A.one.offset
0
>>> A.one.size
4
>>> type(A.one)
<class '_ctypes.CField'>

理想情况下,我想要获取字段类型,类似于下面的代码片段...

>>> A.one.type
c_long

2 个回答

6

在ctypes这个API中,似乎不支持这个功能。当创建Field的表示形式<Field type=c_long ..>时,名称是从嵌入的类型中获取的,具体是这样做的:

name = ((PyTypeObject *)self->proto)->tp_name;

对于你的字段,成员self->proto指向c_long,但我在Python 2.7的cfield.c中找不到任何地方可以直接获取self->proto的值。因此,你可能需要:

  1. 自己创建一个从nametype的映射。
  2. (这很麻烦)解析表示形式中的<Field type=X,然后使用getattr(ctypes, X)来获取类型对象。

为了给出选项(1)的一个例子,这里有一个类装饰器,它为你创建类型映射,并添加了一个_typeof(cls, fld)类方法:

from ctypes import *

def typemap(cls):
    _types = dict((getattr(cls, t), v) for t, v in cls._fields_)
    setattr(cls, '_typeof', classmethod(lambda c, f: _types.get(f)))
    return cls

@typemap
class A(Structure):
    _fields_ = [("one", c_long),
                ("two", c_char),
                ("three", c_byte)]

print A._typeof(A.one), A._typeof(A.two), A._typeof(A.three)

结果:

<class 'ctypes.c_long'> <class 'ctypes.c_char'> <class 'ctypes.c_byte'>
5

只需要使用 _fields_ 这个列表就可以了:

>>> for f,t in A._fields_:
...  a = getattr(A,f)
...  print a,a.offset,a.size,t
...
<Field type=c_long, ofs=0, size=4> 0 4 <class 'ctypes.c_long'>
<Field type=c_char, ofs=4, size=1> 4 1 <class 'ctypes.c_char'>
<Field type=c_byte, ofs=5, size=1> 5 1 <class 'ctypes.c_byte'>

撰写回答