diff options
author | Walter Dörwald <walter@livinglogic.de> | 2007-05-04 19:28:21 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2007-05-04 19:28:21 (GMT) |
commit | 612344f12774cbbefd735d9fcbfb2001fe187362 (patch) | |
tree | 3be6051c7e4ac7fe3a93372fa3d86bce06072e1f /Python | |
parent | c2b87a6dff1edade6542a484cb9b9419b254c1ed (diff) | |
download | cpython-612344f12774cbbefd735d9fcbfb2001fe187362.zip cpython-612344f12774cbbefd735d9fcbfb2001fe187362.tar.gz cpython-612344f12774cbbefd735d9fcbfb2001fe187362.tar.bz2 |
Change UnicodeDecodeError objects so that the 'object' attribute
is a bytes object.
Add 'y' and 'y#' format specifiers that work like 's' and 's#'
but only accept bytes objects.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/getargs.c | 27 | ||||
-rw-r--r-- | Python/modsupport.c | 33 |
2 files changed, 60 insertions, 0 deletions
diff --git a/Python/getargs.c b/Python/getargs.c index f7a6604..8331a18 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -819,6 +819,32 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, break; } + case 'y': {/* bytes */ + if (*format == '#') { + void **p = (void **)va_arg(*p_va, char **); + FETCH_SIZE; + + if (PyBytes_Check(arg)) { + *p = PyBytes_AS_STRING(arg); + STORE_SIZE(PyBytes_GET_SIZE(arg)); + } + else + return converterr("bytes", arg, msgbuf, bufsize); + format++; + } else { + char **p = va_arg(*p_va, char **); + + if (PyBytes_Check(arg)) + *p = PyBytes_AS_STRING(arg); + else + return converterr("bytes", arg, msgbuf, bufsize); + if ((Py_ssize_t)strlen(*p) != PyBytes_Size(arg)) + return converterr("bytes without null bytes", + arg, msgbuf, bufsize); + } + break; + } + case 'z': {/* string, may be NULL (None) */ if (*format == '#') { /* any buffer-like object */ void **p = (void **)va_arg(*p_va, char **); @@ -1595,6 +1621,7 @@ skipitem(const char **p_format, va_list *p_va, int flags) case 's': /* string */ case 'z': /* string or None */ + case 'y': /* bytes */ case 'u': /* unicode string */ case 't': /* buffer, read-only */ case 'w': /* buffer, read-write */ diff --git a/Python/modsupport.c b/Python/modsupport.c index af774f0..8f600dc 100644 --- a/Python/modsupport.c +++ b/Python/modsupport.c @@ -424,6 +424,39 @@ do_mkvalue(const char **p_format, va_list *p_va, int flags) return v; } + case 'y': + { + PyObject *v; + char *str = va_arg(*p_va, char *); + Py_ssize_t n; + if (**p_format == '#') { + ++*p_format; + if (flags & FLAG_SIZE_T) + n = va_arg(*p_va, Py_ssize_t); + else + n = va_arg(*p_va, int); + } + else + n = -1; + if (str == NULL) { + v = Py_None; + Py_INCREF(v); + } + else { + if (n < 0) { + size_t m = strlen(str); + if (m > PY_SSIZE_T_MAX) { + PyErr_SetString(PyExc_OverflowError, + "string too long for Python bytes"); + return NULL; + } + n = (Py_ssize_t)m; + } + v = PyBytes_FromStringAndSize(str, n); + } + return v; + } + case 'N': case 'S': case 'O': |