コード例 #1
0
ファイル: memoryobject.py プロジェクト: yuanleilei/pypy
def memory_realize(space, obj):
    """
    Creates the memory object in the interpreter
    """
    py_mem = rffi.cast(PyMemoryViewObject, obj)
    view = py_mem.c_view
    ndim = widen(view.c_ndim)
    shape = None
    if view.c_shape:
        shape = [view.c_shape[i] for i in range(ndim)]
    strides = None
    if view.c_strides:
        strides = [view.c_strides[i] for i in range(ndim)]
    format = 'B'
    if view.c_format:
        format = rffi.charp2str(view.c_format)
    buf = CPyBuffer(space,
                    view.c_buf,
                    view.c_len,
                    from_ref(space, view.c_obj),
                    format=format,
                    shape=shape,
                    strides=strides,
                    ndim=ndim,
                    itemsize=view.c_itemsize,
                    readonly=widen(view.c_readonly))
    # Ensure view.c_buf is released upon object finalization
    fq.register_finalizer(buf)
    # Allow subclassing W_MemeoryView
    w_type = from_ref(space, rffi.cast(PyObject, obj.c_ob_type))
    w_obj = space.allocate_instance(W_MemoryView, w_type)
    w_obj.__init__(buf)
    track_reference(space, obj, w_obj)
    return w_obj
コード例 #2
0
def wrap_getwritebuffer(space, w_self, w_args, func):
    func_target = rffi.cast(readbufferproc, func)
    py_type = _get_ob_type(space, w_self)
    rbp = rffi.cast(rffi.VOIDP, 0)
    if py_type.c_tp_as_buffer:
        rbp = rffi.cast(rffi.VOIDP, py_type.c_tp_as_buffer.c_bf_releasebuffer)
    with lltype.scoped_alloc(rffi.VOIDPP.TO, 1) as ptr:
        index = rffi.cast(Py_ssize_t, 0)
        size = generic_cpy_call(space, func_target, w_self, index, ptr)
        if size < 0:
            space.fromcache(State).check_and_raise_exception(always=True)
        view = CPyBuffer(space, ptr[0], size, w_self, readonly=False,
                               releasebufferproc=rbp)
        fq.register_finalizer(view)
        return space.newbuffer(CBuffer(view))
コード例 #3
0
def wrap_getbuffer(space, w_self, w_args, func):
    func_target = rffi.cast(getbufferproc, func)
    py_obj = make_ref(space, w_self)
    py_type = py_obj.c_ob_type
    rbp = rffi.cast(rffi.VOIDP, 0)
    if py_type.c_tp_as_buffer:
        rbp = rffi.cast(rffi.VOIDP, py_type.c_tp_as_buffer.c_bf_releasebuffer)
    decref(space, py_obj)
    with lltype.scoped_alloc(Py_buffer) as pybuf:
        _flags = 0
        if space.len_w(w_args) > 0:
            _flags = space.int_w(space.listview(w_args)[0])
        flags = rffi.cast(rffi.INT_real, _flags)
        size = generic_cpy_call(space, func_target, w_self, pybuf, flags)
        if widen(size) < 0:
            space.fromcache(State).check_and_raise_exception(always=True)
        ptr = pybuf.c_buf
        size = pybuf.c_len
        ndim = widen(pybuf.c_ndim)
        shape = None
        if pybuf.c_shape:
            shape = [pybuf.c_shape[i] for i in range(ndim)]
        strides = None
        if pybuf.c_strides:
            strides = [pybuf.c_strides[i] for i in range(ndim)]
        if pybuf.c_format:
            format = rffi.charp2str(pybuf.c_format)
        else:
            format = 'B'
        # the CPython docs mandates that you do an incref whenever you call
        # bf_getbuffer; so, we pass needs_decref=True to ensure that we don't
        # leak we release the buffer:
        # https://docs.python.org/3.5/c-api/typeobj.html#c.PyBufferProcs.bf_getbuffer
        buf = CPyBuffer(space,
                        ptr,
                        size,
                        w_self,
                        format=format,
                        ndim=ndim,
                        shape=shape,
                        strides=strides,
                        itemsize=pybuf.c_itemsize,
                        readonly=widen(pybuf.c_readonly),
                        needs_decref=True,
                        releasebufferproc=rbp)
        fq.register_finalizer(buf)
        return buf.wrap(space)