这不起作用:
class ifinfomsg(ctypes.Structure):
_fields_ = [
('ifi_family', ctypes.c_ubyte),
('__ifi_pad', ctypes.c_ubyte),
('ifi_type', ctypes.c_ushort),
('ifi_index', ctypes.c_int),
('ifi_flags', ctypes.c_uint),
('ifi_change', ctypes.c_uint(0xFFFFFFFF))
]
Run Code Online (Sandbox Code Playgroud)
它的错误是:
File "rtnetlink.py", line 243, in <module>
class ifinfomsg(ctypes.Structure):
TypeError: Error when calling the metaclass bases
second item in _fields_ tuple (index 5) must be a C type
Run Code Online (Sandbox Code Playgroud)
但是我可以设置以下值__init__():
class ifinfomsg(ctypes.Structure):
_fields_ = [
('ifi_family', ctypes.c_ubyte),
('__ifi_pad', ctypes.c_ubyte),
('ifi_type', ctypes.c_ushort),
('ifi_index', ctypes.c_int),
('ifi_flags', ctypes.c_uint),
('ifi_change', ctypes.c_uint)
]
def __init__(self):
self.__ifi_pad = 0
self.ifi_change = 0xFFFFFFFF
Run Code Online (Sandbox Code Playgroud)
这是通过 执行此操作的“正确”方法吗__init__?
您的示例错误是因为ctypes.c_uint(0xFFFFFFFF)不是类型。在类定义中,一切都必须是类型(指针不是POINTER)pointer。
我觉得__init__你用的方法就好了。我已经包装了一些非常大的结构,并且通常__init__像您设置默认值一样使用。
一般来说,如果您可以控制要包装的 C 库,我认为您不应该在两个地方(C 和 Python)都有默认设置。在我包装的一个大型库中,我可以访问 CI 创建的 C 函数“set_defaults”,我从__init__. 这样,只有 C 用户可以访问默认值,并且只需维护一组代码。
| 归档时间: |
|
| 查看次数: |
2404 次 |
| 最近记录: |