[pypy-commit] pypy cpyext-old-buffers: Add tests for old buffer protocol, and add missing method for buffer objects.
devin.jeanpierre
pypy.commits at gmail.com
Fri May 13 13:52:42 EDT 2016
Author: Devin Jeanpierre <jeanpierreda at gmail.com>
Branch: cpyext-old-buffers
Changeset: r84424:878811dabd46
Date: 2016-05-10 08:12 -0700
http://bitbucket.org/pypy/pypy/changeset/878811dabd46/
Log: Add tests for old buffer protocol, and add missing method for buffer
objects.
diff --git a/pypy/module/cpyext/test/test_abstract.py b/pypy/module/cpyext/test/test_abstract.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/cpyext/test/test_abstract.py
@@ -0,0 +1,57 @@
+from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
+
+
+class AppTestBufferProtocol(AppTestCpythonExtensionBase):
+ """Tests for the old buffer protocol."""
+ def w_get_buffer_support(self):
+ return self.import_extension('foo', [
+ ("charbuffer_as_string", "METH_O",
+ """
+ char *ptr;
+ Py_ssize_t size;
+ if (PyObject_AsCharBuffer(args, &ptr, &size) < 0)
+ return NULL;
+ return PyString_FromStringAndSize(ptr, size);
+ """),
+ ("check_readbuffer", "METH_O",
+ """
+ return PyBool_FromLong(PyObject_CheckReadBuffer(args));
+ """),
+ ("readbuffer_as_string", "METH_O",
+ """
+ const void *ptr;
+ Py_ssize_t size;
+ if (PyObject_AsReadBuffer(args, &ptr, &size) < 0)
+ return NULL;
+ return PyString_FromStringAndSize((char*)ptr, size);
+ """),
+ ("writebuffer_as_string", "METH_O",
+ """
+ void *ptr;
+ Py_ssize_t size;
+ if (PyObject_AsWriteBuffer(args, &ptr, &size) < 0)
+ return NULL;
+ return PyString_FromStringAndSize((char*)ptr, size);
+ """),
+ ])
+
+ def test_string(self):
+ buffer_support = self.get_buffer_support()
+
+ s = 'a\0x'
+
+ assert buffer_support.check_readbuffer(s)
+ assert s == buffer_support.readbuffer_as_string(s)
+ assert raises(TypeError, buffer_support.writebuffer_as_string, s)
+ assert s == buffer_support.charbuffer_as_string(s)
+
+ def test_buffer(self):
+ buffer_support = self.get_buffer_support()
+
+ s = 'a\0x'
+ buf = buffer(s)
+
+ assert buffer_support.check_readbuffer(buf)
+ assert s == buffer_support.readbuffer_as_string(buf)
+ assert raises(TypeError, buffer_support.writebuffer_as_string, buf)
+ assert s == buffer_support.charbuffer_as_string(buf)
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
@@ -486,6 +486,18 @@
#Py_DecRef(space, pyref)
return py_buf.c_b_size
+ at cpython_api([PyObject, Py_ssize_t, rffi.CCHARPP], lltype.Signed,
+ header=None, error=-1)
+def buf_getcharbuffer(space, pyref, segment, ref):
+ from pypy.module.cpyext.bufferobject import PyBufferObject
+ if segment != 0:
+ raise oefmt(space.w_SystemError,
+ "accessing non-existent string segment")
+ py_buf = rffi.cast(PyBufferObject, pyref)
+ ref[0] = rffi.cast(rffi.CCHARP, 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)
lltype.render_immortal(c_buf)
@@ -505,6 +517,8 @@
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))
+ c_buf.c_bf_getcharbuffer = llhelper(buf_getcharbuffer.api_func.functype,
+ buf_getcharbuffer.api_func.get_wrapper(space))
pto.c_tp_as_buffer = c_buf
@cpython_api([PyObject], lltype.Void, header=None)
More information about the pypy-commit
mailing list