API ctypes. Field repr <Field type=c_long ..>, :
name = ((PyTypeObject *)self->proto)->tp_name;
self->proto c_long, Python 2.7 cfield.c, self->proto. :
name → type.- (yuck)
<Field type=X 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'>
source
share