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 /Python | |
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 'Python')
-rw-r--r-- | Python/bltinmodule.c | 1 | ||||
-rw-r--r-- | Python/getargs.c | 55 | ||||
-rw-r--r-- | Python/marshal.c | 12 |
3 files changed, 48 insertions, 20 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 14fb349..ce8b7f5 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -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 diff --git a/Python/getargs.c b/Python/getargs.c index ce1fef7..a0aa872 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -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; } diff --git a/Python/marshal.c b/Python/marshal.c index 975ec8d..c5952b2 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -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); |