如何获取“类型”来自 ctypes 结构或联合字段的字段描述符

发布于 2024-11-08 19:18:49 字数 665 浏览 4 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我恋#小黄人 2024-11-15 19:18:49

ctypes API 似乎不支持此功能。创建 Field repr 时,将从嵌入类型中检索名称,如下所示:

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

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

  1. name 创建您自己的映射 -> 类型
  2. (恶心)解析 的 repr 并使用 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'>

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'>
最近可好 2024-11-15 19:18:49

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

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'>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文