如何从ctypes Structure或Union字段获取'type'字段描述符

Iña*_*qui 5 python ctypes types descriptor

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

我可以通过字段的.size和.offset属性访问字段的大小和偏移量.如何获得该字段的"type"属性?使用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'>
Run Code Online (Sandbox Code Playgroud)

理想情况下,我想获得类似下面的代码段的字段类型...

>>> A.one.type
c_long
Run Code Online (Sandbox Code Playgroud)

sam*_*ias 6

ctypes API似乎不支持此功能.当Field再版<Field type=c_long ..>被创建,名字是从这样的埋设式检索到:

name = ((PyTypeObject *)self->proto)->tp_name;
Run Code Online (Sandbox Code Playgroud)

对于你的领域,成员self->proto指向c_long,但我在Python 2.7中找不到cfield.c可以检索其self->proto自身价值的地方.您可能会被迫:

  1. name- > 创建自己的映射type.
  2. (yuck)解析repr for <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)
Run Code Online (Sandbox Code Playgroud)

结果:

<class 'ctypes.c_long'> <class 'ctypes.c_char'> <class 'ctypes.c_byte'>
Run Code Online (Sandbox Code Playgroud)


Mar*_*nen 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'>
Run Code Online (Sandbox Code Playgroud)