def test_it_reads_a_float64_to_buffer(self, space): w_float64_type = ffitype.FloatRWStrategy(ffitype.FLOAT64) data = lltype.malloc(rffi.CCHARP.TO, 8, flavor='raw') misc.write_raw_float_data(data, 1e-10, 8) w_res = w_float64_type.read(space, data) assert self.unwrap(space, w_res) == 1e-10 lltype.free(data, flavor='raw')
def convert_from_object(self, cdata, w_ob): space = self.space real, imag = space.unpackcomplex(w_ob) halfsize = self.size >> 1 cdata2 = rffi.ptradd(cdata, halfsize) misc.write_raw_float_data(cdata, real, halfsize) misc.write_raw_float_data(cdata2, imag, halfsize)
def write_raw_float_data(self, source): with self as ptr: misc.write_raw_float_data(ptr, source, self.ctype.size)
def convert_from_object(self, cdata, w_ob): space = self.space value = space.float_w(space.float(w_ob)) misc.write_raw_float_data(cdata, value, self.size)
def write_raw_complex_data(self, real, imag): with self as ptr: halfsize = self.ctype.size >> 1 ptr2 = rffi.ptradd(ptr, halfsize) misc.write_raw_float_data(ptr, real, halfsize) misc.write_raw_float_data(ptr2, imag, halfsize)
def write(self, space, data, w_arg): arg = space.float_w(w_arg) misc.write_raw_float_data(data, arg, self.typesize)
def write_raw_float_data(self, source): misc.write_raw_float_data(self._cdata, source, self.ctype.size) keepalive_until_here(self)
def rcall(self, funcaddr, args): assert self.cif_descr self = jit.promote(self) # no checking of len(args) needed, as calls in this context are not dynamic # The following code is functionally similar to W_CTypeFunc._call, but its # implementation is tailored to the restricted use (include memory handling) # of the CAPI calls. space = self.space cif_descr = self.cif_descr size = cif_descr.exchange_size raw_string1 = rffi.cast(rffi.CCHARP, 0) raw_string2 = rffi.cast(rffi.CCHARP, 0) # have max two in any CAPI buffer = lltype.malloc(rffi.CCHARP.TO, size, flavor='raw') try: for i in range(len(args)): data = rffi.ptradd(buffer, cif_descr.exchange_args[i]) obj = args[i] argtype = self.fargs[i] # the following is clumsy, but the data types used as arguments are # very limited, so it'll do for now if obj.tc == 'l': assert isinstance(argtype, ctypeprim.W_CTypePrimitiveSigned) misc.write_raw_signed_data(data, rffi.cast(rffi.LONG, obj._long), argtype.size) elif obj.tc == 'h': assert isinstance(argtype, ctypeprim.W_CTypePrimitiveUnsigned) misc.write_raw_unsigned_data( data, rffi.cast(rffi.UINTPTR_T, obj._handle), argtype.size) elif obj.tc == 'u': assert isinstance(argtype, ctypeprim.W_CTypePrimitiveUnsigned) misc.write_raw_unsigned_data( data, rffi.cast(rffi.SIZE_T, obj._index), argtype.size) elif obj.tc == 'p': assert obj._voidp != rffi.cast(rffi.VOIDP, 0) data = rffi.cast(rffi.VOIDPP, data) data[0] = obj._voidp elif obj.tc == 'd': assert isinstance(argtype, ctypeprim.W_CTypePrimitiveFloat) misc.write_raw_float_data( data, rffi.cast(rffi.DOUBLE, obj._double), argtype.size) else: # only other use is string assert obj.tc == 's' n = len(obj._string) data = rffi.cast(rffi.CCHARPP, data) if raw_string1 == rffi.cast(rffi.CCHARP, 0): # XXX could use rffi.get_nonmovingbuffer_final_null() raw_string1 = rffi.str2charp(obj._string) data[0] = raw_string1 else: assert raw_string2 == rffi.cast(rffi.CCHARP, 0) raw_string2 = rffi.str2charp(obj._string) data[0] = raw_string2 jit_libffi.jit_ffi_call(cif_descr, rffi.cast(rffi.VOIDP, funcaddr), buffer) resultdata = rffi.ptradd(buffer, cif_descr.exchange_result) # this wrapping is unnecessary, but the assumption is that given the # immediate unwrapping, the round-trip is removed w_res = self.ctitem.copy_and_convert_to_object(resultdata) finally: if raw_string1 != rffi.cast(rffi.CCHARP, 0): rffi.free_charp(raw_string1) if raw_string2 != rffi.cast(rffi.CCHARP, 0): rffi.free_charp(raw_string2) lltype.free(buffer, flavor='raw') return w_res