[Python-checkins] bpo-31229: Fixed wrong error messages when too many keyword arguments are received. (#3180)

Serhiy Storchaka webhook-mailer at python.org
Wed Aug 23 14:16:57 EDT 2017


https://github.com/python/cpython/commit/bf9075a0c55186d2f34df63e6c8512dd6414ff4b
commit: bf9075a0c55186d2f34df63e6c8512dd6414ff4b
branch: master
author: Oren Milman <orenmn at gmail.com>
committer: Serhiy Storchaka <storchaka at gmail.com>
date: 2017年08月23日T21:16:48+03:00
summary:
bpo-31229: Fixed wrong error messages when too many keyword arguments are received. (#3180)
files:
M Lib/test/test_call.py
M Python/getargs.c
diff --git a/Lib/test/test_call.py b/Lib/test/test_call.py
index b004b5803c3..3f9987c9e77 100644
--- a/Lib/test/test_call.py
+++ b/Lib/test/test_call.py
@@ -7,6 +7,7 @@
 _testcapi = None
 import struct
 import collections
+import itertools
 
 # The test cases here cover several paths through the function calling
 # code. They depend on the METH_XXX flag that is used to define a C
@@ -194,6 +195,26 @@ def test_varargs13_kw(self):
 msg = r"^classmethod\(\) takes no keyword arguments$"
 self.assertRaisesRegex(TypeError, msg, classmethod, func=id)
 
+ def test_varargs14_kw(self):
+ msg = r"^product\(\) takes at most 1 keyword argument \(2 given\)$"
+ self.assertRaisesRegex(TypeError, msg,
+ itertools.product, 0, repeat=1, foo=2)
+
+ def test_varargs15_kw(self):
+ msg = r"^ImportError\(\) takes at most 2 keyword arguments \(3 given\)$"
+ self.assertRaisesRegex(TypeError, msg,
+ ImportError, 0, name=1, path=2, foo=3)
+
+ def test_varargs16_kw(self):
+ msg = r"^min\(\) takes at most 2 keyword arguments \(3 given\)$"
+ self.assertRaisesRegex(TypeError, msg,
+ min, 0, default=1, key=2, foo=3)
+
+ def test_varargs17_kw(self):
+ msg = r"^print\(\) takes at most 4 keyword arguments \(5 given\)$"
+ self.assertRaisesRegex(TypeError, msg,
+ print, 0, sep=1, end=2, file=3, flush=4, foo=5)
+
 def test_oldargs0_1(self):
 msg = r"keys\(\) takes no arguments \(1 given\)"
 self.assertRaisesRegex(TypeError, msg, {}.keys, 0)
diff --git a/Python/getargs.c b/Python/getargs.c
index 4645b0f2006..4b969d924a9 100644
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -1654,11 +1654,14 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
 nargs = PyTuple_GET_SIZE(args);
 nkwargs = (kwargs == NULL) ? 0 : PyDict_GET_SIZE(kwargs);
 if (nargs + nkwargs > len) {
+ /* 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 argument%s (%zd given)",
+ "%.200s%s takes at most %d %sargument%s (%zd given)",
 (fname == NULL) ? "function" : fname,
 (fname == NULL) ? "" : "()",
 len,
+ (nargs == 0) ? "keyword " : "",
 (len == 1) ? "" : "s",
 nargs + nkwargs);
 return cleanreturn(0, &freelist);
@@ -2077,11 +2080,14 @@ vgetargskeywordsfast_impl(PyObject **args, Py_ssize_t nargs,
 nkwargs = 0;
 }
 if (nargs + nkwargs > len) {
+ /* 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 argument%s (%zd given)",
+ "%.200s%s takes at most %d %sargument%s (%zd given)",
 (parser->fname == NULL) ? "function" : parser->fname,
 (parser->fname == NULL) ? "" : "()",
 len,
+ (nargs == 0) ? "keyword " : "",
 (len == 1) ? "" : "s",
 nargs + nkwargs);
 return cleanreturn(0, &freelist);


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /