mirror of
https://github.com/python/cpython.git
synced 2025-09-10 10:47:34 +00:00
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.
This commit is contained in:
parent
c2b87a6dff
commit
612344f127
5 changed files with 98 additions and 8 deletions
|
@ -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':
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue