diff options
Diffstat (limited to 'Python/getargs.c')
-rw-r--r-- | Python/getargs.c | 349 |
1 files changed, 285 insertions, 64 deletions
diff --git a/Python/getargs.c b/Python/getargs.c index ba1a9d4214..59f0fdabb7 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -372,14 +372,14 @@ vgetargs1_impl(PyObject *compat_args, PyObject *const *stack, Py_ssize_t nargs, if (nargs < min || max < nargs) { if (message == NULL) PyErr_Format(PyExc_TypeError, - "%.150s%s takes %s %d argument%s (%ld given)", + "%.150s%s takes %s %d argument%s (%zd given)", fname==NULL ? "function" : fname, fname==NULL ? "" : "()", min==max ? "exactly" : nargs < min ? "at least" : "at most", nargs < min ? min : max, (nargs < min ? min : max) == 1 ? "" : "s", - Py_SAFE_DOWNCAST(nargs, Py_ssize_t, long)); + nargs); else PyErr_SetString(PyExc_TypeError, message); return cleanreturn(0, &freelist); @@ -681,7 +681,13 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, /* For # codes */ #define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\ if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \ - else q=va_arg(*p_va, int*); + else { \ + if (PyErr_WarnEx(PyExc_DeprecationWarning, \ + "PY_SSIZE_T_CLEAN will be required for '#' formats", 1)) { \ + return NULL; \ + } \ + q=va_arg(*p_va, int*); \ + } #define STORE_SIZE(s) \ if (flags & FLAG_SIZE_T) \ *q2=s; \ @@ -1741,7 +1747,7 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format, else { PyErr_Format(PyExc_TypeError, "%.200s%s takes %s %d positional argument%s" - " (%d given)", + " (%zd given)", (fname == NULL) ? "function" : fname, (fname == NULL) ? "" : "()", (min != INT_MAX) ? "at most" : "exactly", @@ -1826,7 +1832,7 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format, if (skip) { PyErr_Format(PyExc_TypeError, "%.200s%s takes %s %d positional argument%s" - " (%d given)", + " (%zd given)", (fname == NULL) ? "function" : fname, (fname == NULL) ? "" : "()", (Py_MIN(pos, min) < i) ? "at least" : "exactly", @@ -1905,24 +1911,11 @@ parser_init(struct _PyArg_Parser *parser) int i, len, min, max, nkw; PyObject *kwtuple; - assert(parser->format != NULL); assert(parser->keywords != NULL); if (parser->kwtuple != NULL) { return 1; } - /* grab the function name or custom error msg first (mutually exclusive) */ - parser->fname = strchr(parser->format, ':'); - if (parser->fname) { - parser->fname++; - parser->custom_msg = NULL; - } - else { - parser->custom_msg = strchr(parser->format,';'); - if (parser->custom_msg) - parser->custom_msg++; - } - keywords = parser->keywords; /* scan keywords and count the number of positional-only parameters */ for (i = 0; keywords[i] && !*keywords[i]; i++) { @@ -1938,60 +1931,74 @@ parser_init(struct _PyArg_Parser *parser) } len = i; - min = max = INT_MAX; format = parser->format; - for (i = 0; i < len; i++) { - if (*format == '|') { - if (min != INT_MAX) { - PyErr_SetString(PyExc_SystemError, - "Invalid format string (| specified twice)"); - return 0; + if (format) { + /* grab the function name or custom error msg first (mutually exclusive) */ + parser->fname = strchr(parser->format, ':'); + if (parser->fname) { + parser->fname++; + parser->custom_msg = NULL; + } + else { + parser->custom_msg = strchr(parser->format,';'); + if (parser->custom_msg) + parser->custom_msg++; + } + + min = max = INT_MAX; + for (i = 0; i < len; i++) { + if (*format == '|') { + if (min != INT_MAX) { + PyErr_SetString(PyExc_SystemError, + "Invalid format string (| specified twice)"); + return 0; + } + if (max != INT_MAX) { + PyErr_SetString(PyExc_SystemError, + "Invalid format string ($ before |)"); + return 0; + } + min = i; + format++; } - if (max != INT_MAX) { - PyErr_SetString(PyExc_SystemError, - "Invalid format string ($ before |)"); - return 0; + if (*format == '$') { + if (max != INT_MAX) { + PyErr_SetString(PyExc_SystemError, + "Invalid format string ($ specified twice)"); + return 0; + } + if (i < parser->pos) { + PyErr_SetString(PyExc_SystemError, + "Empty parameter name after $"); + return 0; + } + max = i; + format++; } - min = i; - format++; - } - if (*format == '$') { - if (max != INT_MAX) { - PyErr_SetString(PyExc_SystemError, - "Invalid format string ($ specified twice)"); + if (IS_END_OF_FORMAT(*format)) { + PyErr_Format(PyExc_SystemError, + "More keyword list entries (%d) than " + "format specifiers (%d)", len, i); return 0; } - if (i < parser->pos) { - PyErr_SetString(PyExc_SystemError, - "Empty parameter name after $"); + + msg = skipitem(&format, NULL, 0); + if (msg) { + PyErr_Format(PyExc_SystemError, "%s: '%s'", msg, + format); return 0; } - max = i; - format++; - } - if (IS_END_OF_FORMAT(*format)) { - PyErr_Format(PyExc_SystemError, - "More keyword list entries (%d) than " - "format specifiers (%d)", len, i); - return 0; } + parser->min = Py_MIN(min, len); + parser->max = Py_MIN(max, len); - msg = skipitem(&format, NULL, 0); - if (msg) { - PyErr_Format(PyExc_SystemError, "%s: '%s'", msg, - format); + if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) { + PyErr_Format(PyExc_SystemError, + "more argument specifiers than keyword list entries " + "(remaining format:'%s')", format); return 0; } } - parser->min = Py_MIN(min, len); - parser->max = Py_MIN(max, len); - - if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) { - PyErr_Format(PyExc_SystemError, - "more argument specifiers than keyword list entries " - "(remaining format:'%s')", format); - return 0; - } nkw = len - parser->pos; kwtuple = PyTuple_New(nkw); @@ -2134,10 +2141,10 @@ vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs, } else { PyErr_Format(PyExc_TypeError, - "%.200s%s takes %s %d positional argument%s (%d given)", + "%.200s%s takes %s %d positional argument%s (%zd given)", (parser->fname == NULL) ? "function" : parser->fname, (parser->fname == NULL) ? "" : "()", - (parser->min != INT_MAX) ? "at most" : "exactly", + (parser->min < parser->max) ? "at most" : "exactly", parser->max, parser->max == 1 ? "" : "s", nargs); @@ -2194,7 +2201,7 @@ vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs, Py_ssize_t min = Py_MIN(pos, parser->min); PyErr_Format(PyExc_TypeError, "%.200s%s takes %s %d positional argument%s" - " (%d given)", + " (%zd given)", (parser->fname == NULL) ? "function" : parser->fname, (parser->fname == NULL) ? "" : "()", min < parser->max ? "at least" : "exactly", @@ -2313,6 +2320,215 @@ vgetargskeywordsfast(PyObject *args, PyObject *keywords, } +#undef _PyArg_UnpackKeywords + +PyObject * const * +_PyArg_UnpackKeywords(PyObject *const *args, Py_ssize_t nargs, + PyObject *kwargs, PyObject *kwnames, + struct _PyArg_Parser *parser, + int minpos, int maxpos, int minkw, + PyObject **buf) +{ + PyObject *kwtuple; + PyObject *keyword; + int i, posonly, minposonly, maxargs; + int reqlimit = minkw ? maxpos + minkw : minpos; + Py_ssize_t nkwargs; + PyObject *current_arg; + PyObject * const *kwstack = NULL; + + assert(kwargs == NULL || PyDict_Check(kwargs)); + assert(kwargs == NULL || kwnames == NULL); + + if (parser == NULL) { + PyErr_BadInternalCall(); + return NULL; + } + + if (kwnames != NULL && !PyTuple_Check(kwnames)) { + PyErr_BadInternalCall(); + return NULL; + } + + if (args == NULL && nargs == 0) { + args = buf; + } + + if (!parser_init(parser)) { + return NULL; + } + + kwtuple = parser->kwtuple; + posonly = parser->pos; + minposonly = Py_MIN(posonly, minpos); + maxargs = posonly + (int)PyTuple_GET_SIZE(kwtuple); + + if (kwargs != NULL) { + nkwargs = PyDict_GET_SIZE(kwargs); + } + else if (kwnames != NULL) { + nkwargs = PyTuple_GET_SIZE(kwnames); + kwstack = args + nargs; + } + else { + nkwargs = 0; + } + if (nkwargs == 0 && minkw == 0 && minpos <= nargs && nargs <= maxpos) { + /* Fast path. */ + return args; + } + if (nargs + nkwargs > maxargs) { + /* Adding "keyword" (when nargs == 0) prevents producing wrong error + messages in some special cases (see bpo-31229). */ + PyErr_Format(PyExc_TypeError, + "%.200s%s takes at most %d %sargument%s (%zd given)", + (parser->fname == NULL) ? "function" : parser->fname, + (parser->fname == NULL) ? "" : "()", + maxargs, + (nargs == 0) ? "keyword " : "", + (maxargs == 1) ? "" : "s", + nargs + nkwargs); + return NULL; + } + if (nargs > maxpos) { + if (maxpos == 0) { + PyErr_Format(PyExc_TypeError, + "%.200s%s takes no positional arguments", + (parser->fname == NULL) ? "function" : parser->fname, + (parser->fname == NULL) ? "" : "()"); + } + else { + PyErr_Format(PyExc_TypeError, + "%.200s%s takes %s %d positional argument%s (%zd given)", + (parser->fname == NULL) ? "function" : parser->fname, + (parser->fname == NULL) ? "" : "()", + (minpos < maxpos) ? "at most" : "exactly", + maxpos, + (maxpos == 1) ? "" : "s", + nargs); + } + return NULL; + } + if (nargs < minposonly) { + PyErr_Format(PyExc_TypeError, + "%.200s%s takes %s %d positional argument%s" + " (%zd given)", + (parser->fname == NULL) ? "function" : parser->fname, + (parser->fname == NULL) ? "" : "()", + minposonly < maxpos ? "at least" : "exactly", + minposonly, + minposonly == 1 ? "" : "s", + nargs); + return NULL; + } + + /* copy tuple args */ + for (i = 0; i < nargs; i++) { + buf[i] = args[i]; + } + + /* copy keyword args using kwtuple to drive process */ + for (i = Py_MAX((int)nargs, posonly); i < maxargs; i++) { + if (nkwargs) { + keyword = PyTuple_GET_ITEM(kwtuple, i - posonly); + if (kwargs != NULL) { + current_arg = PyDict_GetItemWithError(kwargs, keyword); + if (!current_arg && PyErr_Occurred()) { + return NULL; + } + } + else { + current_arg = find_keyword(kwnames, kwstack, keyword); + } + } + else if (i >= reqlimit) { + break; + } + else { + current_arg = NULL; + } + + buf[i] = current_arg; + + if (current_arg) { + --nkwargs; + } + else if (i < minpos || (maxpos <= i && i < reqlimit)) { + /* Less arguments than required */ + keyword = PyTuple_GET_ITEM(kwtuple, i - posonly); + PyErr_Format(PyExc_TypeError, "%.200s%s missing required " + "argument '%U' (pos %d)", + (parser->fname == NULL) ? "function" : parser->fname, + (parser->fname == NULL) ? "" : "()", + keyword, i+1); + return NULL; + } + } + + if (nkwargs > 0) { + Py_ssize_t j; + /* make sure there are no arguments given by name and position */ + for (i = posonly; i < nargs; i++) { + keyword = PyTuple_GET_ITEM(kwtuple, i - posonly); + if (kwargs != NULL) { + current_arg = PyDict_GetItemWithError(kwargs, keyword); + if (!current_arg && PyErr_Occurred()) { + return NULL; + } + } + else { + current_arg = find_keyword(kwnames, kwstack, keyword); + } + if (current_arg) { + /* arg present in tuple and in dict */ + PyErr_Format(PyExc_TypeError, + "argument for %.200s%s given by name ('%U') " + "and position (%d)", + (parser->fname == NULL) ? "function" : parser->fname, + (parser->fname == NULL) ? "" : "()", + keyword, i+1); + return NULL; + } + } + /* make sure there are no extraneous keyword arguments */ + j = 0; + while (1) { + int match; + if (kwargs != NULL) { + if (!PyDict_Next(kwargs, &j, &keyword, NULL)) + break; + } + else { + if (j >= PyTuple_GET_SIZE(kwnames)) + break; + keyword = PyTuple_GET_ITEM(kwnames, j); + j++; + } + + if (!PyUnicode_Check(keyword)) { + PyErr_SetString(PyExc_TypeError, + "keywords must be strings"); + return NULL; + } + match = PySequence_Contains(kwtuple, keyword); + if (match <= 0) { + if (!match) { + PyErr_Format(PyExc_TypeError, + "'%U' is an invalid keyword " + "argument for %.200s%s", + keyword, + (parser->fname == NULL) ? "this function" : parser->fname, + (parser->fname == NULL) ? "" : "()"); + } + return NULL; + } + } + } + + return buf; +} + + static const char * skipitem(const char **p_format, va_list *p_va, int flags) { @@ -2381,8 +2597,13 @@ skipitem(const char **p_format, va_list *p_va, int flags) if (p_va != NULL) { if (flags & FLAG_SIZE_T) (void) va_arg(*p_va, Py_ssize_t *); - else + else { + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "PY_SSIZE_T_CLEAN will be required for '#' formats", 1)) { + return NULL; + } (void) va_arg(*p_va, int *); + } } format++; } else if ((c == 's' || c == 'z' || c == 'y' || c == 'w') |