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



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

我可以通过字段的 .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

这在 ctypes API 中似乎不受支持。创建 Field repr <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'>

只需使用_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'>

最新更新