def __init__(self, basicffitype, size): # A W_Array represent the C type '*T', which can also represent # the type of pointers to arrays of T. So the following fields # are used to describe T only. It is 'basicffitype' possibly # repeated until reaching the length 'size'. self.basicffitype = basicffitype self.size = size self.alignment = size_alignment(basicffitype)[1]
def get_basic_ffi_type(self): if not self.ffi_struct: # Repeated fields are delicate. Consider for example # struct { int a[5]; } # or struct { struct {int x;} a[5]; } # Seeing no corresponding doc in clibffi, let's just repeat # the field 5 times... fieldtypes = [] for name, tp, bitsize in self.fields: basic_ffi_type = tp.get_basic_ffi_type() basic_size, _ = size_alignment(basic_ffi_type) total_size = tp.size count = 0 while count + basic_size <= total_size: fieldtypes.append(basic_ffi_type) count += basic_size self.ffi_struct = clibffi.make_struct_ffitype_e(self.size, self.alignment, fieldtypes) return self.ffi_struct.ffistruct
def get_basic_ffi_type(self): if not self.ffi_struct: # Repeated fields are delicate. Consider for example # struct { int a[5]; } # or struct { struct {int x;} a[5]; } # Seeing no corresponding doc in clibffi, let's just repeat # the field 5 times... fieldtypes = [] for name, tp, bitsize in self.fields: basic_ffi_type = tp.get_basic_ffi_type() basic_size, _ = size_alignment(basic_ffi_type) total_size = tp.size count = 0 while count + basic_size <= total_size: fieldtypes.append(basic_ffi_type) count += basic_size if basic_size == 0: # corner case. get out of this infinite break # loop after 1 iteration ("why not") self.ffi_struct = clibffi.make_struct_ffitype_e( self.size, self.alignment, fieldtypes) return self.ffi_struct.ffistruct
self.itemcode, w_item) return space.wrap(result) def descr_repr(self, space): return space.wrap("<_rawffi.Array '%s' (%d, %d)>" % (self.itemcode, self.size, self.alignment)) @unwrap_spec(address=r_uint, length=int) def fromaddress(self, space, address, length): return space.wrap(W_ArrayInstance(space, self, length, address)) PRIMITIVE_ARRAY_TYPES = {} for _code in TYPEMAP: PRIMITIVE_ARRAY_TYPES[_code] = W_Array(TYPEMAP[_code], size_alignment(TYPEMAP[_code])[0]) PRIMITIVE_ARRAY_TYPES[_code].itemcode = _code ARRAY_OF_PTRS = PRIMITIVE_ARRAY_TYPES['P'] def descr_new_array(space, w_type, w_shape): return unpack_shape_with_length(space, w_shape) W_Array.typedef = TypeDef('Array', __new__=interp2app(descr_new_array), __call__=interp2app(W_Array.descr_call), __repr__=interp2app(W_Array.descr_repr), fromaddress=interp2app(W_Array.fromaddress), size_alignment=interp2app( W_Array.descr_size_alignment))
self.itemcode, w_item) return space.wrap(result) def descr_repr(self, space): return space.wrap("<_rawffi.Array '%s' (%d, %d)>" % (self.itemcode, self.size, self.alignment)) @unwrap_spec(address=r_uint, length=int) def fromaddress(self, space, address, length): return space.wrap(W_ArrayInstance(space, self, length, address)) PRIMITIVE_ARRAY_TYPES = {} for _code in TYPEMAP: PRIMITIVE_ARRAY_TYPES[_code] = W_Array(TYPEMAP[_code], size_alignment(TYPEMAP[_code])[0]) PRIMITIVE_ARRAY_TYPES[_code].itemcode = _code ARRAY_OF_PTRS = PRIMITIVE_ARRAY_TYPES['P'] def descr_new_array(space, w_type, w_shape): return unpack_shape_with_length(space, w_shape) W_Array.typedef = TypeDef( 'Array', __new__ = interp2app(descr_new_array), __call__ = interp2app(W_Array.descr_call), __repr__ = interp2app(W_Array.descr_repr), fromaddress = interp2app(W_Array.fromaddress), size_alignment = interp2app(W_Array.descr_size_alignment) ) W_Array.typedef.acceptable_as_base_class = False