在 _fields_ 中混合 ctypes 和 python 类
Mixing ctypes and python classes in _fields_
我正在尝试为使用 gobject 的结构创建一个 ctypes 包装器。
结构定义如下:
struct GstMetaStruc
{
GstMeta meta;
GstStructure* structure;
};
GstMeta 有一个现有的内省包装器,可以让我访问基本元对象。
我目前的错误做法是这样的:
import ctypes
import gi
gi.require_version("Gst", "1.0")
from gi.repository import Gst
class TcamMeta(ctypes.Structure):
"""
"""
_fields_ = [("meta", Gst.Meta),
("structure", ctypes.POINTER(Gst.Structure))]
是否可以将 ctype 定义与现有 python 包装器 类 混合使用?
是否有更好的方法来为派生类型定义 python 类?
[Python 3.Docs]: ctypes - Structures and unions 状态(重点 是我的):
Structures and unions must derive from the Structure and Union base classes which are defined in the ctypes module. Each subclass must define a _fields_ attribute. _fields_ must be a list of 2-tuples, containing a field name and a field type.
The field type must be a ctypes type like c_int, or any other derived ctypes type: structure, union, array, pointer.
_fields_ 成员实现为 描述符 ,这意味着它们是 "special"(与常规 class 成员)。因此,在声明结构时会执行一些检查。
>>> import ctypes as ct
>>>
>>> class A: pass
...
>>> class Stru0(ct.Structure): pass
...
>>> class Stru1(ct.Structure): _fields_ = [("c", Stru0)]
...
>>> class Stru1(ct.Structure): _fields_ = [("c", ct.c_float)]
...
>>> class Stru1(ct.Structure): _fields_ = [("c", int)]
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: second item in _fields_ tuple (index 0) must be a C type
>>> class Stru1(ct.Structure): _fields_ = [("c", A)]
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: second item in _fields_ tuple (index 0) must be a C type
因此,如果您没有收到 TypeError,您可能 OK。但是简单地看一下 PyGObject 例子,你不应该处于需要这个的情况。
我正在尝试为使用 gobject 的结构创建一个 ctypes 包装器。
结构定义如下:
struct GstMetaStruc
{
GstMeta meta;
GstStructure* structure;
};
GstMeta 有一个现有的内省包装器,可以让我访问基本元对象。
我目前的错误做法是这样的:
import ctypes
import gi
gi.require_version("Gst", "1.0")
from gi.repository import Gst
class TcamMeta(ctypes.Structure):
"""
"""
_fields_ = [("meta", Gst.Meta),
("structure", ctypes.POINTER(Gst.Structure))]
是否可以将 ctype 定义与现有 python 包装器 类 混合使用?
是否有更好的方法来为派生类型定义 python 类?
[Python 3.Docs]: ctypes - Structures and unions 状态(重点 是我的):
Structures and unions must derive from the Structure and Union base classes which are defined in the ctypes module. Each subclass must define a _fields_ attribute. _fields_ must be a list of 2-tuples, containing a field name and a field type.
The field type must be a ctypes type like c_int, or any other derived ctypes type: structure, union, array, pointer.
_fields_ 成员实现为 描述符 ,这意味着它们是 "special"(与常规 class 成员)。因此,在声明结构时会执行一些检查。
>>> import ctypes as ct >>> >>> class A: pass ... >>> class Stru0(ct.Structure): pass ... >>> class Stru1(ct.Structure): _fields_ = [("c", Stru0)] ... >>> class Stru1(ct.Structure): _fields_ = [("c", ct.c_float)] ... >>> class Stru1(ct.Structure): _fields_ = [("c", int)] ... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: second item in _fields_ tuple (index 0) must be a C type >>> class Stru1(ct.Structure): _fields_ = [("c", A)] ... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: second item in _fields_ tuple (index 0) must be a C type
因此,如果您没有收到 TypeError,您可能 OK。但是简单地看一下 PyGObject 例子,你不应该处于需要这个的情况。