6

我有一个具有不同数据类型字段的结构。我想遍历结构字段,检查数据类型,并使用适当的值设置字段。

我可以通过字段的 .size 和 .offset 属性访问字段的大小和偏移量。如何获取字段的“类型”属性?使用type(value)不会打印特定字段的 ctypes 数据类型。如果我打印值,那么我确实看到了 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
4

2 回答 2

6

ctypes API 似乎不支持此功能。创建Fieldrepr时<Field type=c_long ..>,会从嵌入类型中检索名称,如下所示:

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

对于您的字段,成员self->proto指向c_long,但我在 Python 2.7 中找不到cfield.c可以检索其self->proto自身值的位置。您可能被迫:

  1. name从->创建您自己的映射type
  2. (yuck) 解析 repr<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'>
于 2011-05-19T16:01:45.457 回答
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'>
于 2011-05-20T08:09:02.797 回答