summaryrefslogtreecommitdiffstats
path: root/Python/getargs.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/getargs.c')
-rw-r--r--Python/getargs.c128
1 files changed, 69 insertions, 59 deletions
diff --git a/Python/getargs.c b/Python/getargs.c
index 600941d..77f27be 100644
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -310,20 +310,18 @@ vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
if (max == 0) {
if (args == NULL)
return 1;
- PyOS_snprintf(msgbuf, sizeof(msgbuf),
- "%.200s%s takes no arguments",
- fname==NULL ? "function" : fname,
- fname==NULL ? "" : "()");
- PyErr_SetString(PyExc_TypeError, msgbuf);
+ PyErr_Format(PyExc_TypeError,
+ "%.200s%s takes no arguments",
+ fname==NULL ? "function" : fname,
+ fname==NULL ? "" : "()");
return 0;
}
else if (min == 1 && max == 1) {
if (args == NULL) {
- PyOS_snprintf(msgbuf, sizeof(msgbuf),
- "%.200s%s takes at least one argument",
- fname==NULL ? "function" : fname,
- fname==NULL ? "" : "()");
- PyErr_SetString(PyExc_TypeError, msgbuf);
+ PyErr_Format(PyExc_TypeError,
+ "%.200s%s takes at least one argument",
+ fname==NULL ? "function" : fname,
+ fname==NULL ? "" : "()");
return 0;
}
msg = convertitem(args, &format, p_va, flags, levels,
@@ -349,20 +347,18 @@ vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
len = PyTuple_GET_SIZE(args);
if (len < min || max < len) {
- if (message == NULL) {
- PyOS_snprintf(msgbuf, sizeof(msgbuf),
- "%.150s%s takes %s %d argument%s "
- "(%ld given)",
- fname==NULL ? "function" : fname,
- fname==NULL ? "" : "()",
- min==max ? "exactly"
- : len < min ? "at least" : "at most",
- len < min ? min : max,
- (len < min ? min : max) == 1 ? "" : "s",
- Py_SAFE_DOWNCAST(len, Py_ssize_t, long));
- message = msgbuf;
- }
- PyErr_SetString(PyExc_TypeError, message);
+ if (message == NULL)
+ PyErr_Format(PyExc_TypeError,
+ "%.150s%s takes %s %d argument%s (%ld given)",
+ fname==NULL ? "function" : fname,
+ fname==NULL ? "" : "()",
+ min==max ? "exactly"
+ : len < min ? "at least" : "at most",
+ len < min ? min : max,
+ (len < min ? min : max) == 1 ? "" : "s",
+ Py_SAFE_DOWNCAST(len, Py_ssize_t, long));
+ else
+ PyErr_SetString(PyExc_TypeError, message);
return 0;
}
@@ -550,9 +546,6 @@ convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
-#define UNICODE_DEFAULT_ENCODING(arg) \
- _PyUnicode_AsDefaultEncodedString(arg, NULL)
-
/* Format an error message generated by convertsimple(). */
static char *
@@ -615,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) {
@@ -832,6 +825,8 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
char *p = va_arg(*p_va, char *);
if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
*p = PyBytes_AS_STRING(arg)[0];
+ else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1)
+ *p = PyByteArray_AS_STRING(arg)[0];
else
return converterr("a byte string of length 1", arg, msgbuf, bufsize);
break;
@@ -839,11 +834,21 @@ 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];
- else
+ int kind;
+ void *data;
+
+ if (!PyUnicode_Check(arg))
return converterr("a unicode character", arg, msgbuf, bufsize);
+
+ if (PyUnicode_READY(arg))
+ RETURN_ERR_OCCURRED;
+
+ if (PyUnicode_GET_LENGTH(arg) != 1)
+ return converterr("a unicode character", arg, msgbuf, bufsize);
+
+ kind = PyUnicode_KIND(arg);
+ data = PyUnicode_DATA(arg);
+ *p = PyUnicode_READ(kind, data, 0);
break;
}
@@ -891,13 +896,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;
@@ -920,12 +924,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? */
@@ -939,25 +944,25 @@ 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",
+ c == 'z' ? "str without null characters or None"
+ : "str without null characters",
arg, msgbuf, bufsize);
}
break;
@@ -966,9 +971,10 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
case 'u': /* raw unicode buffer (Py_UNICODE *) */
case 'Z': /* raw unicode buffer or None */
{
+ Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
+
if (*format == '#') { /* any buffer-like object */
/* "s#" or "Z#" */
- Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
FETCH_SIZE;
if (c == 'Z' && arg == Py_None) {
@@ -976,23 +982,27 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
STORE_SIZE(0);
}
else if (PyUnicode_Check(arg)) {
- *p = PyUnicode_AS_UNICODE(arg);
- STORE_SIZE(PyUnicode_GET_SIZE(arg));
+ Py_ssize_t len;
+ *p = PyUnicode_AsUnicodeAndSize(arg, &len);
+ if (*p == NULL)
+ RETURN_ERR_OCCURRED;
+ STORE_SIZE(len);
}
else
return converterr("str or None", arg, msgbuf, bufsize);
format++;
} else {
/* "s" or "Z" */
- Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
-
if (c == 'Z' && arg == Py_None)
*p = NULL;
else if (PyUnicode_Check(arg)) {
- *p = PyUnicode_AS_UNICODE(arg);
- if (Py_UNICODE_strlen(*p) != PyUnicode_GET_SIZE(arg))
+ Py_ssize_t len;
+ *p = PyUnicode_AsUnicodeAndSize(arg, &len);
+ if (*p == NULL)
+ RETURN_ERR_OCCURRED;
+ if (Py_UNICODE_strlen(*p) != len)
return converterr(
- "str without null character or None",
+ "str without null characters or None",
arg, msgbuf, bufsize);
} else
return converterr(c == 'Z' ? "str or None" : "str",
@@ -1459,8 +1469,8 @@ vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
nargs = PyTuple_GET_SIZE(args);
nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
if (nargs + nkeywords > len) {
- PyErr_Format(PyExc_TypeError, "%s%s takes at most %d "
- "argument%s (%d given)",
+ PyErr_Format(PyExc_TypeError,
+ "%s%s takes at most %d argument%s (%d given)",
(fname == NULL) ? "function" : fname,
(fname == NULL) ? "" : "()",
len,