[Python-checkins] cpython (2.7): Issue #10211 : Buffer object should support the new buffer interface.
kristjan.jonsson
python-checkins at python.org
Wed Mar 20 01:40:32 CET 2013
http://hg.python.org/cpython/rev/6b3217b96a77
changeset: 82813:6b3217b96a77
branch: 2.7
user: Kristján Valur Jónsson <sweskman at gmail.com>
date: Tue Mar 19 16:50:51 2013 -0700
summary:
Issue #10211 : Buffer object should support the new buffer interface.
files:
Lib/test/test_buffer.py | 8 ++++++++
Objects/bufferobject.c | 15 +++++++++++++--
2 files changed, 21 insertions(+), 2 deletions(-)
diff --git a/Lib/test/test_buffer.py b/Lib/test/test_buffer.py
--- a/Lib/test/test_buffer.py
+++ b/Lib/test/test_buffer.py
@@ -21,6 +21,14 @@
self.assertEqual(b[start:stop:step],
s[start:stop:step])
+ def test_newbuffer_interface(self):
+ # Test that the buffer object has the new buffer interface
+ # as used by the memoryview object
+ s = "".join(chr(c) for c in list(range(255, -1, -1)))
+ b = buffer(s)
+ m = memoryview(b) # Should not raise an exception
+ self.assertEqual(m.tobytes(), s)
+
def test_main():
with test_support.check_py3k_warnings(("buffer.. not supported",
diff --git a/Objects/bufferobject.c b/Objects/bufferobject.c
--- a/Objects/bufferobject.c
+++ b/Objects/bufferobject.c
@@ -802,6 +802,16 @@
return size;
}
+static int buffer_getbuffer(PyBufferObject *self, Py_buffer *buf, int flags)
+{
+ void *ptr;
+ Py_ssize_t size;
+ if (!get_buf(self, &ptr, &size, ANY_BUFFER))
+ return -1;
+ return PyBuffer_FillInfo(buf, (PyObject*)self, ptr, size,
+ self->b_readonly, flags);
+}
+
static PySequenceMethods buffer_as_sequence = {
(lenfunc)buffer_length, /*sq_length*/
(binaryfunc)buffer_concat, /*sq_concat*/
@@ -823,6 +833,7 @@
(writebufferproc)buffer_getwritebuf,
(segcountproc)buffer_getsegcount,
(charbufferproc)buffer_getcharbuf,
+ (getbufferproc)buffer_getbuffer,
};
PyTypeObject PyBuffer_Type = {
@@ -845,7 +856,7 @@
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
&buffer_as_buffer, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GETCHARBUFFER, /* tp_flags */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GETCHARBUFFER | Py_TPFLAGS_HAVE_NEWBUFFER, /* tp_flags */
buffer_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
@@ -864,4 +875,4 @@
0, /* tp_init */
0, /* tp_alloc */
buffer_new, /* tp_new */
-};
+};
\ No newline at end of file
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list