diff options
author | Travis E. Oliphant <oliphant@enthought.com> | 2007-08-18 11:21:56 (GMT) |
---|---|---|
committer | Travis E. Oliphant <oliphant@enthought.com> | 2007-08-18 11:21:56 (GMT) |
commit | b99f762f10edb2646a634c2290ecb064bd52e5c7 (patch) | |
tree | e0a354d42dccb18b7b2c99ed2733c135135a50af /Objects/unicodeobject.c | |
parent | 3de862df45480438dc6756103109ea9010d2825e (diff) | |
download | cpython-b99f762f10edb2646a634c2290ecb064bd52e5c7.zip cpython-b99f762f10edb2646a634c2290ecb064bd52e5c7.tar.gz cpython-b99f762f10edb2646a634c2290ecb064bd52e5c7.tar.bz2 |
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Diffstat (limited to 'Objects/unicodeobject.c')
-rw-r--r-- | Objects/unicodeobject.c | 63 |
1 files changed, 15 insertions, 48 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 5ee3347..157ea1c 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -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 * |