Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.

This commit is contained in:
Travis E. Oliphant 2007-08-18 11:21:56 +00:00
parent 3de862df45
commit b99f762f10
22 changed files with 1732 additions and 688 deletions

View file

@ -8108,57 +8108,26 @@ static PyMappingMethods unicode_as_mapping = {
(objobjargproc)0, /* mp_ass_subscript */
};
static Py_ssize_t
unicode_buffer_getreadbuf(PyUnicodeObject *self,
Py_ssize_t index,
const void **ptr)
{
if (index != 0) {
PyErr_SetString(PyExc_SystemError,
"accessing non-existent unicode segment");
return -1;
}
*ptr = (void *) self->str;
return PyUnicode_GET_DATA_SIZE(self);
}
static Py_ssize_t
unicode_buffer_getwritebuf(PyUnicodeObject *self, Py_ssize_t index,
const void **ptr)
{
PyErr_SetString(PyExc_TypeError,
"cannot use unicode as modifiable buffer");
return -1;
}
static int
unicode_buffer_getsegcount(PyUnicodeObject *self,
Py_ssize_t *lenp)
unicode_buffer_getbuffer(PyUnicodeObject *self, PyBuffer *view, int flags)
{
if (lenp)
*lenp = PyUnicode_GET_DATA_SIZE(self);
return 1;
}
static Py_ssize_t
unicode_buffer_getcharbuf(PyUnicodeObject *self,
Py_ssize_t index,
const void **ptr)
{
PyObject *str;
if (index != 0) {
PyErr_SetString(PyExc_SystemError,
"accessing non-existent unicode segment");
return -1;
if (flags & PyBUF_CHARACTER) {
PyObject *str;
str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL);
if (str == NULL) return -1;
return PyBuffer_FillInfo(view, (void *)PyString_AS_STRING(str),
PyString_GET_SIZE(str), 1, flags);
}
else {
return PyBuffer_FillInfo(view, (void *)self->str,
PyUnicode_GET_DATA_SIZE(self), 1, flags);
}
str = _PyUnicode_AsDefaultEncodedString((PyObject *)self, NULL);
if (str == NULL)
return -1;
*ptr = (void *) PyString_AS_STRING(str);
return PyString_GET_SIZE(str);
}
/* Helpers for PyUnicode_Format() */
static PyObject *
@ -8853,10 +8822,8 @@ PyObject *PyUnicode_Format(PyObject *format,
}
static PyBufferProcs unicode_as_buffer = {
(readbufferproc) unicode_buffer_getreadbuf,
(writebufferproc) unicode_buffer_getwritebuf,
(segcountproc) unicode_buffer_getsegcount,
(charbufferproc) unicode_buffer_getcharbuf,
(getbufferproc) unicode_buffer_getbuffer,
NULL,
};
static PyObject *