def convert_bitfield_from_object(self, cdata, w_ob): ctype = self.ctype space = ctype.space # value = misc.as_long_long(space, w_ob) if isinstance(ctype, ctypeprim.W_CTypePrimitiveSigned): is_signed = True fmin = -(r_longlong(1) << (self.bitsize - 1)) fmax = (r_longlong(1) << (self.bitsize - 1)) - 1 if fmax == 0: fmax = 1 # special case to let "int x:1" receive "1" else: is_signed = False fmin = r_longlong(0) fmax = r_longlong((r_ulonglong(1) << self.bitsize) - 1) if value < fmin or value > fmax: raise oefmt(space.w_OverflowError, "value %d outside the range allowed by the bit field " "width: %d <= x <= %d", value, fmin, fmax) rawmask = ((r_ulonglong(1) << self.bitsize) - 1) << self.bitshift rawvalue = r_ulonglong(value) << self.bitshift rawfielddata = misc.read_raw_unsigned_data(cdata, ctype.size) rawfielddata = (rawfielddata & ~rawmask) | (rawvalue & rawmask) if is_signed: misc.write_raw_signed_data(cdata, rawfielddata, ctype.size) else: misc.write_raw_unsigned_data(cdata, rawfielddata, ctype.size)
def read(self, space, data): result = misc.read_raw_unsigned_data(data, self.typesize) result = rffi.cast(lltype.Signed, result) w_FFI = space.find_const(space.w_kernel, 'FFI') w_Pointer = space.find_const(w_FFI, 'Pointer') return space.send(w_Pointer, 'new', [space.newint(result)])
def convert_bitfield_from_object(self, cdata, w_ob): ctype = self.ctype space = ctype.space # value = misc.as_long_long(space, w_ob) if isinstance(ctype, ctypeprim.W_CTypePrimitiveSigned): is_signed = True fmin = -(r_longlong(1) << (self.bitsize - 1)) fmax = (r_longlong(1) << (self.bitsize - 1)) - 1 if fmax == 0: fmax = 1 # special case to let "int x:1" receive "1" else: is_signed = False fmin = r_longlong(0) fmax = r_longlong((r_ulonglong(1) << self.bitsize) - 1) if value < fmin or value > fmax: raise oefmt( space.w_OverflowError, "value %d outside the range allowed by the bit field " "width: %d <= x <= %d", value, fmin, fmax) rawmask = ((r_ulonglong(1) << self.bitsize) - 1) << self.bitshift rawvalue = r_ulonglong(value) << self.bitshift rawfielddata = misc.read_raw_unsigned_data(cdata, ctype.size) rawfielddata = (rawfielddata & ~rawmask) | (rawvalue & rawmask) if is_signed: misc.write_raw_signed_data(cdata, rawfielddata, ctype.size) else: misc.write_raw_unsigned_data(cdata, rawfielddata, ctype.size)
def test_it_writes_a_bool_to_buffer(self, space): w_bool_type = ffitype.BoolRWStrategy() size = w_bool_type.typesize data = lltype.malloc(rffi.CCHARP.TO, size, flavor='raw') w_true = space.execute("true") w_bool_type.write(space, data, w_true) raw_res = misc.read_raw_unsigned_data(data, size) assert bool(raw_res) lltype.free(data, flavor='raw')
def test_it_writes_a_pointer_to_buffer(self, space): w_pointer_type = ffitype.PointerRWStrategy() size = w_pointer_type.typesize data = lltype.malloc(rffi.CCHARP.TO, size, flavor='raw') w_ptr = space.execute("FFI::Pointer.new(15)") w_pointer_type.write(space, data, w_ptr) raw_res = misc.read_raw_unsigned_data(data, size) raw_res = rffi.cast(lltype.Unsigned, raw_res) assert raw_res == 15 lltype.free(data, flavor='raw')
def test_it_writes_a_string_to_buffer(self, space): w_string_type = ffitype.StringRWStrategy() size = w_string_type.typesize data = lltype.malloc(rffi.CCHARP.TO, size, flavor='raw') w_str = space.newstr_fromstr("test") w_string_type.write(space, data, w_str) raw_res = misc.read_raw_unsigned_data(data, size) raw_res = rffi.cast(rffi.CCHARP, raw_res) assert rffi.charp2str(raw_res) == "test" lltype.free(data, flavor='raw')
def convert_to_object(self, cdata): if self.value_fits_ulong: value = misc.read_raw_ulong_data(cdata, self.size) if self.value_fits_long: return self.space.wrap(intmask(value)) else: return self.space.wrap(value) # r_uint => 'long' object else: value = misc.read_raw_unsigned_data(cdata, self.size) return self.space.wrap(value) # r_ulonglong => 'long' object
def convert_bitfield_to_object(self, cdata): ctype = self.ctype space = ctype.space # if isinstance(ctype, ctypeprim.W_CTypePrimitiveSigned): if ctype.value_fits_long: value = r_uint(misc.read_raw_long_data(cdata, ctype.size)) valuemask = (r_uint(1) << self.bitsize) - 1 shiftforsign = r_uint(1) << (self.bitsize - 1) value = ((value >> self.bitshift) + shiftforsign) & valuemask result = intmask(value) - intmask(shiftforsign) return space.wrap(result) else: value = misc.read_raw_unsigned_data(cdata, ctype.size) valuemask = (r_ulonglong(1) << self.bitsize) - 1 shiftforsign = r_ulonglong(1) << (self.bitsize - 1) value = ((value >> self.bitshift) + shiftforsign) & valuemask result = r_longlong(value) - r_longlong(shiftforsign) return space.wrap(result) # if isinstance(ctype, ctypeprim.W_CTypePrimitiveUnsigned): value_fits_long = ctype.value_fits_long value_fits_ulong = ctype.value_fits_ulong elif isinstance(ctype, ctypeprim.W_CTypePrimitiveCharOrUniChar): value_fits_long = True value_fits_ulong = True else: raise NotImplementedError # if value_fits_ulong: value = misc.read_raw_ulong_data(cdata, ctype.size) valuemask = (r_uint(1) << self.bitsize) - 1 value = (value >> self.bitshift) & valuemask if value_fits_long: return space.wrap(intmask(value)) else: return space.wrap(value) # uint => wrapped long object else: value = misc.read_raw_unsigned_data(cdata, ctype.size) valuemask = (r_ulonglong(1) << self.bitsize) - 1 value = (value >> self.bitshift) & valuemask return space.wrap(value) # ulonglong => wrapped long object
def test_it_writes_unsigned_types_to_buffer(self, space): for t in [ ffitype.UINT8, ffitype.UINT16, ffitype.UINT32, ffitype.UINT64, ffitype.ULONG ]: w_unsigned_type = ffitype.UnsignedRWStrategy(t) size = w_unsigned_type.typesize # make new buffer for every t data = lltype.malloc(rffi.CCHARP.TO, size, flavor='raw') w_i = space.newint(16) w_unsigned_type.write(space, data, w_i) raw_res = misc.read_raw_unsigned_data(data, size) assert raw_res == 16 lltype.free(data, flavor='raw')
def test_it_writes_unsigned_types_to_buffer(self, space): for t in [ffitype.UINT8, ffitype.UINT16, ffitype.UINT32, ffitype.UINT64, ffitype.ULONG]: w_unsigned_type = ffitype.UnsignedRWStrategy(t) size = w_unsigned_type.typesize # make new buffer for every t data = lltype.malloc(rffi.CCHARP.TO, size, flavor='raw') w_i = space.newint(16) w_unsigned_type.write(space, data, w_i) raw_res = misc.read_raw_unsigned_data(data, size) assert raw_res == 16 lltype.free(data, flavor='raw')
def read(self, space, data): result = misc.read_raw_unsigned_data(data, self.typesize) return space.newint(intmask(result))
def _convert_to_object_longlong(self, cdata): # in its own function: LONGLONG may make the whole function jit-opaque value = misc.read_raw_unsigned_data(cdata, self.size) return self.space.wrap(value) # r_ulonglong => 'long' object
def read(self, space, data): result = misc.read_raw_unsigned_data(data, self.typesize) result = rffi.cast(rffi.CCHARP, result) result = rffi.charp2str(result) return space.newstr_fromstr(result)