How to get 'type' field descriptor from ctypes Structure or Union field

生来就可爱ヽ(ⅴ<●) 提交于 2020-04-06 03:07:09

问题


I have a structure with different datatype fields. I would like to iterate through the structure fields, check the datatype, and set the field with an appropriate value.

I have access to the size and offset of the field through the .size and .offset attribute of the field. How can I get the 'type' attribute of the field? Using type(value) does not print the ctypes datatype for the particular field. If I print value then I do see the ctypes datatype but there doesn't seem to be an attribute to access this directly.

How can I access the type field descriptor directly?

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'>

Ideally I would like to get the field type similar to the snippet below...

>>> A.one.type
c_long

回答1:


Just use the _fields_ list:

>>> 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'>



回答2:


This doesn't appear to be supported in the ctypes API. When the Field repr <Field type=c_long ..> is created, the name is retrieved from the embedded type like this:

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

For your field the member self->proto points to c_long, yet I find no place in Python 2.7's cfield.c where you can retrieve the value of self->proto itself. You may be forced either to:

  1. Create your own mapping from name -> type.
  2. (yuck) Parse the repr for <Field type=X and use getattr(ctypes, X) to fetch the type object.

Just to follow up with an example of option (1), here's a class decorator which creates the type mapping for you, adding a _typeof(cls, fld) class method:

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)

Result:

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


来源:https://stackoverflow.com/questions/6061225/how-to-get-type-field-descriptor-from-ctypes-structure-or-union-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!