mirror of
https://github.com/python/cpython.git
synced 2025-07-12 13:55:34 +00:00
Implement PEP 393.
This commit is contained in:
parent
48d49497c5
commit
d63a3b8beb
102 changed files with 8153 additions and 5431 deletions
|
@ -546,9 +546,6 @@ convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
|||
|
||||
|
||||
|
||||
#define UNICODE_DEFAULT_ENCODING(arg) \
|
||||
_PyUnicode_AsDefaultEncodedString(arg)
|
||||
|
||||
/* Format an error message generated by convertsimple(). */
|
||||
|
||||
static char *
|
||||
|
@ -611,7 +608,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
|||
|
||||
const char *format = *p_format;
|
||||
char c = *format++;
|
||||
PyObject *uarg;
|
||||
char *sarg;
|
||||
|
||||
switch (c) {
|
||||
|
||||
|
@ -838,8 +835,11 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
|||
case 'C': {/* unicode char */
|
||||
int *p = va_arg(*p_va, int *);
|
||||
if (PyUnicode_Check(arg) &&
|
||||
PyUnicode_GET_SIZE(arg) == 1)
|
||||
*p = PyUnicode_AS_UNICODE(arg)[0];
|
||||
PyUnicode_GET_LENGTH(arg) == 1) {
|
||||
int kind = PyUnicode_KIND(arg);
|
||||
void *data = PyUnicode_DATA(arg);
|
||||
*p = PyUnicode_READ(kind, data, 0);
|
||||
}
|
||||
else
|
||||
return converterr("a unicode character", arg, msgbuf, bufsize);
|
||||
break;
|
||||
|
@ -889,13 +889,12 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
|||
if (c == 'z' && arg == Py_None)
|
||||
PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
|
||||
else if (PyUnicode_Check(arg)) {
|
||||
uarg = UNICODE_DEFAULT_ENCODING(arg);
|
||||
if (uarg == NULL)
|
||||
Py_ssize_t len;
|
||||
sarg = PyUnicode_AsUTF8AndSize(arg, &len);
|
||||
if (sarg == NULL)
|
||||
return converterr(CONV_UNICODE,
|
||||
arg, msgbuf, bufsize);
|
||||
PyBuffer_FillInfo(p, arg,
|
||||
PyBytes_AS_STRING(uarg), PyBytes_GET_SIZE(uarg),
|
||||
1, 0);
|
||||
PyBuffer_FillInfo(p, arg, sarg, len, 1, 0);
|
||||
}
|
||||
else { /* any buffer-like object */
|
||||
char *buf;
|
||||
|
@ -918,12 +917,13 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
|||
STORE_SIZE(0);
|
||||
}
|
||||
else if (PyUnicode_Check(arg)) {
|
||||
uarg = UNICODE_DEFAULT_ENCODING(arg);
|
||||
if (uarg == NULL)
|
||||
Py_ssize_t len;
|
||||
sarg = PyUnicode_AsUTF8AndSize(arg, &len);
|
||||
if (sarg == NULL)
|
||||
return converterr(CONV_UNICODE,
|
||||
arg, msgbuf, bufsize);
|
||||
*p = PyBytes_AS_STRING(uarg);
|
||||
STORE_SIZE(PyBytes_GET_SIZE(uarg));
|
||||
*p = sarg;
|
||||
STORE_SIZE(len);
|
||||
}
|
||||
else { /* any buffer-like object */
|
||||
/* XXX Really? */
|
||||
|
@ -937,22 +937,22 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
|||
} else {
|
||||
/* "s" or "z" */
|
||||
char **p = va_arg(*p_va, char **);
|
||||
uarg = NULL;
|
||||
Py_ssize_t len;
|
||||
sarg = NULL;
|
||||
|
||||
if (c == 'z' && arg == Py_None)
|
||||
*p = NULL;
|
||||
else if (PyUnicode_Check(arg)) {
|
||||
uarg = UNICODE_DEFAULT_ENCODING(arg);
|
||||
if (uarg == NULL)
|
||||
sarg = PyUnicode_AsUTF8AndSize(arg, &len);
|
||||
if (sarg == NULL)
|
||||
return converterr(CONV_UNICODE,
|
||||
arg, msgbuf, bufsize);
|
||||
*p = PyBytes_AS_STRING(uarg);
|
||||
*p = sarg;
|
||||
}
|
||||
else
|
||||
return converterr(c == 'z' ? "str or None" : "str",
|
||||
arg, msgbuf, bufsize);
|
||||
if (*p != NULL && uarg != NULL &&
|
||||
(Py_ssize_t) strlen(*p) != PyBytes_GET_SIZE(uarg))
|
||||
if (*p != NULL && sarg != NULL && (Py_ssize_t) strlen(*p) != len)
|
||||
return converterr(
|
||||
c == 'z' ? "str without null bytes or None"
|
||||
: "str without null bytes",
|
||||
|
@ -976,6 +976,8 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
|||
}
|
||||
else if (PyUnicode_Check(arg)) {
|
||||
*p = PyUnicode_AS_UNICODE(arg);
|
||||
if (*p == NULL)
|
||||
RETURN_ERR_OCCURRED;
|
||||
STORE_SIZE(PyUnicode_GET_SIZE(arg));
|
||||
}
|
||||
else
|
||||
|
@ -987,6 +989,8 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
|||
*p = NULL;
|
||||
else if (PyUnicode_Check(arg)) {
|
||||
*p = PyUnicode_AS_UNICODE(arg);
|
||||
if (*p == NULL)
|
||||
RETURN_ERR_OCCURRED;
|
||||
if (Py_UNICODE_strlen(*p) != PyUnicode_GET_SIZE(arg))
|
||||
return converterr(
|
||||
"str without null character or None",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue