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

@ -1672,44 +1672,38 @@ getstring(PyObject* string, Py_ssize_t* p_length, int* p_charsize)
Py_ssize_t size, bytes;
int charsize;
void* ptr;
#if defined(HAVE_UNICODE)
if (PyUnicode_Check(string)) {
/* unicode strings doesn't always support the buffer interface */
ptr = (void*) PyUnicode_AS_DATA(string);
bytes = PyUnicode_GET_DATA_SIZE(string);
size = PyUnicode_GET_SIZE(string);
charsize = sizeof(Py_UNICODE);
} else {
#endif
PyBuffer view;
/* get pointer to string buffer */
view.len = -1;
buffer = Py_Type(string)->tp_as_buffer;
if (!buffer || !buffer->bf_getreadbuffer || !buffer->bf_getsegcount ||
buffer->bf_getsegcount(string, NULL) != 1) {
PyErr_SetString(PyExc_TypeError, "expected string or buffer");
return NULL;
if (!buffer || !buffer->bf_getbuffer ||
(*buffer->bf_getbuffer)(string, &view, PyBUF_SIMPLE) < 0) {
PyErr_SetString(PyExc_TypeError, "expected string or buffer");
return NULL;
}
/* determine buffer size */
bytes = buffer->bf_getreadbuffer(string, 0, &ptr);
bytes = view.len;
ptr = view.buf;
/* Release the buffer immediately --- possibly dangerous
but doing something else would require some re-factoring
*/
PyObject_ReleaseBuffer(string, &view);
if (bytes < 0) {
PyErr_SetString(PyExc_TypeError, "buffer has negative size");
return NULL;
}
/* determine character size */
#if PY_VERSION_HEX >= 0x01060000
size = PyObject_Size(string);
#else
size = PyObject_Length(string);
#endif
if (PyString_Check(string) || bytes == size)
charsize = 1;
#if defined(HAVE_UNICODE)
else if (bytes == (Py_ssize_t) (size * sizeof(Py_UNICODE)))
else if (bytes == (Py_ssize_t) (size * sizeof(Py_UNICODE)))
charsize = sizeof(Py_UNICODE);
#endif
else {
@ -1717,13 +1711,13 @@ getstring(PyObject* string, Py_ssize_t* p_length, int* p_charsize)
return NULL;
}
#if defined(HAVE_UNICODE)
}
#endif
*p_length = size;
*p_charsize = charsize;
if (ptr == NULL) {
PyErr_SetString(PyExc_ValueError,
"Buffer is NULL");
}
return ptr;
}