python - Setting ctypes.Structure default values -
this doesn't work:
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)) ] it errors with:
file "rtnetlink.py", line 243, in <module> class ifinfomsg(ctypes.structure): typeerror: error when calling metaclass bases second item in _fields_ tuple (index 5) must c type however can set values in __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 is 'correct' way this, via __init__?
your example errors because ctypes.c_uint(0xffffffff) not type. in class definitions has type (pointers pointer not pointer).
i think __init__ method use if fine. have wrapped large structures , typically use __init__ have done set default values.
in general if have control of c library wrapping think should not have default setting in both places (c , python). in 1 large library wrapped have access c created c functions "set_defaults" call __init__. way c users have access defaults , there 1 set of code maintain.
Comments
Post a Comment