def getkind(TYPE, supports_floats=True, supports_longlong=True, supports_singlefloats=True): if TYPE is lltype.Void: return "void" elif isinstance(TYPE, lltype.Primitive): if TYPE is lltype.Float and supports_floats: return 'float' if TYPE is lltype.SingleFloat and supports_singlefloats: return 'int' # singlefloats are stored in an int if TYPE in (lltype.Float, lltype.SingleFloat): raise NotImplementedError("type %s not supported" % TYPE) # XXX fix this for oo... if (TYPE != llmemory.Address and rffi.sizeof(TYPE) > rffi.sizeof(lltype.Signed)): if supports_longlong: assert rffi.sizeof(TYPE) == 8 return 'float' raise NotImplementedError("type %s is too large" % TYPE) return "int" elif isinstance(TYPE, lltype.Ptr): if TYPE.TO._gckind == 'raw': return "int" else: return "ref" elif isinstance(TYPE, ootype.OOType): return "ref" else: raise NotImplementedError("type %s not supported" % TYPE)
def test_get_call_descr_not_translated(): c0 = GcCache(False) descr1 = get_call_descr(c0, [lltype.Char, lltype.Signed], lltype.Char) assert descr1.get_result_size() == rffi.sizeof(lltype.Char) assert descr1.get_result_type() == history.INT assert descr1.arg_classes == "ii" # T = lltype.GcStruct('T') descr2 = get_call_descr(c0, [lltype.Ptr(T)], lltype.Ptr(T)) assert descr2.get_result_size() == rffi.sizeof(lltype.Ptr(T)) assert descr2.get_result_type() == history.REF assert descr2.arg_classes == "r" # U = lltype.GcStruct('U', ('x', lltype.Signed)) assert descr2 == get_call_descr(c0, [lltype.Ptr(U)], lltype.Ptr(U)) # V = lltype.Struct('V', ('x', lltype.Signed)) assert (get_call_descr(c0, [], lltype.Ptr(V)).get_result_type() == history.INT) # assert (get_call_descr(c0, [], lltype.Void).get_result_type() == history.VOID) # descr4 = get_call_descr(c0, [lltype.Float, lltype.Float], lltype.Float) assert descr4.get_result_size() == rffi.sizeof(lltype.Float) assert descr4.get_result_type() == history.FLOAT assert descr4.arg_classes == "ff" # descr5 = get_call_descr(c0, [lltype.SingleFloat], lltype.SingleFloat) assert descr5.get_result_size() == rffi.sizeof(lltype.SingleFloat) assert descr5.get_result_type() == "S" assert descr5.arg_classes == "S"
def push_arg_as_ffiptr(ffitp, arg, ll_buf): # This is for primitive types. Note that the exact type of 'arg' may be # different from the expected 'c_size'. To cope with that, we fall back # to a byte-by-byte copy. TP = lltype.typeOf(arg) TP_P = lltype.Ptr(rffi.CArray(TP)) TP_size = rffi.sizeof(TP) c_size = intmask(ffitp.c_size) # if both types have the same size, we can directly write the # value to the buffer if c_size == TP_size: buf = rffi.cast(TP_P, ll_buf) buf[0] = arg else: # needs byte-by-byte copying. Make sure 'arg' is an integer type. # Note that this won't work for rffi.FLOAT/rffi.DOUBLE. assert TP is not rffi.FLOAT and TP is not rffi.DOUBLE if TP_size <= rffi.sizeof(lltype.Signed): arg = rffi.cast(lltype.Unsigned, arg) else: arg = rffi.cast(lltype.UnsignedLongLong, arg) if _LITTLE_ENDIAN: for i in range(c_size): ll_buf[i] = chr(arg & 0xFF) arg >>= 8 elif _BIG_ENDIAN: for i in range(c_size-1, -1, -1): ll_buf[i] = chr(arg & 0xFF) arg >>= 8 else: raise AssertionError
def getDescrClass(TYPE, BaseDescr, GcPtrDescr, NonGcPtrDescr, nameprefix, methodname, floatattrname, signedattrname, _cache={}): if isinstance(TYPE, lltype.Ptr): if TYPE.TO._gckind == 'gc': return GcPtrDescr else: return NonGcPtrDescr if TYPE is lltype.SingleFloat: assert rffi.sizeof(rffi.UINT) == rffi.sizeof(TYPE) TYPE = rffi.UINT try: return _cache[nameprefix, TYPE] except KeyError: # class Descr(BaseDescr): _clsname = '%s%sDescr' % (TYPE._name, nameprefix) Descr.__name__ = Descr._clsname # def method(self, translate_support_code): return symbolic.get_size(TYPE, translate_support_code) setattr(Descr, methodname, method) # if TYPE is lltype.Float or is_longlong(TYPE): setattr(Descr, floatattrname, True) elif TYPE is not lltype.Bool and rffi.cast(TYPE, -1) == -1: setattr(Descr, signedattrname, True) # _cache[nameprefix, TYPE] = Descr return Descr
def byteswap(arg): """ Convert little->big endian and the opposite """ from pypy.rpython.lltypesystem import lltype, rffi T = lltype.typeOf(arg) # XXX we cannot do arithmetics on small ints if isinstance(arg, base_int): arg = widen(arg) if rffi.sizeof(T) == 1: res = arg elif rffi.sizeof(T) == 2: a, b = arg & 0xFF, arg & 0xFF00 res = (a << 8) | (b >> 8) elif rffi.sizeof(T) == 4: FF = r_uint(0xFF) arg = r_uint(arg) a, b, c, d = (arg & FF, arg & (FF << 8), arg & (FF << 16), arg & (FF << 24)) res = (a << 24) | (b << 8) | (c >> 8) | (d >> 24) elif rffi.sizeof(T) == 8: FF = r_ulonglong(0xFF) arg = r_ulonglong(arg) a, b, c, d = (arg & FF, arg & (FF << 8), arg & (FF << 16), arg & (FF << 24)) e, f, g, h = (arg & (FF << 32), arg & (FF << 40), arg & (FF << 48), arg & (FF << 56)) res = ((a << 56) | (b << 40) | (c << 24) | (d << 8) | (e >> 8) | (f >> 24) | (g >> 40) | (h >> 56)) else: assert False # unreachable code return rffi.cast(T, res)
def test_closure_heap(self): ch = ClosureHeap() assert not ch.free_list a = ch.alloc() assert ch.free_list b = ch.alloc() chunks = [a, b] p = ch.free_list while p: chunks.append(p) p = rffi.cast(rffi.VOIDPP, p)[0] closure_size = rffi.sizeof(FFI_CLOSUREP.TO) assert len(chunks) == CHUNK // closure_size for i in range(len(chunks) - 1): s = rffi.cast(rffi.UINT, chunks[i + 1]) e = rffi.cast(rffi.UINT, chunks[i]) assert (e - s) >= rffi.sizeof(FFI_CLOSUREP.TO) ch.free(a) assert ch.free_list == rffi.cast(rffi.VOIDP, a) snd = rffi.cast(rffi.VOIDPP, a)[0] assert snd == chunks[2] ch.free(b) assert ch.free_list == rffi.cast(rffi.VOIDP, b) snd = rffi.cast(rffi.VOIDPP, b)[0] assert snd == rffi.cast(rffi.VOIDP, a)
def test_get_call_descr_not_translated(): c0 = GcCache(False) descr1 = get_call_descr(c0, [lltype.Char, lltype.Signed], lltype.Char) assert descr1.get_result_size(False) == rffi.sizeof(lltype.Char) assert descr1.get_return_type() == history.INT assert descr1.arg_classes == "ii" # T = lltype.GcStruct('T') descr2 = get_call_descr(c0, [lltype.Ptr(T)], lltype.Ptr(T)) assert descr2.get_result_size(False) == rffi.sizeof(lltype.Ptr(T)) assert descr2.get_return_type() == history.REF assert descr2.arg_classes == "r" # U = lltype.GcStruct('U', ('x', lltype.Signed)) assert descr2 == get_call_descr(c0, [lltype.Ptr(U)], lltype.Ptr(U)) # V = lltype.Struct('V', ('x', lltype.Signed)) assert (get_call_descr(c0, [], lltype.Ptr(V)).get_return_type() == history.INT) # assert (get_call_descr(c0, [], lltype.Void).get_return_type() == history.VOID) # descr4 = get_call_descr(c0, [lltype.Float, lltype.Float], lltype.Float) assert descr4.get_result_size(False) == rffi.sizeof(lltype.Float) assert descr4.get_return_type() == history.FLOAT assert descr4.arg_classes == "ff" # descr5 = get_call_descr(c0, [lltype.SingleFloat], lltype.SingleFloat) assert descr5.get_result_size(False) == rffi.sizeof(lltype.SingleFloat) assert descr5.get_return_type() == "S" assert descr5.arg_classes == "S"
def getCallDescrClass(RESULT): if RESULT is lltype.Void: return VoidCallDescr if RESULT is lltype.Float: return FloatCallDescr if RESULT is lltype.SingleFloat: global _SingleFloatCallDescr if _SingleFloatCallDescr is None: assert rffi.sizeof(rffi.UINT) == rffi.sizeof(RESULT) class SingleFloatCallDescr(getCallDescrClass(rffi.UINT)): _clsname = "SingleFloatCallDescr" _return_type = "S" _SingleFloatCallDescr = SingleFloatCallDescr return _SingleFloatCallDescr if is_longlong(RESULT): return LongLongCallDescr return getDescrClass( RESULT, BaseIntCallDescr, GcPtrCallDescr, NonGcPtrCallDescr, "Call", "get_result_size", Ellipsis, # <= floatattrname should not be used here "_is_result_signed", )
def _get_bind_info(self, space, numElements): # avoid bus errors on 64bit platforms numElements = numElements + (rffi.sizeof(roci.dvoidp) - numElements % rffi.sizeof(roci.dvoidp)) # initialize the buffers bindNames = lltype.malloc(roci.Ptr(roci.oratext).TO, numElements, flavor='raw') bindNameLengths = lltype.malloc(roci.Ptr(roci.ub1).TO, numElements, flavor='raw') indicatorNames = lltype.malloc(roci.Ptr(roci.oratext).TO, numElements, flavor='raw') indicatorNameLengths = lltype.malloc(roci.Ptr(roci.ub1).TO, numElements, flavor='raw') duplicate = lltype.malloc(roci.Ptr(roci.ub1).TO, numElements, flavor='raw') bindHandles = lltype.malloc(roci.Ptr(roci.OCIBind).TO, numElements, flavor='raw') foundElementsPtr = lltype.malloc(roci.Ptr(roci.sb4).TO, 1, flavor='raw') try: status = roci.OCIStmtGetBindInfo( self.handle, self.environment.errorHandle, numElements, 1, foundElementsPtr, bindNames, bindNameLengths, indicatorNames, indicatorNameLengths, duplicate, bindHandles) if status != roci.OCI_NO_DATA: self.environment.checkForError(status, "Cursor_GetBindNames()") # Too few elements allocated foundElements = rffi.cast(lltype.Signed, foundElementsPtr[0]) if foundElements < 0: return -foundElements, None names_w = [] # process the bind information returned for i in range(foundElements): if rffi.cast(lltype.Signed, duplicate[i]): continue names_w.append( w_string(space, bindNames[i], rffi.cast(lltype.Signed, bindNameLengths[i]))) return 0, names_w finally: lltype.free(bindNames, flavor='raw') lltype.free(bindNameLengths, flavor='raw') lltype.free(indicatorNames, flavor='raw') lltype.free(indicatorNameLengths, flavor='raw') lltype.free(duplicate, flavor='raw') lltype.free(bindHandles, flavor='raw') lltype.free(foundElementsPtr, flavor='raw')
def _get_bind_info(self, space, numElements): # avoid bus errors on 64bit platforms numElements = numElements + (rffi.sizeof(roci.dvoidp) - numElements % rffi.sizeof(roci.dvoidp)) # initialize the buffers bindNames = lltype.malloc(roci.Ptr(roci.oratext).TO, numElements, flavor='raw') bindNameLengths = lltype.malloc(roci.Ptr(roci.ub1).TO, numElements, flavor='raw') indicatorNames = lltype.malloc(roci.Ptr(roci.oratext).TO, numElements, flavor='raw') indicatorNameLengths = lltype.malloc(roci.Ptr(roci.ub1).TO, numElements, flavor='raw') duplicate = lltype.malloc(roci.Ptr(roci.ub1).TO, numElements, flavor='raw') bindHandles = lltype.malloc(roci.Ptr(roci.OCIBind).TO, numElements, flavor='raw') foundElementsPtr = lltype.malloc(roci.Ptr(roci.sb4).TO, 1, flavor='raw') try: status = roci.OCIStmtGetBindInfo( self.handle, self.environment.errorHandle, numElements, 1, foundElementsPtr, bindNames, bindNameLengths, indicatorNames, indicatorNameLengths, duplicate, bindHandles) if status != roci.OCI_NO_DATA: self.environment.checkForError( status, "Cursor_GetBindNames()") # Too few elements allocated foundElements = rffi.cast(lltype.Signed, foundElementsPtr[0]) if foundElements < 0: return -foundElements, None names_w = [] # process the bind information returned for i in range(foundElements): if rffi.cast(lltype.Signed, duplicate[i]): continue names_w.append( w_string(space, bindNames[i], rffi.cast(lltype.Signed, bindNameLengths[i]))) return 0, names_w finally: lltype.free(bindNames, flavor='raw') lltype.free(bindNameLengths, flavor='raw') lltype.free(indicatorNames, flavor='raw') lltype.free(indicatorNameLengths, flavor='raw') lltype.free(duplicate, flavor='raw') lltype.free(bindHandles, flavor='raw') lltype.free(foundElementsPtr, flavor='raw')
def test_get_field_descr(): U = lltype.Struct('U') T = lltype.GcStruct('T') S = lltype.GcStruct('S', ('x', lltype.Char), ('y', lltype.Ptr(T)), ('z', lltype.Ptr(U)), ('f', lltype.Float)) assert getFieldDescrClass(lltype.Ptr(T)) is GcPtrFieldDescr assert getFieldDescrClass(lltype.Ptr(U)) is NonGcPtrFieldDescr cls = getFieldDescrClass(lltype.Char) assert cls != getFieldDescrClass(lltype.Signed) assert cls == getFieldDescrClass(lltype.Char) clsf = getFieldDescrClass(lltype.Float) assert clsf != cls assert clsf == getFieldDescrClass(lltype.Float) # c0 = GcCache(False) c1 = GcCache(True) assert get_field_descr(c0, S, 'y') == get_field_descr(c0, S, 'y') assert get_field_descr(c0, S, 'y') != get_field_descr(c1, S, 'y') for tsc in [False, True]: c2 = GcCache(tsc) descr_x = get_field_descr(c2, S, 'x') descr_y = get_field_descr(c2, S, 'y') descr_z = get_field_descr(c2, S, 'z') descr_f = get_field_descr(c2, S, 'f') assert descr_x.__class__ is cls assert descr_y.__class__ is GcPtrFieldDescr assert descr_z.__class__ is NonGcPtrFieldDescr assert descr_f.__class__ is clsf assert descr_x.name == 'S.x' assert descr_y.name == 'S.y' assert descr_z.name == 'S.z' assert descr_f.name == 'S.f' if not tsc: assert descr_x.offset < descr_y.offset < descr_z.offset assert descr_x.sort_key() < descr_y.sort_key() < descr_z.sort_key() assert descr_x.get_field_size(False) == rffi.sizeof(lltype.Char) assert descr_y.get_field_size(False) == rffi.sizeof(lltype.Ptr(T)) assert descr_z.get_field_size(False) == rffi.sizeof(lltype.Ptr(U)) assert descr_f.get_field_size(False) == rffi.sizeof(lltype.Float) else: assert isinstance(descr_x.offset, Symbolic) assert isinstance(descr_y.offset, Symbolic) assert isinstance(descr_z.offset, Symbolic) assert isinstance(descr_f.offset, Symbolic) assert isinstance(descr_x.get_field_size(True), Symbolic) assert isinstance(descr_y.get_field_size(True), Symbolic) assert isinstance(descr_z.get_field_size(True), Symbolic) assert isinstance(descr_f.get_field_size(True), Symbolic) assert not descr_x.is_pointer_field() assert descr_y.is_pointer_field() assert not descr_z.is_pointer_field() assert not descr_f.is_pointer_field() assert not descr_x.is_float_field() assert not descr_y.is_float_field() assert not descr_z.is_float_field() assert descr_f.is_float_field()
def _fits_into_signed(TYPE): if isinstance(TYPE, lltype.Ptr): return True # pointers always fits into Signeds if not isinstance(TYPE, lltype.Primitive): return False if TYPE is lltype.Void or TYPE is rffi.FLOAT or TYPE is rffi.DOUBLE: return False sz = rffi.sizeof(TYPE) return sz <= rffi.sizeof(rffi.SIGNED)
def f(points, result_point, n): i = 0 while i < n: myjitdriver.jit_merge_point(i=i, points=points, n=n, result_point=result_point) x = array_getitem(types.slong, rffi.sizeof(lltype.Signed) * 2, points, i, 0) y = array_getitem(types.slong, rffi.sizeof(lltype.Signed) * 2, points, i, rffi.sizeof(lltype.Signed)) cur_x = array_getitem(types.slong, rffi.sizeof(lltype.Signed) * 2, result_point, 0, 0) cur_y = array_getitem(types.slong, rffi.sizeof(lltype.Signed) * 2, result_point, 0, rffi.sizeof(lltype.Signed)) array_setitem(types.slong, rffi.sizeof(lltype.Signed) * 2, result_point, 0, 0, cur_x + x) array_setitem(types.slong, rffi.sizeof(lltype.Signed) * 2, result_point, 0, rffi.sizeof(lltype.Signed), cur_y + y) i += 1
def f(points, result_point, n): i = 0 while i < n: myjitdriver.jit_merge_point(i=i, points=points, n=n, result_point=result_point) x = array_getitem( types.slong, rffi.sizeof(lltype.Signed) * 2, points, i, 0 ) y = array_getitem( types.slong, rffi.sizeof(lltype.Signed) * 2, points, i, rffi.sizeof(lltype.Signed) ) cur_x = array_getitem( types.slong, rffi.sizeof(lltype.Signed) * 2, result_point, 0, 0 ) cur_y = array_getitem( types.slong, rffi.sizeof(lltype.Signed) * 2, result_point, 0, rffi.sizeof(lltype.Signed) ) array_setitem( types.slong, rffi.sizeof(lltype.Signed) * 2, result_point, 0, 0, cur_x + x ) array_setitem( types.slong, rffi.sizeof(lltype.Signed) * 2, result_point, 0, rffi.sizeof(lltype.Signed), cur_y + y ) i += 1
def inet_ntoa(packed): "packet 32-bits string -> IPv4 dotted string" if len(packed) != sizeof(_c.in_addr): raise RSocketError("packed IP wrong length for inet_ntoa") buf = rffi.make(_c.in_addr) try: for i in range(sizeof(_c.in_addr)): rffi.cast(rffi.CCHARP, buf)[i] = packed[i] return rffi.charp2str(_c.inet_ntoa(buf)) finally: lltype.free(buf, flavor='raw')
def test_get_field_descr(): U = lltype.Struct("U") T = lltype.GcStruct("T") S = lltype.GcStruct( "S", ("x", lltype.Char), ("y", lltype.Ptr(T)), ("z", lltype.Ptr(U)), ("f", lltype.Float), ("s", lltype.SingleFloat), ) # c0 = GcCache(False) c1 = GcCache(True) assert get_field_descr(c0, S, "y") == get_field_descr(c0, S, "y") assert get_field_descr(c0, S, "y") != get_field_descr(c1, S, "y") for tsc in [False, True]: c2 = GcCache(tsc) descr_x = get_field_descr(c2, S, "x") descr_y = get_field_descr(c2, S, "y") descr_z = get_field_descr(c2, S, "z") descr_f = get_field_descr(c2, S, "f") descr_s = get_field_descr(c2, S, "s") assert isinstance(descr_x, FieldDescr) assert descr_x.name == "S.x" assert descr_y.name == "S.y" assert descr_z.name == "S.z" assert descr_f.name == "S.f" assert descr_s.name == "S.s" if not tsc: assert descr_x.offset < descr_y.offset < descr_z.offset assert descr_x.sort_key() < descr_y.sort_key() < descr_z.sort_key() assert descr_x.field_size == rffi.sizeof(lltype.Char) assert descr_y.field_size == rffi.sizeof(lltype.Ptr(T)) assert descr_z.field_size == rffi.sizeof(lltype.Ptr(U)) assert descr_f.field_size == rffi.sizeof(lltype.Float) assert descr_s.field_size == rffi.sizeof(lltype.SingleFloat) else: assert isinstance(descr_x.offset, Symbolic) assert isinstance(descr_y.offset, Symbolic) assert isinstance(descr_z.offset, Symbolic) assert isinstance(descr_f.offset, Symbolic) assert isinstance(descr_s.offset, Symbolic) assert isinstance(descr_x.field_size, Symbolic) assert isinstance(descr_y.field_size, Symbolic) assert isinstance(descr_z.field_size, Symbolic) assert isinstance(descr_f.field_size, Symbolic) assert isinstance(descr_s.field_size, Symbolic) assert descr_x.flag == FLAG_UNSIGNED assert descr_y.flag == FLAG_POINTER assert descr_z.flag == FLAG_UNSIGNED assert descr_f.flag == FLAG_FLOAT assert descr_s.flag == FLAG_UNSIGNED
def test_get_field_descr(): U = lltype.Struct('U') T = lltype.GcStruct('T') S = lltype.GcStruct('S', ('x', lltype.Char), ('y', lltype.Ptr(T)), ('z', lltype.Ptr(U)), ('f', lltype.Float)) assert getFieldDescrClass(lltype.Ptr(T)) is GcPtrFieldDescr assert getFieldDescrClass(lltype.Ptr(U)) is NonGcPtrFieldDescr cls = getFieldDescrClass(lltype.Char) assert cls != getFieldDescrClass(lltype.Signed) assert cls == getFieldDescrClass(lltype.Char) clsf = getFieldDescrClass(lltype.Float) assert clsf != cls assert clsf == getFieldDescrClass(lltype.Float) # c0 = GcCache(False) c1 = GcCache(True) assert get_field_descr(c0, S, 'y') == get_field_descr(c0, S, 'y') assert get_field_descr(c0, S, 'y') != get_field_descr(c1, S, 'y') for tsc in [False, True]: c2 = GcCache(tsc) descr_x = get_field_descr(c2, S, 'x') descr_y = get_field_descr(c2, S, 'y') descr_z = get_field_descr(c2, S, 'z') descr_f = get_field_descr(c2, S, 'f') assert descr_x.__class__ is cls assert descr_y.__class__ is GcPtrFieldDescr assert descr_z.__class__ is NonGcPtrFieldDescr assert descr_f.__class__ is clsf if not tsc: assert descr_x.offset < descr_y.offset < descr_z.offset assert descr_x.sort_key() < descr_y.sort_key() < descr_z.sort_key() assert descr_x.get_field_size(False) == rffi.sizeof(lltype.Char) assert descr_y.get_field_size(False) == rffi.sizeof(lltype.Ptr(T)) assert descr_z.get_field_size(False) == rffi.sizeof(lltype.Ptr(U)) assert descr_f.get_field_size(False) == rffi.sizeof(lltype.Float) else: assert isinstance(descr_x.offset, Symbolic) assert isinstance(descr_y.offset, Symbolic) assert isinstance(descr_z.offset, Symbolic) assert isinstance(descr_f.offset, Symbolic) assert isinstance(descr_x.get_field_size(True), Symbolic) assert isinstance(descr_y.get_field_size(True), Symbolic) assert isinstance(descr_z.get_field_size(True), Symbolic) assert isinstance(descr_f.get_field_size(True), Symbolic) assert not descr_x.is_pointer_field() assert descr_y.is_pointer_field() assert not descr_z.is_pointer_field() assert not descr_f.is_pointer_field() assert not descr_x.is_float_field() assert not descr_y.is_float_field() assert not descr_z.is_float_field() assert descr_f.is_float_field()
def _build_array_converters(): "NOT_RPYTHON" array_info = ( ('h', rffi.sizeof(rffi.SHORT), ("short int", "short")), ('H', rffi.sizeof(rffi.USHORT), ("unsigned short int", "unsigned short")), ('i', rffi.sizeof(rffi.INT), ("int",)), ('I', rffi.sizeof(rffi.UINT), ("unsigned int", "unsigned")), ('l', rffi.sizeof(rffi.LONG), ("long int", "long")), ('L', rffi.sizeof(rffi.ULONG), ("unsigned long int", "unsigned long")), ('f', rffi.sizeof(rffi.FLOAT), ("float",)), ('d', rffi.sizeof(rffi.DOUBLE), ("double",)), ) for info in array_info: class ArrayConverter(ArrayTypeConverterMixin, TypeConverter): _immutable_ = True typecode = info[0] typesize = info[1] class PtrConverter(PtrTypeConverterMixin, TypeConverter): _immutable_ = True typecode = info[0] typesize = info[1] for name in info[2]: _a_converters[name+'[]'] = ArrayConverter _a_converters[name+'*'] = PtrConverter
def test_get_field_descr(): U = lltype.Struct('U') T = lltype.GcStruct('T') S = lltype.GcStruct('S', ('x', lltype.Char), ('y', lltype.Ptr(T)), ('z', lltype.Ptr(U)), ('f', lltype.Float), ('s', lltype.SingleFloat)) # c0 = GcCache(False) c1 = GcCache(True) assert get_field_descr(c0, S, 'y') == get_field_descr(c0, S, 'y') assert get_field_descr(c0, S, 'y') != get_field_descr(c1, S, 'y') for tsc in [False, True]: c2 = GcCache(tsc) descr_x = get_field_descr(c2, S, 'x') descr_y = get_field_descr(c2, S, 'y') descr_z = get_field_descr(c2, S, 'z') descr_f = get_field_descr(c2, S, 'f') descr_s = get_field_descr(c2, S, 's') assert isinstance(descr_x, FieldDescr) assert descr_x.name == 'S.x' assert descr_y.name == 'S.y' assert descr_z.name == 'S.z' assert descr_f.name == 'S.f' assert descr_s.name == 'S.s' if not tsc: assert descr_x.offset < descr_y.offset < descr_z.offset assert descr_x.sort_key() < descr_y.sort_key() < descr_z.sort_key() assert descr_x.field_size == rffi.sizeof(lltype.Char) assert descr_y.field_size == rffi.sizeof(lltype.Ptr(T)) assert descr_z.field_size == rffi.sizeof(lltype.Ptr(U)) assert descr_f.field_size == rffi.sizeof(lltype.Float) assert descr_s.field_size == rffi.sizeof(lltype.SingleFloat) else: assert isinstance(descr_x.offset, Symbolic) assert isinstance(descr_y.offset, Symbolic) assert isinstance(descr_z.offset, Symbolic) assert isinstance(descr_f.offset, Symbolic) assert isinstance(descr_s.offset, Symbolic) assert isinstance(descr_x.field_size, Symbolic) assert isinstance(descr_y.field_size, Symbolic) assert isinstance(descr_z.field_size, Symbolic) assert isinstance(descr_f.field_size, Symbolic) assert isinstance(descr_s.field_size, Symbolic) assert descr_x.flag == FLAG_UNSIGNED assert descr_y.flag == FLAG_POINTER assert descr_z.flag == FLAG_UNSIGNED assert descr_f.flag == FLAG_FLOAT assert descr_s.flag == FLAG_UNSIGNED
def __init__(self, path): sun = lltype.malloc(_c.sockaddr_un, flavor='raw', zero=True) baseofs = offsetof(_c.sockaddr_un, 'c_sun_path') self.setdata(sun, baseofs + len(path)) rffi.setintfield(sun, 'c_sun_family', AF_UNIX) if _c.linux and path.startswith('\x00'): # Linux abstract namespace extension if len(path) > sizeof(_c.sockaddr_un.c_sun_path): raise RSocketError("AF_UNIX path too long") else: # regular NULL-terminated string if len(path) >= sizeof(_c.sockaddr_un.c_sun_path): raise RSocketError("AF_UNIX path too long") sun.c_sun_path[len(path)] = '\x00' for i in range(len(path)): sun.c_sun_path[i] = path[i]
def estimate_best_nursery_size(debugprint=False): """Try to estimate the best nursery size at run-time, depending on the machine we are running on. """ L2cache = 0 l2cache_p = lltype.malloc(rffi.LONGLONGP.TO, 1, flavor='raw') try: len_p = lltype.malloc(rffi.SIZE_TP.TO, 1, flavor='raw') try: size = rffi.sizeof(rffi.LONGLONG) l2cache_p[0] = rffi.cast(rffi.LONGLONG, 0) len_p[0] = rffi.cast(rffi.SIZE_T, size) # XXX a hack for llhelper not being robust-enough result = sysctlbyname("hw.l2cachesize", rffi.cast(rffi.VOIDP, l2cache_p), len_p, lltype.nullptr(rffi.VOIDP.TO), rffi.cast(rffi.SIZE_T, 0)) if (rffi.cast(lltype.Signed, result) == 0 and rffi.cast(lltype.Signed, len_p[0]) == size): L2cache = rffi.cast(lltype.Signed, l2cache_p[0]) if rffi.cast(rffi.LONGLONG, L2cache) != l2cache_p[0]: L2cache = 0 # overflow! finally: lltype.free(len_p, flavor='raw') finally: lltype.free(l2cache_p, flavor='raw') if L2cache > 0: return best_nursery_size_for_L2cache(L2cache, debugprint) else: # Print a warning even in non-debug builds llop.debug_print( lltype.Void, "Warning: cannot find your CPU L2 cache size with sysctl()") return -1
def estimate_best_nursery_size(): """Try to estimate the best nursery size at run-time, depending on the machine we are running on. """ L2cache = 0 l2cache_p = lltype.malloc(rffi.LONGLONGP.TO, 1, flavor='raw') try: len_p = lltype.malloc(rffi.SIZE_TP.TO, 1, flavor='raw') try: size = rffi.sizeof(rffi.LONGLONG) l2cache_p[0] = rffi.cast(rffi.LONGLONG, 0) len_p[0] = rffi.cast(rffi.SIZE_T, size) result = sysctlbyname("hw.l2cachesize", rffi.cast(rffi.VOIDP, l2cache_p), len_p, lltype.nullptr(rffi.VOIDP.TO), rffi.cast(rffi.SIZE_T, 0)) if (rffi.cast(lltype.Signed, result) == 0 and rffi.cast(lltype.Signed, len_p[0]) == size): L2cache = rffi.cast(lltype.Signed, l2cache_p[0]) if rffi.cast(rffi.LONGLONG, L2cache) != l2cache_p[0]: L2cache = 0 # overflow! finally: lltype.free(len_p, flavor='raw') finally: lltype.free(l2cache_p, flavor='raw') if L2cache > 0: return best_nursery_size_for_L2cache(L2cache) else: # Print a warning even in non-debug builds llop.debug_print(lltype.Void, "Warning: cannot find your CPU L2 cache size with sysctl()") return -1
def _unsigned_type_for(TYPE): sz = rffi.sizeof(TYPE) if sz == 1: return ffi_type_uint8 elif sz == 2: return ffi_type_uint16 elif sz == 4: return ffi_type_uint32 elif sz == 8: return ffi_type_uint64 else: raise ValueError("unsupported type size for %r" % (TYPE,))
def test_callback(self): libc = CDLL('libc.so.6') qsort = libc.getpointer('qsort', [ffi_type_pointer, ffi_type_slong, ffi_type_slong, ffi_type_pointer], ffi_type_void) def callback(ll_args, ll_res, stuff): a1 = rffi.cast(rffi.INTP, rffi.cast(rffi.VOIDPP, ll_args[0])[0])[0] a2 = rffi.cast(rffi.INTP, rffi.cast(rffi.VOIDPP, ll_args[0])[1])[0] res = rffi.cast(rffi.INTP, ll_res) if a1 > a2: res[0] = 1 else: res[0] = -1 ptr = CallbackFuncPtr([ffi_type_pointer, ffi_type_pointer], ffi_type_sint, callback) TP = rffi.CArray(rffi.INT) to_sort = lltype.malloc(TP, 4, flavor='raw') to_sort[0] = 4 to_sort[1] = 3 to_sort[2] = 1 to_sort[3] = 2 qsort.push_arg(rffi.cast(rffi.VOIDP, to_sort)) qsort.push_arg(rffi.sizeof(rffi.INT)) qsort.push_arg(4) qsort.push_arg(ptr.ll_closure) qsort.call(lltype.Void) assert [to_sort[i] for i in range(4)] == [1,2,3,4] lltype.free(to_sort, flavor='raw')
def _more(self): chunk = rffi.cast(CLOSURES, alloc(CHUNK)) count = CHUNK//rffi.sizeof(FFI_CLOSUREP.TO) for i in range(count): rffi.cast(rffi.VOIDPP, chunk)[0] = self.free_list self.free_list = rffi.cast(rffi.VOIDP, chunk) chunk = rffi.ptradd(chunk, 1)
def external(name, args, result, eci=CConfig._compilation_info_): if _WIN and rffi.sizeof(rffi.TIME_T) == 8: # Recent Microsoft compilers use 64bit time_t and # the corresponding functions are named differently if rffi.TIME_T in args or rffi.TIME_TP in args or result in (rffi.TIME_T, rffi.TIME_TP): name = "_" + name + "64" return rffi.llexternal(name, args, result, compilation_info=eci, calling_conv=calling_conv, threadsafe=False)
def get_layout(TYPE): layout = {} if isinstance(TYPE, lltype.Primitive): try: return primitive_to_fmt[TYPE] except KeyError: from pypy.rpython.lltypesystem import rffi return rffi.sizeof(TYPE) elif isinstance(TYPE, lltype.Ptr): return "P" elif isinstance(TYPE, lltype.Struct): curr = 0 for name in TYPE._names: layout[name] = curr curr += get_fixed_size(TYPE._flds[name]) layout["_size"] = curr return layout elif isinstance(TYPE, lltype.Array): return (get_fixed_size(lltype.Signed), get_fixed_size(TYPE.OF)) elif isinstance(TYPE, lltype.OpaqueType): return "i" elif isinstance(TYPE, lltype.FuncType): return "i" elif isinstance(TYPE, lltype.PyObjectType): return "i" else: assert 0, "type %s not yet implemented" % (TYPE, )
def get_darwin_sysctl_signed(sysctl_name): rval_p = lltype.malloc(rffi.LONGLONGP.TO, 1, flavor='raw') try: len_p = lltype.malloc(rffi.SIZE_TP.TO, 1, flavor='raw') try: size = rffi.sizeof(rffi.LONGLONG) rval_p[0] = rffi.cast(rffi.LONGLONG, 0) len_p[0] = rffi.cast(rffi.SIZE_T, size) # XXX a hack for llhelper not being robust-enough result = sysctlbyname(sysctl_name, rffi.cast(rffi.VOIDP, rval_p), len_p, lltype.nullptr(rffi.VOIDP.TO), rffi.cast(rffi.SIZE_T, 0)) rval = 0 if (rffi.cast(lltype.Signed, result) == 0 and rffi.cast(lltype.Signed, len_p[0]) == size): rval = rffi.cast(lltype.Signed, rval_p[0]) if rffi.cast(rffi.LONGLONG, rval) != rval_p[0]: rval = 0 # overflow! return rval finally: lltype.free(len_p, flavor='raw') finally: lltype.free(rval_p, flavor='raw')
def f(): libc = CDLL(get_libc_name()) qsort = libc.getpointer( 'qsort', [ffi_type_pointer, ffi_size_t, ffi_size_t, ffi_type_pointer], ffi_type_void) ptr = CallbackFuncPtr([ffi_type_pointer, ffi_type_pointer], ffi_type_sint, callback) TP = rffi.CArray(rffi.LONG) to_sort = lltype.malloc(TP, 4, flavor='raw') to_sort[0] = 4 to_sort[1] = 3 to_sort[2] = 1 to_sort[3] = 2 qsort.push_arg(rffi.cast(rffi.VOIDP, to_sort)) qsort.push_arg(rffi.cast(rffi.SIZE_T, 4)) qsort.push_arg(rffi.cast(rffi.SIZE_T, rffi.sizeof(rffi.LONG))) qsort.push_arg(rffi.cast(rffi.VOIDP, ptr.ll_closure)) qsort.call(lltype.Void) result = [to_sort[i] for i in range(4)] == [1, 2, 3, 4] lltype.free(to_sort, flavor='raw') keepalive_until_here(ptr) return int(result)
def test_callback(self): slong = cast_type_to_ffitype(rffi.LONG) libc = self.get_libc() qsort = libc.getpointer("qsort", [ffi_type_pointer, slong, slong, ffi_type_pointer], ffi_type_void) def callback(ll_args, ll_res, stuff): p_a1 = rffi.cast(rffi.VOIDPP, ll_args[0])[0] p_a2 = rffi.cast(rffi.VOIDPP, ll_args[1])[0] a1 = rffi.cast(rffi.INTP, p_a1)[0] a2 = rffi.cast(rffi.INTP, p_a2)[0] res = rffi.cast(rffi.INTP, ll_res) if a1 > a2: res[0] = rffi.cast(rffi.INT, 1) else: res[0] = rffi.cast(rffi.INT, -1) ptr = CallbackFuncPtr([ffi_type_pointer, ffi_type_pointer], ffi_type_sint, callback) TP = rffi.CArray(rffi.INT) to_sort = lltype.malloc(TP, 4, flavor="raw") to_sort[0] = rffi.cast(rffi.INT, 4) to_sort[1] = rffi.cast(rffi.INT, 3) to_sort[2] = rffi.cast(rffi.INT, 1) to_sort[3] = rffi.cast(rffi.INT, 2) qsort.push_arg(rffi.cast(rffi.VOIDP, to_sort)) qsort.push_arg(rffi.sizeof(rffi.INT)) qsort.push_arg(4) qsort.push_arg(ptr.ll_closure) qsort.call(lltype.Void) assert [rffi.cast(lltype.Signed, to_sort[i]) for i in range(4)] == [1, 2, 3, 4] lltype.free(to_sort, flavor="raw") keepalive_until_here(ptr) # <= this test is not translated, but don't
def get_long_info(space): assert rbigint.SHIFT == 31 bits_per_digit = rbigint.SHIFT sizeof_digit = rffi.sizeof(rffi.ULONG) info_w = [space.wrap(bits_per_digit), space.wrap(sizeof_digit)] w_long_info = app.wget(space, "long_info") return space.call_function(w_long_info, space.newtuple(info_w))
def test_addr_raw_packet(): if not hasattr(rsocket._c, 'sockaddr_ll'): py.test.skip("posix specific test") c_addr_ll = lltype.malloc(rsocket._c.sockaddr_ll, flavor='raw') addrlen = rffi.sizeof(rsocket._c.sockaddr_ll) c_addr = rffi.cast(lltype.Ptr(rsocket._c.sockaddr), c_addr_ll) rffi.setintfield(c_addr_ll, 'c_sll_ifindex', 1) rffi.setintfield(c_addr_ll, 'c_sll_protocol', 8) rffi.setintfield(c_addr_ll, 'c_sll_pkttype', 13) rffi.setintfield(c_addr_ll, 'c_sll_hatype', 0) rffi.setintfield(c_addr_ll, 'c_sll_halen', 3) c_addr_ll.c_sll_addr[0] = 'a' c_addr_ll.c_sll_addr[1] = 'b' c_addr_ll.c_sll_addr[2] = 'c' rffi.setintfield(c_addr, 'c_sa_family', socket.AF_PACKET) # fd needs to be somehow valid s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) fd = s.fileno() w_obj = rsocket.make_address(c_addr, addrlen).as_object(fd, space) lltype.free(c_addr_ll, flavor='raw') assert space.is_true( space.eq( w_obj, space.newtuple([ space.wrap('lo'), space.wrap(socket.ntohs(8)), space.wrap(13), space.wrap(False), space.wrap("abc"), ])))
def test_addr_raw_packet(): if not hasattr(rsocket._c, 'sockaddr_ll'): py.test.skip("posix specific test") c_addr_ll = lltype.malloc(rsocket._c.sockaddr_ll, flavor='raw') addrlen = rffi.sizeof(rsocket._c.sockaddr_ll) c_addr = rffi.cast(lltype.Ptr(rsocket._c.sockaddr), c_addr_ll) rffi.setintfield(c_addr_ll, 'c_sll_ifindex', 1) rffi.setintfield(c_addr_ll, 'c_sll_protocol', 8) rffi.setintfield(c_addr_ll, 'c_sll_pkttype', 13) rffi.setintfield(c_addr_ll, 'c_sll_hatype', 0) rffi.setintfield(c_addr_ll, 'c_sll_halen', 3) c_addr_ll.c_sll_addr[0] = 'a' c_addr_ll.c_sll_addr[1] = 'b' c_addr_ll.c_sll_addr[2] = 'c' rffi.setintfield(c_addr, 'c_sa_family', socket.AF_PACKET) # fd needs to be somehow valid s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) fd = s.fileno() w_obj = rsocket.make_address(c_addr, addrlen).as_object(fd, space) lltype.free(c_addr_ll, flavor='raw') assert space.is_true(space.eq(w_obj, space.newtuple([ space.wrap('lo'), space.wrap(socket.ntohs(8)), space.wrap(13), space.wrap(False), space.wrap("abc"), ])))
def test_callback(self): libc = CDLL('libc.so.6') qsort = libc.getpointer('qsort', [ ffi_type_pointer, ffi_type_slong, ffi_type_slong, ffi_type_pointer ], ffi_type_void) def callback(ll_args, ll_res, stuff): a1 = rffi.cast(rffi.INTP, rffi.cast(rffi.VOIDPP, ll_args[0])[0])[0] a2 = rffi.cast(rffi.INTP, rffi.cast(rffi.VOIDPP, ll_args[0])[1])[0] res = rffi.cast(rffi.INTP, ll_res) if a1 > a2: res[0] = 1 else: res[0] = -1 ptr = CallbackFuncPtr([ffi_type_pointer, ffi_type_pointer], ffi_type_sint, callback) TP = rffi.CArray(rffi.INT) to_sort = lltype.malloc(TP, 4, flavor='raw') to_sort[0] = 4 to_sort[1] = 3 to_sort[2] = 1 to_sort[3] = 2 qsort.push_arg(rffi.cast(rffi.VOIDP, to_sort)) qsort.push_arg(rffi.sizeof(rffi.INT)) qsort.push_arg(4) qsort.push_arg(ptr.ll_closure) qsort.call(lltype.Void) assert [to_sort[i] for i in range(4)] == [1, 2, 3, 4] lltype.free(to_sort, flavor='raw')
def __init__(self, itemtype, unwrap, canoverflow=False, signed=False): self.itemtype = itemtype self.bytes = rffi.sizeof(itemtype) #self.arraytype = lltype.GcArray(itemtype) self.arraytype = lltype.Array(itemtype, hints={'nolength': True}) self.unwrap = unwrap self.signed = signed self.canoverflow = canoverflow self.w_class = None if self.canoverflow: assert self.bytes <= rffi.sizeof(rffi.ULONG) if self.bytes == rffi.sizeof(rffi.ULONG) and not signed and \ self.unwrap == 'int_w': # Treat this type as a ULONG self.unwrap = 'bigint_w' self.canoverflow = False
def f(data, n): i = 0 s = rffi.cast(COMPUTE_TYPE, 0) while i < n: myjitdriver.jit_merge_point(n=n, i=i, s=s, data=data) s += rffi.cast(COMPUTE_TYPE, array_getitem(ffitype, rffi.sizeof(TYPE), data, 0, 0)) i += 1 return s
def gcmapend(self): addr = self.gcmapstart() if self._gcmap_curlength: addr += rffi.sizeof(lltype.Signed) * self._gcmap_curlength if not we_are_translated() and type(addr) is long: from pypy.rpython.lltypesystem import ll2ctypes addr = ll2ctypes._lladdress(addr) # XXX workaround return addr
def keysize((_, T)): if T is lltype.Void: return None from pypy.rpython.lltypesystem.rffi import sizeof try: return -sizeof(T) except StandardError: return None
def test_gc_heap_stats(self): py.test.skip("this test makes the following test crash. Investigate.") run = self.runner("gc_heap_stats") res = run([]) assert res % 10000 == 2611 totsize = (res / 10000) size_of_int = rffi.sizeof(lltype.Signed) assert (totsize - 26 * size_of_int) % 4 == 0
def setup(): INSPECT = {'b': 'signed char', 'h': 'signed short', 'i': 'signed int', 'l': 'signed long', 'q': 'signed long long', 'B': 'unsigned char', 'H': 'unsigned short', 'I': 'unsigned int', 'L': 'unsigned long', 'Q': 'unsigned long long', 'P': 'char *', 'f': 'float', 'd': 'double', } pre_include_lines = [] for fmtchar, ctype in INSPECT.items(): pre_include_lines += (""" struct about_%s { char pad; %s field; }; """ % (fmtchar, ctype)).split("\n") class CConfig: _compilation_info_ = ExternalCompilationInfo( pre_include_lines = pre_include_lines ) for fmtchar, ctype in INSPECT.items(): setattr(CConfig, fmtchar, rffi_platform.Struct( "struct about_%s" % (fmtchar,), [('field', lltype.FixedSizeArray(rffi.CHAR, 1))])) cConfig = rffi_platform.configure(CConfig) for fmtchar, ctype in INSPECT.items(): S = cConfig[fmtchar] alignment = rffi.offsetof(S, 'c_field') size = rffi.sizeof(S.c_field) signed = 'a' <= fmtchar <= 'z' if fmtchar == 'f': pack = pack_float unpack = unpack_float elif fmtchar == 'd': pack = pack_double unpack = unpack_double else: cpython_checks_range = fmtchar in 'bBhH' pack = std.make_int_packer(size, signed, cpython_checks_range) unpack = std.make_int_unpacker(size, signed) native_fmttable[fmtchar] = {'size': size, 'alignment': alignment, 'pack': pack, 'unpack': unpack}
def convert_to_regdata(space, w_value, typ): buf = None if typ == rwinreg.REG_DWORD: if space.is_true(space.isinstance(w_value, space.w_int)): buflen = rffi.sizeof(rwin32.DWORD) buf1 = lltype.malloc(rffi.CArray(rwin32.DWORD), 1, flavor='raw') buf1[0] = space.uint_w(w_value) buf = rffi.cast(rffi.CCHARP, buf1) elif typ == rwinreg.REG_SZ or typ == rwinreg.REG_EXPAND_SZ: if space.is_w(w_value, space.w_None): buflen = 1 buf = lltype.malloc(rffi.CCHARP.TO, buflen, flavor='raw') buf[0] = '\0' else: if space.is_true(space.isinstance(w_value, space.w_unicode)): w_value = space.call_method(w_value, 'encode', space.wrap('mbcs')) buf = rffi.str2charp(space.str_w(w_value)) buflen = space.len_w(w_value) + 1 elif typ == rwinreg.REG_MULTI_SZ: if space.is_w(w_value, space.w_None): buflen = 1 buf = lltype.malloc(rffi.CCHARP.TO, buflen, flavor='raw') buf[0] = '\0' elif space.is_true(space.isinstance(w_value, space.w_list)): strings = [] buflen = 0 # unwrap strings and compute total size w_iter = space.iter(w_value) while True: try: w_item = space.next(w_iter) if space.is_true(space.isinstance(w_item, space.w_unicode)): w_item = space.call_method(w_item, 'encode', space.wrap('mbcs')) item = space.str_w(w_item) strings.append(item) buflen += len(item) + 1 except OperationError, e: if not e.match(space, space.w_StopIteration): raise # re-raise other app-level exceptions break buflen += 1 buf = lltype.malloc(rffi.CCHARP.TO, buflen, flavor='raw') # Now copy data buflen = 0 for string in strings: for i in range(len(string)): buf[buflen + i] = string[i] buflen += len(string) + 1 buf[buflen - 1] = '\0' buflen += 1 buf[buflen - 1] = '\0'
def from_in6_addr(in6_addr): result = instantiate(INET6Address) # store the malloc'ed data into 'result' as soon as possible # to avoid leaks if an exception occurs inbetween sin = lltype.malloc(_c.sockaddr_in6, flavor='raw', zero=True) result.setdata(sin, sizeof(_c.sockaddr_in6)) rffi.setintfield(sin, 'c_sin6_family', AF_INET) rffi.structcopy(sin.c_sin6_addr, in6_addr) return result
def __init__(self, rtyper, stats=None, translate_support_code=False, annmixlevel=None, gcdescr=None): self.rtyper = rtyper self.translate_support_code = translate_support_code self.compiled_functions = [] self.fail_ops = [] self.in_out_args = [] if translate_support_code: get_size = llmemory.sizeof else: get_size = rffi.sizeof self._arraydescrs = [ ArrayDescr(get_size(llmemory.GCREF), self.SIZE_GCPTR), # 0 ArrayDescr(get_size(lltype.Signed), self.SIZE_INT), # 1 ArrayDescr(get_size(lltype.Char), self.SIZE_CHAR), # 2 ArrayDescr(get_size(lltype.UniChar), self.SIZE_UNICHAR), # 3 ] self._descr_caches = {} self.fielddescr_vtable = self.fielddescrof(rclass.OBJECT, 'typeptr') if sys.maxint == 2147483647: self.size_of_int = 4 else: self.size_of_int = 8 if runicode.MAXUNICODE > 0xffff: self.size_of_unicode = 4 else: self.size_of_unicode = 2 self.gcarray_gcref = lltype.GcArray(llmemory.GCREF) self.gcarray_signed = lltype.GcArray(lltype.Signed) self.gcarray_char = lltype.GcArray(lltype.Char) self.gcarray_unichar = lltype.GcArray(lltype.UniChar) basesize, _, ofs_length = symbolic.get_array_token( self.gcarray_signed, self.translate_support_code) self.array_index_array = basesize self.array_index_length = ofs_length basesize, _, ofs_length = symbolic.get_array_token( rstr.STR, self.translate_support_code) self.string_index_array = basesize self.string_index_length = ofs_length basesize, _, ofs_length = symbolic.get_array_token( rstr.UNICODE, self.translate_support_code) self.unicode_index_array = basesize self.unicode_index_length = ofs_length self.vtable_descr = self.fielddescrof(rclass.OBJECT, 'typeptr') self._ovf_error_instance = self._get_prebuilt_error(OverflowError) self._zer_error_instance = self._get_prebuilt_error(ZeroDivisionError) # # temporary (Boehm only) from pypy.translator.tool.cbuild import ExternalCompilationInfo compilation_info = ExternalCompilationInfo(libraries=['gc']) self.malloc_fn_ptr = rffi.llexternal("GC_malloc", [rffi.SIZE_T], llmemory.GCREF, compilation_info=compilation_info, sandboxsafe=True, _nowrapper=True) assert rffi.sizeof(rffi.SIZE_T) == self.size_of_int
def compress_callshape(self, shape, datablockwrapper): length = len(shape) SZINT = rffi.sizeof(rffi.INT) rawaddr = datablockwrapper.malloc_aligned((length + 1) * SZINT, SZINT) p = rffi.cast(self.INTARRAYPTR, rawaddr) for i in range(length): p[i] = rffi.cast(rffi.INT, shape[i]) p[length] = rffi.cast(rffi.INT, 0) return p
def flush(self): if self.buf_count > 0: bytes = self.buf_count * rffi.sizeof(rffi.LONG) count = raw_os_write(self.fd, rffi.cast(llmemory.Address, self.writebuffer), rffi.cast(rffi.SIZE_T, bytes)) if rffi.cast(lltype.Signed, count) != bytes: raise OSError(rposix.get_errno(), "raw_os_write failed") self.buf_count = 0