pypy / pypy / module / _cffi_backend /


from pypy.interpreter.error import operationerrfmt
from pypy.rpython.lltypesystem import rffi
from pypy.rlib.objectmodel import keepalive_until_here

from pypy.module._cffi_backend.ctypeobj import W_CType
from pypy.module._cffi_backend.ctypeprim import W_CTypePrimitiveChar
from pypy.module._cffi_backend import cdataobj, misc

class W_CTypePtrOrArray(W_CType):

    def __init__(self, space, size, extra, extra_position, ctitem,
        name, name_position = ctitem.insert_name(extra, extra_position)
        W_CType.__init__(self, space, size, name, name_position)
        # this is the "underlying type":
        #  - for pointers, it is the pointed-to type
        #  - for arrays, it is the array item type
        #  - for functions, it is the return type
        self.ctitem = ctitem
        self.can_cast_anything = could_cast_anything and ctitem.cast_anything

class W_CTypePtrBase(W_CTypePtrOrArray):
    # base class for both pointers and pointers-to-functions

    def cast(self, w_ob):
        space =
        ob = space.interpclass_w(w_ob)
        if (isinstance(ob, cdataobj.W_CData) and
                isinstance(ob.ctype, W_CTypePtrOrArray)):
            value = ob._cdata
            value = misc.as_unsigned_long_long(space, w_ob, strict=False)
            value = rffi.cast(rffi.CCHARP, value)
        return cdataobj.W_CData(space, value, self)

    def convert_to_object(self, cdata):
        ptrdata = rffi.cast(rffi.CCHARPP, cdata)[0]
        return cdataobj.W_CData(, ptrdata, self)

    def convert_from_object(self, cdata, w_ob):
        space =
        ob = space.interpclass_w(w_ob)
        if not isinstance(ob, cdataobj.W_CData):
            raise self._convert_error("compatible pointer", w_ob)
        other = ob.ctype
        if not isinstance(other, W_CTypePtrBase):
            from pypy.module._cffi_backend import ctypearray
            if isinstance(other, ctypearray.W_CTypeArray):
                other = other.ctptr
                raise self._convert_error("compatible pointer", w_ob)
        if self is not other:
            if not (self.can_cast_anything or other.can_cast_anything):
                raise self._convert_error("compatible pointer", w_ob)

        rffi.cast(rffi.CCHARPP, cdata)[0] = ob._cdata

    def _alignof(self):
        from pypy.module._cffi_backend import newtype
        return newtype.alignment_of_pointer

class W_CTypePointer(W_CTypePtrBase):

    def __init__(self, space, ctitem):
        from pypy.module._cffi_backend import ctypearray
        size = rffi.sizeof(rffi.VOIDP)
        if isinstance(ctitem, ctypearray.W_CTypeArray):
            extra = "(*)"    # obscure case: see test_array_add
            extra = " *"
        W_CTypePtrBase.__init__(self, space, size, extra, 2, ctitem)

    def str(self, cdataobj):
        if isinstance(self.ctitem, W_CTypePrimitiveChar):
            if not cdataobj._cdata:
                space =
                raise operationerrfmt(space.w_RuntimeError,
                                      "cannot use str() on %s",
            s = rffi.charp2str(cdataobj._cdata)
        return W_CTypePtrOrArray.str(self, cdataobj)

    def newp(self, w_init):
        from pypy.module._cffi_backend import ctypeprim
        space =
        ctitem = self.ctitem
        datasize = ctitem.size
        if datasize < 0:
            raise operationerrfmt(space.w_TypeError,
                "cannot instantiate ctype '%s' of unknown size",
        if isinstance(ctitem, W_CTypePrimitiveChar):
            datasize *= 2       # forcefully add a null character
        cdata = cdataobj.W_CDataOwn(space, datasize, self)
        if not space.is_w(w_init, space.w_None):
            ctitem.convert_from_object(cdata._cdata, w_init)
        return cdata

    def _check_subscript_index(self, w_cdata, i):
        if isinstance(w_cdata, cdataobj.W_CDataOwn) and i != 0:
            space =
            raise operationerrfmt(space.w_IndexError,
                                  "cdata '%s' can only be indexed by 0",

    def add(self, cdata, i):
        space =
        ctitem = self.ctitem
        if ctitem.size < 0:
            raise operationerrfmt(space.w_TypeError,
                                  "ctype '%s' points to items of unknown size",
        p = rffi.ptradd(cdata, i * self.ctitem.size)
        return cdataobj.W_CData(space, p, self)