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

@ -1704,6 +1704,7 @@ _PyBuiltin_Init(void)
SETBUILTIN("basestring", &PyBaseString_Type);
SETBUILTIN("bool", &PyBool_Type);
SETBUILTIN("buffer", &PyBuffer_Type);
SETBUILTIN("memoryview", &PyMemoryView_Type);
SETBUILTIN("bytes", &PyBytes_Type);
SETBUILTIN("classmethod", &PyClassMethod_Type);
#ifndef WITHOUT_COMPLEX

View file

@ -1179,21 +1179,31 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
void **p = va_arg(*p_va, void **);
PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
int count;
int temp=-1;
PyBuffer view;
if (pb == NULL ||
pb->bf_getwritebuffer == NULL ||
pb->bf_getsegcount == NULL)
return converterr("read-write buffer", arg, msgbuf, bufsize);
if ((*pb->bf_getsegcount)(arg, NULL) != 1)
pb->bf_getbuffer == NULL ||
((temp = (*pb->bf_getbuffer)(arg, &view,
PyBUF_SIMPLE)) != 0) ||
view.readonly == 1) {
if (temp==0 && pb->bf_releasebuffer != NULL) {
(*pb->bf_releasebuffer)(arg, &view);
}
return converterr("single-segment read-write buffer",
arg, msgbuf, bufsize);
if ((count = pb->bf_getwritebuffer(arg, 0, p)) < 0)
}
if ((count = view.len) < 0)
return converterr("(unspecified)", arg, msgbuf, bufsize);
*p = view.buf;
if (*format == '#') {
FETCH_SIZE;
STORE_SIZE(count);
format++;
}
if (pb->bf_releasebuffer != NULL)
(*pb->bf_releasebuffer)(arg, &view);
break;
}
@ -1201,23 +1211,27 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
char **p = va_arg(*p_va, char **);
PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
int count;
PyBuffer view;
if (*format++ != '#')
return converterr(
"invalid use of 't' format character",
arg, msgbuf, bufsize);
if (pb == NULL || pb->bf_getcharbuffer == NULL ||
pb->bf_getsegcount == NULL)
if (pb == NULL || pb->bf_getbuffer == NULL)
return converterr(
"string or read-only character buffer",
arg, msgbuf, bufsize);
if (pb->bf_getsegcount(arg, NULL) != 1)
return converterr(
"string or single-segment read-only buffer",
arg, msgbuf, bufsize);
if ((*pb->bf_getbuffer)(arg, &view, PyBUF_CHARACTER) != 0)
return converterr("string or single-segment read-only buffer",
arg, msgbuf, bufsize);
count = pb->bf_getcharbuffer(arg, 0, p);
count = view.len;
*p = view.buf;
/* XXX : shouldn't really release buffer, but it should be O.K.
*/
if (pb->bf_releasebuffer != NULL)
(*pb->bf_releasebuffer)(arg, &view);
if (count < 0)
return converterr("(unspecified)", arg, msgbuf, bufsize);
{
@ -1241,19 +1255,24 @@ convertbuffer(PyObject *arg, void **p, char **errmsg)
{
PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Py_ssize_t count;
PyBuffer view;
*errmsg = NULL;
*p = NULL;
if (pb == NULL ||
pb->bf_getreadbuffer == NULL ||
pb->bf_getsegcount == NULL) {
pb->bf_getbuffer == NULL) {
*errmsg = "string or read-only buffer";
return -1;
}
if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
if ((*pb->bf_getbuffer)(arg, &view, PyBUF_SIMPLE) != 0) {
*errmsg = "string or single-segment read-only buffer";
return -1;
}
if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
*errmsg = "(unspecified)";
}
count = view.len;
*p = view.buf;
if (pb->bf_releasebuffer != NULL)
(*pb->bf_releasebuffer)(arg, &view);
return count;
}

View file

@ -358,12 +358,18 @@ w_object(PyObject *v, WFILE *p)
w_long(co->co_firstlineno, p);
w_object(co->co_lnotab, p);
}
else if (PyObject_CheckReadBuffer(v)) {
else if (PyObject_CheckBuffer(v)) {
/* Write unknown buffer-style objects as a string */
char *s;
PyBufferProcs *pb = v->ob_type->tp_as_buffer;
PyBuffer view;
if ((*pb->bf_getbuffer)(v, &view, PyBUF_SIMPLE) != 0) {
w_byte(TYPE_UNKNOWN, p);
p->error = 1;
}
w_byte(TYPE_STRING, p);
n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s);
n = view.len;
s = view.buf;
if (n > INT_MAX) {
p->depth--;
p->error = 1;
@ -371,6 +377,8 @@ w_object(PyObject *v, WFILE *p)
}
w_long((long)n, p);
w_string(s, (int)n, p);
if (pb->bf_releasebuffer != NULL)
(*pb->bf_releasebuffer)(v, &view);
}
else {
w_byte(TYPE_UNKNOWN, p);