summaryrefslogtreecommitdiffstats
path: root/Python/getargs.c
diff options
context:
space:
mode:
authorMichael Seifert <michaelseifert04@yahoo.de>2017-04-09 07:47:12 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2017-04-09 07:47:12 (GMT)
commit64c8f705c0121a4b45ca2c3bc7b47b282e9efcd8 (patch)
tree5f58baf5aa8ab19a167688c1710016291f5758ca /Python/getargs.c
parenta2a9ddd923a849124bdd1c484f70f02df6fde0e9 (diff)
downloadcpython-64c8f705c0121a4b45ca2c3bc7b47b282e9efcd8.zip
cpython-64c8f705c0121a4b45ca2c3bc7b47b282e9efcd8.tar.gz
cpython-64c8f705c0121a4b45ca2c3bc7b47b282e9efcd8.tar.bz2
bpo-29951: Include function name for some error messages in `PyArg_ParseTuple*` (#916)
Also changed format specifier for function name from "%s" to "%.200s" and exception messages should start with lowercase letter.
Diffstat (limited to 'Python/getargs.c')
-rw-r--r--Python/getargs.c64
1 files changed, 42 insertions, 22 deletions
diff --git a/Python/getargs.c b/Python/getargs.c
index 8cb672d..58c9a99 100644
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -1584,7 +1584,7 @@ PyArg_ValidateKeywordArguments(PyObject *kwargs)
}
if (!_PyDict_HasOnlyStringKeys(kwargs)) {
PyErr_SetString(PyExc_TypeError,
- "keyword arguments must be strings");
+ "keywords must be strings");
return 0;
}
return 1;
@@ -1655,7 +1655,7 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
nkwargs = (kwargs == NULL) ? 0 : PyDict_GET_SIZE(kwargs);
if (nargs + nkwargs > len) {
PyErr_Format(PyExc_TypeError,
- "%s%s takes at most %d argument%s (%zd given)",
+ "%.200s%s takes at most %d argument%s (%zd given)",
(fname == NULL) ? "function" : fname,
(fname == NULL) ? "" : "()",
len,
@@ -1705,8 +1705,10 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
}
if (max < nargs) {
PyErr_Format(PyExc_TypeError,
- "Function takes %s %d positional arguments"
+ "%.200s%s takes %s %d positional arguments"
" (%d given)",
+ (fname == NULL) ? "function" : fname,
+ (fname == NULL) ? "" : "()",
(min != INT_MAX) ? "at most" : "exactly",
max, nargs);
return cleanreturn(0, &freelist);
@@ -1752,8 +1754,10 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
* or the end of the format. */
}
else {
- PyErr_Format(PyExc_TypeError, "Required argument "
- "'%s' (pos %d) not found",
+ PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
+ "argument '%s' (pos %d)",
+ (fname == NULL) ? "function" : fname,
+ (fname == NULL) ? "" : "()",
kwlist[i], i+1);
return cleanreturn(0, &freelist);
}
@@ -1779,8 +1783,10 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
if (skip) {
PyErr_Format(PyExc_TypeError,
- "Function takes %s %d positional arguments"
+ "%.200s%s takes %s %d positional arguments"
" (%d given)",
+ (fname == NULL) ? "function" : fname,
+ (fname == NULL) ? "" : "()",
(Py_MIN(pos, min) < i) ? "at least" : "exactly",
Py_MIN(pos, min), nargs);
return cleanreturn(0, &freelist);
@@ -1802,8 +1808,10 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
if (current_arg) {
/* arg present in tuple and in dict */
PyErr_Format(PyExc_TypeError,
- "Argument given by name ('%s') "
+ "argument for %.200s%s given by name ('%s') "
"and position (%d)",
+ (fname == NULL) ? "function" : fname,
+ (fname == NULL) ? "" : "()",
kwlist[i], i+1);
return cleanreturn(0, &freelist);
}
@@ -1826,8 +1834,10 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
if (!match) {
PyErr_Format(PyExc_TypeError,
"'%U' is an invalid keyword "
- "argument for this function",
- key);
+ "argument for %.200s%s",
+ key,
+ (fname == NULL) ? "this function" : fname,
+ (fname == NULL) ? "" : "()");
return cleanreturn(0, &freelist);
}
}
@@ -2060,7 +2070,7 @@ vgetargskeywordsfast_impl(PyObject **args, Py_ssize_t nargs,
}
if (nargs + nkwargs > len) {
PyErr_Format(PyExc_TypeError,
- "%s%s takes at most %d argument%s (%zd given)",
+ "%.200s%s takes at most %d argument%s (%zd given)",
(parser->fname == NULL) ? "function" : parser->fname,
(parser->fname == NULL) ? "" : "()",
len,
@@ -2070,7 +2080,9 @@ vgetargskeywordsfast_impl(PyObject **args, Py_ssize_t nargs,
}
if (parser->max < nargs) {
PyErr_Format(PyExc_TypeError,
- "Function takes %s %d positional arguments (%d given)",
+ "%200s%s takes %s %d positional arguments (%d given)",
+ (parser->fname == NULL) ? "function" : parser->fname,
+ (parser->fname == NULL) ? "" : "()",
(parser->min != INT_MAX) ? "at most" : "exactly",
parser->max, nargs);
return cleanreturn(0, &freelist);
@@ -2115,15 +2127,19 @@ vgetargskeywordsfast_impl(PyObject **args, Py_ssize_t nargs,
if (i < pos) {
Py_ssize_t min = Py_MIN(pos, parser->min);
PyErr_Format(PyExc_TypeError,
- "Function takes %s %d positional arguments"
+ "%.200s%s takes %s %d positional arguments"
" (%d given)",
+ (parser->fname == NULL) ? "function" : parser->fname,
+ (parser->fname == NULL) ? "" : "()",
min < parser->max ? "at least" : "exactly",
min, nargs);
}
else {
keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
- PyErr_Format(PyExc_TypeError, "Required argument "
- "'%U' (pos %d) not found",
+ 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 cleanreturn(0, &freelist);
@@ -2153,8 +2169,10 @@ vgetargskeywordsfast_impl(PyObject **args, Py_ssize_t nargs,
if (current_arg) {
/* arg present in tuple and in dict */
PyErr_Format(PyExc_TypeError,
- "Argument given by name ('%U') "
+ "argument for %.200s%s given by name ('%U') "
"and position (%d)",
+ (parser->fname == NULL) ? "function" : parser->fname,
+ (parser->fname == NULL) ? "" : "()",
keyword, i+1);
return cleanreturn(0, &freelist);
}
@@ -2184,8 +2202,10 @@ vgetargskeywordsfast_impl(PyObject **args, Py_ssize_t nargs,
if (!match) {
PyErr_Format(PyExc_TypeError,
"'%U' is an invalid keyword "
- "argument for this function",
- keyword);
+ "argument for %.200s%s",
+ keyword,
+ (parser->fname == NULL) ? "this function" : parser->fname,
+ (parser->fname == NULL) ? "" : "()");
}
return cleanreturn(0, &freelist);
}
@@ -2365,7 +2385,7 @@ unpack_stack(PyObject **args, Py_ssize_t nargs, const char *name,
if (name != NULL)
PyErr_Format(
PyExc_TypeError,
- "%s expected %s%zd arguments, got %zd",
+ "%.200s expected %s%zd arguments, got %zd",
name, (min == max ? "" : "at least "), min, nargs);
else
PyErr_Format(
@@ -2384,7 +2404,7 @@ unpack_stack(PyObject **args, Py_ssize_t nargs, const char *name,
if (name != NULL)
PyErr_Format(
PyExc_TypeError,
- "%s expected %s%zd arguments, got %zd",
+ "%.200s expected %s%zd arguments, got %zd",
name, (min == max ? "" : "at most "), max, nargs);
else
PyErr_Format(
@@ -2469,7 +2489,7 @@ _PyArg_NoKeywords(const char *funcname, PyObject *kwargs)
return 1;
}
- PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
+ PyErr_Format(PyExc_TypeError, "%.200s does not take keyword arguments",
funcname);
return 0;
}
@@ -2486,7 +2506,7 @@ _PyArg_NoStackKeywords(const char *funcname, PyObject *kwnames)
return 1;
}
- PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
+ PyErr_Format(PyExc_TypeError, "%.200s does not take keyword arguments",
funcname);
return 0;
}
@@ -2504,7 +2524,7 @@ _PyArg_NoPositional(const char *funcname, PyObject *args)
if (PyTuple_GET_SIZE(args) == 0)
return 1;
- PyErr_Format(PyExc_TypeError, "%s does not take positional arguments",
+ PyErr_Format(PyExc_TypeError, "%.200s does not take positional arguments",
funcname);
return 0;
}