本文整理汇总了Python中pypy.rpython.lltypesystem.rffi.sizeof函数的典型用法代码示例。如果您正苦于以下问题:Python sizeof函数的具体用法?Python sizeof怎么用?Python sizeof使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sizeof函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getDescrClass
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
示例2: push_arg_as_ffiptr
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
示例3: byteswap
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)
示例4: test_closure_heap
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)
示例5: _get_bind_info
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')
示例6: test_get_field_descr
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()
示例7: _fits_into_signed
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)
示例8: inet_ntoa
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')
示例9: _build_array_converters
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
示例10: test_get_field_descr
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
示例11: get_darwin_sysctl_signed
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')
示例12: test_addr_raw_packet
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"),
])))
示例13: __init__
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]
示例14: test_callback
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')
示例15: estimate_best_nursery_size
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