Author: Maciej Fijalkowski <fij...@gmail.com> Branch: pyarg-parsetuple-s-star-buffer Changeset: r50785:650faf80d8d2 Date: 2011-12-20 23:11 +0100 http://bitbucket.org/pypy/pypy/changeset/650faf80d8d2/
Log: merge diff --git a/pypy/module/cpyext/bufferobject.py b/pypy/module/cpyext/bufferobject.py --- a/pypy/module/cpyext/bufferobject.py +++ b/pypy/module/cpyext/bufferobject.py @@ -2,7 +2,7 @@ from pypy.module.cpyext.api import ( cpython_api, Py_ssize_t, cpython_struct, bootstrap_function, PyObjectFields, PyObject) -from pypy.module.cpyext.pyobject import make_typedescr +from pypy.module.cpyext.pyobject import make_typedescr, Py_DecRef from pypy.interpreter.buffer import Buffer, StringBuffer, SubBuffer @@ -25,7 +25,7 @@ make_typedescr(space.gettypefor(Buffer).instancetypedef, basestruct=PyBufferObject.TO, attach=buffer_attach, - # dealloc=buffer_dealloc, + dealloc=buffer_dealloc, realize=buffer_realize) def buffer_attach(space, py_obj, w_obj): @@ -57,6 +57,10 @@ -# @cpython_api([PyObject], lltype.Void, external=False) -# def buffer_dealloc(space, py_obj): - +@cpython_api([PyObject], lltype.Void, external=False) +def buffer_dealloc(space, py_obj): + py_buf = rffi.cast(PyBufferObject, py_obj) + Py_DecRef(space, py_buf.c_b_base) + rffi.free_charp(py_buf.c_b_ptr) + from pypy.module.cpyext.object import PyObject_dealloc + PyObject_dealloc(space, py_obj) diff --git a/pypy/module/cpyext/test/test_getargs.py b/pypy/module/cpyext/test/test_getargs.py --- a/pypy/module/cpyext/test/test_getargs.py +++ b/pypy/module/cpyext/test/test_getargs.py @@ -137,13 +137,11 @@ if (!PyArg_ParseTuple(args, "s*", &buf)) { return NULL; } - printf("OH NO %s %d\\n", buf.buf, buf.len); - fflush(stdout); result = PyString_FromStringAndSize(buf.buf, buf.len); PyBuffer_Release(&buf); return result; ''') - assert buffer('foo\0bar\0baz') == pybuffer(buffer('foo\0bar\0baz')) + assert 'foo\0bar\0baz' == pybuffer(buffer('foo\0bar\0baz')) def test_pyarg_parse_charbuf_and_length(self): diff --git a/pypy/module/cpyext/typeobject.py b/pypy/module/cpyext/typeobject.py --- a/pypy/module/cpyext/typeobject.py +++ b/pypy/module/cpyext/typeobject.py @@ -28,6 +28,7 @@ PyNumberMethods, PyMappingMethods, PySequenceMethods, PyBufferProcs) from pypy.module.cpyext.slotdefs import ( slotdefs_for_tp_slots, slotdefs_for_wrappers, get_slot_tp_function) +from pypy.interpreter.buffer import Buffer from pypy.interpreter.error import OperationError from pypy.rlib.rstring import rsplit from pypy.rlib.objectmodel import specialize @@ -418,8 +419,20 @@ Py_DecRef(space, pyref) return space.len_w(w_str) +@cpython_api([PyObject, Py_ssize_t, rffi.VOIDPP], lltype.Signed, + external=False, error=-1) +def buf_getreadbuffer(space, pyref, segment, ref): + from pypy.module.cpyext.bufferobject import PyBufferObject + if segment != 0: + raise OperationError(space.w_SystemError, space.wrap + ("accessing non-existent string segment")) + py_buf = rffi.cast(PyBufferObject, pyref) + ref[0] = py_buf.c_b_ptr + #Py_DecRef(space, pyref) + return py_buf.c_b_size + def setup_string_buffer_procs(space, pto): - c_buf = lltype.malloc(PyBufferProcs, flavor='raw', zero=True) + c_buf = lltype.malloc(PyBufferProcs, flavor='raw', zero=True, immortal=True) c_buf.c_bf_getsegcount = llhelper(str_segcount.api_func.functype, str_segcount.api_func.get_wrapper(space)) c_buf.c_bf_getreadbuffer = llhelper(str_getreadbuffer.api_func.functype, @@ -429,6 +442,14 @@ pto.c_tp_as_buffer = c_buf pto.c_tp_flags |= Py_TPFLAGS_HAVE_GETCHARBUFFER +def setup_buffer_buffer_procs(space, pto): + c_buf = lltype.malloc(PyBufferProcs, flavor='raw', zero=True, immortal=True) + c_buf.c_bf_getsegcount = llhelper(str_segcount.api_func.functype, + str_segcount.api_func.get_wrapper(space)) + c_buf.c_bf_getreadbuffer = llhelper(buf_getreadbuffer.api_func.functype, + buf_getreadbuffer.api_func.get_wrapper(space)) + pto.c_tp_as_buffer = c_buf + @cpython_api([PyObject], lltype.Void, external=False) def type_dealloc(space, obj): from pypy.module.cpyext.object import PyObject_dealloc @@ -484,6 +505,8 @@ # buffer protocol if space.is_w(w_type, space.w_str): setup_string_buffer_procs(space, pto) + if space.is_w(w_type, space.gettypefor(Buffer)): + setup_buffer_buffer_procs(space, pto) pto.c_tp_free = llhelper(PyObject_Del.api_func.functype, PyObject_Del.api_func.get_wrapper(space)) _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit