有可能——
ctypes.Structure 提供的大部分魔法是由于它的字段是“描述符” - 即 Python's descriptor protocol 之后的对象 - 类似于我们在类主体中使用 @property 装饰器时得到的结果。
ctypes.Structure 有一个元类,负责将特殊大小写变量名称 _fields_ 中列出的每个字段转换为 _ctypes.CField 对象(您可以通过在交互式 Python 提示符中验证 type(mystryct.field) 的结果来检查它.
因此,为了扩展字段本身的行为,我们需要扩展这个CField 类 - 并修改创建您的结构的元类以使用我们的字段。 CField 类本身似乎是一个普通的 Python 类 - 所以如果我们尊重对超级方法的调用,它很容易修改。
然而在你的“愿望清单”中有一些收获:
使用“帮助”要求 Python 对象将帮助字符串嵌入其类 __doc__ 属性(而不是实例)中。因此,每次从结构类中检索字段本身时,我们都可以这样做,我们会在所需的帮助下动态地创建一个新类。
当从一个对象中检索一个值时,Python 不能提前“知道”这个值是仅仅被repr“查看”还是被实际使用。因此,我们要么为具有自定义表示的值自定义 a.fieldB 返回的值,要么根本不这样做。下面的代码确实创建了一个具有自定义表示的字段检索动态类,并尝试保留基础值的所有其他数字属性。但这设置得既慢又可能出现一些不兼容问题——你可以选择在不调试值时关闭它,或者只是获取原始值。
Ctype 的“字段”当然会有一些自己的内部结构,例如每个内存位置的偏移量等等 - 因此,我建议采用以下方法:(1) 创建一个新的“字段” " 完全不从 ctypes.Field 继承的类 - 并且实现了您想要的增强功能; (2) 在创建 ModifiedStructure 时,创建所有以“_”为前缀的名称,并将这些名称传递给原始 Ctypes.Structure 元类以像往常一样创建其字段; (3) 让我们的“Field”类对原来的 ctypes.Fields 进行读写,并有它们的自定义转换和表示。
如您所见,我还负责在写入时实际转换 Enum 值。
要尝试一切,只需从下面的“ModifiedStructure”继承,而不是ctypes.Structure:
from ctypes import Structure
import ctypes
class A(Structure):
_fields_ = [("a", ctypes.c_uint8)]
FieldType = type(A.a)
StructureType = type(A)
del A
def repr_wrapper(value, transform):
class ReprWrapper(type(value)):
def __new__(cls, value):
return super().__new__(cls, value)
def __repr__(self):
return transform(self)
return ReprWrapper(value)
def help_wrapper(field):
class Field2(field.__class__):
__doc__ = field.help
def __repr__(self):
return self.__doc__
return Field2(field.name, field.type_, help=field.help, repr=field.repr, enum=field.enum)
class Field:
def __init__(self, name, type_, **kwargs):
self.name = name
self.type_ = type_
self.real_name = "_" + name
self.help = kwargs.pop("brief", f"Proxy structure field {name}")
self.repr = kwargs.pop("repr", None)
self.enum = kwargs.pop("enum", None)
if self.enum:
self.rev_enum = {constant.value:constant for constant in self.enum.__members__.values() }
def __get__(self, instance, owner):
if not instance:
return help_wrapper(self)
value = getattr(instance, self.real_name)
if self.enum:
return self.rev_enum[value]
if self.repr:
return repr_wrapper(value, self.repr)
return value
def __set__(self, instance, value):
if self.enum:
value = getattr(self.enum, value.name).value
setattr(instance, self.real_name, value)
class ModifiedStructureMeta(StructureType):
def __new__(metacls, name, bases, namespace):
_fields = namespace.get("_fields_", "")
classic_fields = []
for field in _fields:
# Create the set of descriptors for the new-style fields:
name = field[0]
namespace[name] = Field(name, field[1], **(field[2] if len(field) > 2 else {}))
classic_fields.append(("_" + name, field[1]))
namespace["_fields_"] = classic_fields
return super().__new__(metacls, name, bases, namespace)
class ModifiedStructure(ctypes.Structure, metaclass=ModifiedStructureMeta):
__slots__ = ()
并在交互式提示下对其进行测试:
In [165]: class A(ModifiedStructure):
...: _fields_ = [("b", ctypes.c_uint8, {"enum": Color, 'brief': "a color", }), ("c", ctypes.c_uint8, {"repr": hex})]
...:
...:
In [166]: a = A()
In [167]: a.c = 20
In [169]: a.c
Out[169]: 0x14
In [170]: a.c = 256
In [171]: a.c
Out[171]: 0x0
In [172]: a.c = 255
In [173]: a.c
Out[173]: 0xff
In [177]: a.b = Color.RED
In [178]: a._b
Out[178]: 1
In [180]: help(A.b)
(shows full Field class help starting with the given description)
In [181]: A.b
Out[181]: a color