pypy / pypy / module / _ffi /

from pypy.rpython.lltypesystem import lltype, rffi
from pypy.rlib import clibffi
from pypy.rlib import libffi
from pypy.rlib import jit
from pypy.rlib.rgc import must_be_light_finalizer
from pypy.rlib.rarithmetic import r_uint, r_ulonglong, r_singlefloat, intmask
from pypy.interpreter.baseobjspace import Wrappable
from pypy.interpreter.typedef import TypeDef, interp_attrproperty
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.error import operationerrfmt
from pypy.objspace.std.typetype import type_typedef
from pypy.module._ffi.interp_ffitype import W_FFIType, app_types
from pypy.module._ffi.type_converter import FromAppLevelConverter, ToAppLevelConverter

class W_Field(Wrappable):

    def __init__(self, name, w_ffitype): = name
        self.w_ffitype = w_ffitype
        self.offset = -1

    def __repr__(self):
        return '<Field %s %s>' % (,

def descr_new_field(space, w_type, name, w_ffitype):
    w_ffitype = space.interp_w(W_FFIType, w_ffitype)
    return W_Field(name, w_ffitype)

W_Field.typedef = TypeDef(
    __new__ = interp2app(descr_new_field),
    name = interp_attrproperty('name', W_Field),
    ffitype = interp_attrproperty('w_ffitype', W_Field),
    offset = interp_attrproperty('offset', W_Field),

# ==============================================================================

class FFIStructOwner(object):
    The only job of this class is to stay outside of the reference cycle
    W__StructDescr -> W_FFIType -> W__StructDescr and free the ffistruct

    def __init__(self, ffistruct):
        self.ffistruct = ffistruct

    def __del__(self):
        if self.ffistruct:
  , flavor='raw', track_allocation=True)

class W__StructDescr(Wrappable):

    def __init__(self, name):
        self.w_ffitype = W_FFIType('struct %s' % name, clibffi.FFI_TYPE_NULL,
        self.fields_w = None
        self.name2w_field = {}
        self._ffistruct_owner = None

    def define_fields(self, space, w_fields):
        if self.fields_w is not None:
            raise operationerrfmt(space.w_ValueError,
                                  "%s's fields has already been defined",
        fields_w = space.fixedview(w_fields)
        # note that the fields_w returned by compute_size_and_alignement has a
        # different annotation than the original: list(W_Root) vs list(W_Field)
        size, alignment, fields_w = compute_size_and_alignement(space, fields_w)
        self.fields_w = fields_w
        field_types = [] # clibffi's types
        for w_field in fields_w:
            self.name2w_field[] = w_field
        # on CPython, the FFIStructOwner might go into gc.garbage and thus the
        # __del__ never be called. Thus, we don't track the allocation of the
        # malloc done inside this function, else the leakfinder might complain
        ffistruct = clibffi.make_struct_ffitype_e(size, alignment, field_types,
        self._ffistruct_owner = FFIStructOwner(ffistruct)

    def check_complete(self, space):
        if self.fields_w is None:
            raise operationerrfmt(space.w_ValueError, "%s has an incomplete type",

    def allocate(self, space):
        return W__StructInstance(self)

    def fromaddress(self, space, addr):
        rawmem = rffi.cast(rffi.VOIDP, addr)
        return W__StructInstance(self, allocate=False, autofree=True, rawmem=rawmem)

    def get_type_and_offset_for_field(self, space, name):
            w_field = self.name2w_field[name]
        except KeyError:
            raise operationerrfmt(space.w_AttributeError, '%s', name)

        return w_field.w_ffitype, w_field.offset

def descr_new_structdescr(space, w_type, name, w_fields=None):
    descr = W__StructDescr(name)
    if not space.is_none(w_fields):
        descr.define_fields(space, w_fields)
    return descr

def round_up(size, alignment):
    return (size + alignment - 1) & -alignment

def compute_size_and_alignement(space, fields_w):
    size = 0
    alignment = 1
    fields_w2 = []
    for w_field in fields_w:
        w_field = space.interp_w(W_Field, w_field)
        fieldsize = w_field.w_ffitype.sizeof()
        fieldalignment = w_field.w_ffitype.get_alignment()
        alignment = max(alignment, fieldalignment)
        size = round_up(size, fieldalignment)
        w_field.offset = size
        size += fieldsize
    size = round_up(size, alignment)
    return size, alignment, fields_w2

W__StructDescr.typedef = TypeDef(
    __new__ = interp2app(descr_new_structdescr),
    ffitype = interp_attrproperty('w_ffitype', W__StructDescr),
    define_fields = interp2app(W__StructDescr.define_fields),
    allocate = interp2app(W__StructDescr.allocate),
    fromaddress = interp2app(W__StructDescr.fromaddress),

# ==============================================================================

NULL = lltype.nullptr(rffi.VOIDP.TO)

class W__StructInstance(Wrappable):

    _immutable_fields_ = ['structdescr', 'rawmem']

    def __init__(self, structdescr, allocate=True, autofree=True, rawmem=NULL):
        self.structdescr = structdescr
        self.autofree = autofree
        if allocate:
            assert not rawmem
            assert autofree
            size = structdescr.w_ffitype.sizeof()
            self.rawmem = lltype.malloc(rffi.VOIDP.TO, size, flavor='raw',
                                        zero=True, add_memory_pressure=True)
            self.rawmem = rawmem

    def __del__(self):
        if self.autofree and self.rawmem:
  , flavor='raw')
            self.rawmem = lltype.nullptr(rffi.VOIDP.TO)

    def getaddr(self, space):
        addr = rffi.cast(rffi.ULONG, self.rawmem)
        return space.wrap(addr)

    def getfield(self, space, name):
        w_ffitype, offset = self.structdescr.get_type_and_offset_for_field(
            space, name)
        field_getter = GetFieldConverter(space, self.rawmem, offset)
        return field_getter.do_and_wrap(w_ffitype)

    def setfield(self, space, name, w_value):
        w_ffitype, offset = self.structdescr.get_type_and_offset_for_field(
            space, name)
        field_setter = SetFieldConverter(space, self.rawmem, offset)
        field_setter.unwrap_and_do(w_ffitype, w_value)

class GetFieldConverter(ToAppLevelConverter):
    A converter used by W__StructInstance to get a field from the struct and
    wrap it to the correct app-level type.

    def __init__(self, space, rawmem, offset): = space
        self.rawmem = rawmem
        self.offset = offset

    def get_longlong(self, w_ffitype): 
        return libffi.struct_getfield_longlong(libffi.types.slonglong,
                                               self.rawmem, self.offset)

    def get_ulonglong(self, w_ffitype):
        longlongval = libffi.struct_getfield_longlong(libffi.types.ulonglong,
                                                      self.rawmem, self.offset)
        return r_ulonglong(longlongval)

    def get_signed(self, w_ffitype):
        return libffi.struct_getfield_int(w_ffitype.get_ffitype(),
                                          self.rawmem, self.offset)

    def get_unsigned(self, w_ffitype):
        value = libffi.struct_getfield_int(w_ffitype.get_ffitype(),
                                           self.rawmem, self.offset)
        return r_uint(value)

    get_unsigned_which_fits_into_a_signed = get_signed
    get_pointer = get_unsigned

    def get_char(self, w_ffitype):
        intval = libffi.struct_getfield_int(w_ffitype.get_ffitype(),
                                            self.rawmem, self.offset)
        return rffi.cast(rffi.UCHAR, intval)

    def get_unichar(self, w_ffitype):
        intval = libffi.struct_getfield_int(w_ffitype.get_ffitype(),
                                            self.rawmem, self.offset)
        return rffi.cast(rffi.WCHAR_T, intval)

    def get_float(self, w_ffitype):
        return libffi.struct_getfield_float(w_ffitype.get_ffitype(),
                                            self.rawmem, self.offset)

    def get_singlefloat(self, w_ffitype):
        return libffi.struct_getfield_singlefloat(w_ffitype.get_ffitype(),
                                                  self.rawmem, self.offset)

    def get_struct(self, w_ffitype, w_structdescr):
        assert isinstance(w_structdescr, W__StructDescr)
        rawmem = rffi.cast(rffi.CCHARP, self.rawmem)
        innermem = rffi.cast(rffi.VOIDP, rffi.ptradd(rawmem, self.offset))
        # we return a reference to the inner struct, not a copy
        # autofree=False because it's still owned by the parent struct
        return W__StructInstance(w_structdescr, allocate=False, autofree=False,

    ## def get_void(self, w_ffitype):
    ##     ...

class SetFieldConverter(FromAppLevelConverter):
    A converter used by W__StructInstance to convert an app-level object to
    the corresponding low-level value and set the field of a structure.

    def __init__(self, space, rawmem, offset): = space
        self.rawmem = rawmem
        self.offset = offset

    def handle_signed(self, w_ffitype, w_obj, intval):
        libffi.struct_setfield_int(w_ffitype.get_ffitype(), self.rawmem, self.offset,

    def handle_unsigned(self, w_ffitype, w_obj, uintval):
        libffi.struct_setfield_int(w_ffitype.get_ffitype(), self.rawmem, self.offset,

    handle_pointer = handle_signed
    handle_char = handle_signed
    handle_unichar = handle_signed

    def handle_longlong(self, w_ffitype, w_obj, longlongval):
                                        self.rawmem, self.offset, longlongval)

    def handle_float(self, w_ffitype, w_obj, floatval):
                                     self.rawmem, self.offset, floatval)

    def handle_singlefloat(self, w_ffitype, w_obj, singlefloatval):
                                           self.rawmem, self.offset, singlefloatval)

    def handle_struct(self, w_ffitype, w_structinstance):
        rawmem = rffi.cast(rffi.CCHARP, self.rawmem)
        dst = rffi.cast(rffi.VOIDP, rffi.ptradd(rawmem, self.offset))
        src = w_structinstance.rawmem
        length = w_ffitype.sizeof()
        rffi.c_memcpy(dst, src, length)

    ## def handle_char_p(self, w_ffitype, w_obj, strval):
    ##     ...

    ## def handle_unichar_p(self, w_ffitype, w_obj, unicodeval):
    ##     ...

W__StructInstance.typedef = TypeDef(
    getaddr  = interp2app(W__StructInstance.getaddr),
    getfield = interp2app(W__StructInstance.getfield),
    setfield = interp2app(W__StructInstance.setfield),
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.