Commit d7764340 authored by Stefan Behnel's avatar Stefan Behnel

some more testing and tweaking of common cases, follow more descriptive Py3 error messages

parent 805cf073
...@@ -1685,12 +1685,6 @@ class DefNode(FuncDefNode): ...@@ -1685,12 +1685,6 @@ class DefNode(FuncDefNode):
def generate_arg_decref(self, arg, code): def generate_arg_decref(self, arg, code):
if arg: if arg:
code.put_var_decref(arg.entry) code.put_var_decref(arg.entry)
def arg_address(self, arg):
if arg:
return "&%s" % arg.entry.cname
else:
return 0
def generate_stararg_copy_code(self, code): def generate_stararg_copy_code(self, code):
if not self.star_arg: if not self.star_arg:
...@@ -1703,9 +1697,17 @@ class DefNode(FuncDefNode): ...@@ -1703,9 +1697,17 @@ class DefNode(FuncDefNode):
code.globalstate.use_utility_code(keyword_string_check_utility_code) code.globalstate.use_utility_code(keyword_string_check_utility_code)
if self.starstar_arg:
if self.star_arg:
kwarg_check = "unlikely(%s)" % Naming.kwds_cname
else:
kwarg_check = "%s" % Naming.kwds_cname
else:
kwarg_check = "unlikely(%s) && unlikely(PyDict_Size(%s) > 0)" % (
Naming.kwds_cname, Naming.kwds_cname)
code.putln( code.putln(
"if (unlikely(%s) && unlikely(!__Pyx_CheckKeywordStrings(%s, \"%s\", %d))) return %s;" % ( "if (%s && unlikely(!__Pyx_CheckKeywordStrings(%s, \"%s\", %d))) return %s;" % (
Naming.kwds_cname, Naming.kwds_cname, self.name, kwarg_check, Naming.kwds_cname, self.name,
bool(self.starstar_arg), self.error_value())) bool(self.starstar_arg), self.error_value()))
if self.starstar_arg: if self.starstar_arg:
...@@ -4423,10 +4425,10 @@ static INLINE void __Pyx_RaiseDoubleKeywordsError( ...@@ -4423,10 +4425,10 @@ static INLINE void __Pyx_RaiseDoubleKeywordsError(
keyword_string_check_utility_code = [ keyword_string_check_utility_code = [
""" """
static int __Pyx_CheckKeywordStrings(PyObject *kwdict, static INLINE int __Pyx_CheckKeywordStrings(PyObject *kwdict,
const char* function_name, int kw_allowed); /*proto*/ const char* function_name, int kw_allowed); /*proto*/
""",""" ""","""
static int __Pyx_CheckKeywordStrings( static INLINE int __Pyx_CheckKeywordStrings(
PyObject *kwdict, PyObject *kwdict,
const char* function_name, const char* function_name,
int kw_allowed) int kw_allowed)
...@@ -4435,27 +4437,29 @@ static int __Pyx_CheckKeywordStrings( ...@@ -4435,27 +4437,29 @@ static int __Pyx_CheckKeywordStrings(
Py_ssize_t pos = 0; Py_ssize_t pos = 0;
while (PyDict_Next(kwdict, &pos, &key, 0)) { while (PyDict_Next(kwdict, &pos, &key, 0)) {
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
if (unlikely(!PyString_Check(key))) { if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key)))
#else
if (unlikely(!PyUnicode_Check(key))) {
#endif
PyErr_Format(PyExc_TypeError,
"%s() keywords must be strings", function_name);
return 0;
}
}
if (unlikely(!kw_allowed) && unlikely(key)) {
PyErr_Format(PyExc_TypeError,
#if PY_MAJOR_VERSION < 3
"'%s' is an invalid keyword argument for this function",
PyString_AsString(key));
#else #else
"'%U' is an invalid keyword argument for this function", if (unlikely(!PyUnicode_CheckExact(key)) && unlikely(!PyUnicode_Check(key)))
key);
#endif #endif
return 0; goto invalid_keyword_type;
} }
if ((!kw_allowed) && unlikely(key))
goto invalid_keyword;
return 1; return 1;
invalid_keyword_type:
PyErr_Format(PyExc_TypeError,
"%s() keywords must be strings", function_name);
return 0;
invalid_keyword:
PyErr_Format(PyExc_TypeError,
#if PY_MAJOR_VERSION < 3
"%s() got an unexpected keyword argument '%s'",
function_name, PyString_AsString(key));
#else
"%s() got an unexpected keyword argument '%U'",
function_name, key);
#endif
return 0;
} }
"""] """]
...@@ -4535,11 +4539,11 @@ invalid_keyword_type: ...@@ -4535,11 +4539,11 @@ invalid_keyword_type:
invalid_keyword: invalid_keyword:
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
"'%s' is an invalid keyword argument for this function", "%s() got an unexpected keyword argument '%s'",
PyString_AS_STRING(key)); function_name, PyString_AsString(key));
#else #else
"'%U' is an invalid keyword argument for this function", "%s() got an unexpected keyword argument '%U'",
key); function_name, key);
#endif #endif
bad: bad:
return -1; return -1;
......
...@@ -58,6 +58,26 @@ __doc__ = u""" ...@@ -58,6 +58,26 @@ __doc__ = u"""
Traceback (most recent call last): Traceback (most recent call last):
TypeError: h() takes at least 3 positional arguments (2 given) TypeError: h() takes at least 3 positional arguments (2 given)
>>> f(1,2, d=5)
Traceback (most recent call last):
TypeError: f() got an unexpected keyword argument 'd'
>>> f(1, d=5)
Traceback (most recent call last):
TypeError: f() got an unexpected keyword argument 'd'
>>> f(d=5)
Traceback (most recent call last):
TypeError: f() got an unexpected keyword argument 'd'
>>> g(1,2, d=5)
Traceback (most recent call last):
TypeError: g() takes exactly 0 positional arguments (2 given)
>>> g(1,2)
Traceback (most recent call last):
TypeError: g() takes exactly 0 positional arguments (2 given)
>>> g(1)
Traceback (most recent call last):
TypeError: g() takes exactly 0 positional arguments (1 given)
>>> test_int_kwargs(e) >>> test_int_kwargs(e)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: e() keywords must be strings TypeError: e() keywords must be strings
......
...@@ -21,7 +21,7 @@ __doc__ = u""" ...@@ -21,7 +21,7 @@ __doc__ = u"""
TypeError: d() takes exactly 3 positional arguments (4 given) TypeError: d() takes exactly 3 positional arguments (4 given)
>>> d(1,2, d=1) >>> d(1,2, d=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'd' is an invalid keyword argument for this function TypeError: d() got an unexpected keyword argument 'd'
>>> e(1,2) >>> e(1,2)
>>> e(1,2, c=1) >>> e(1,2, c=1)
...@@ -43,7 +43,7 @@ __doc__ = u""" ...@@ -43,7 +43,7 @@ __doc__ = u"""
TypeError: f() needs keyword-only argument c TypeError: f() needs keyword-only argument c
>>> f(1,2, c=1, e=2) >>> f(1,2, c=1, e=2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'e' is an invalid keyword argument for this function TypeError: f() got an unexpected keyword argument 'e'
>>> g(1,2, c=1, f=2) >>> g(1,2, c=1, f=2)
>>> g(1,2, c=1, e=0, f=2, d=11) >>> g(1,2, c=1, e=0, f=2, d=11)
......
...@@ -21,7 +21,7 @@ __doc__ = u""" ...@@ -21,7 +21,7 @@ __doc__ = u"""
TypeError: d() takes exactly 2 positional arguments (3 given) TypeError: d() takes exactly 2 positional arguments (3 given)
>>> d(1,2, d=1) >>> d(1,2, d=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'd' is an invalid keyword argument for this function TypeError: d() got an unexpected keyword argument 'd'
>>> e(1,2) >>> e(1,2)
>>> e(1,2, c=1) >>> e(1,2, c=1)
...@@ -43,7 +43,7 @@ __doc__ = u""" ...@@ -43,7 +43,7 @@ __doc__ = u"""
TypeError: f() needs keyword-only argument c TypeError: f() needs keyword-only argument c
>>> f(1,2, c=1, e=2) >>> f(1,2, c=1, e=2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'e' is an invalid keyword argument for this function TypeError: f() got an unexpected keyword argument 'e'
>>> g(1,2, c=1, f=2) >>> g(1,2, c=1, f=2)
>>> g(1,2, c=1, e=0, f=2, d=11) >>> g(1,2, c=1, e=0, f=2, d=11)
......
...@@ -13,7 +13,7 @@ __doc__ = u""" ...@@ -13,7 +13,7 @@ __doc__ = u"""
TypeError: spam() takes exactly 3 positional arguments (4 given) TypeError: spam() takes exactly 3 positional arguments (4 given)
>>> spam(1,2,3, a=1) #doctest: +ELLIPSIS >>> spam(1,2,3, a=1) #doctest: +ELLIPSIS
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function TypeError: spam() got an unexpected keyword argument 'a'
>>> grail(1,2,3) >>> grail(1,2,3)
(1, 2, 3, ()) (1, 2, 3, ())
...@@ -26,7 +26,7 @@ __doc__ = u""" ...@@ -26,7 +26,7 @@ __doc__ = u"""
TypeError: grail() takes at least 3 positional arguments (2 given) TypeError: grail() takes at least 3 positional arguments (2 given)
>>> grail(1,2,3, a=1) #doctest: +ELLIPSIS >>> grail(1,2,3, a=1) #doctest: +ELLIPSIS
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function TypeError: grail() got an unexpected keyword argument 'a'
>>> swallow(1,2,3) >>> swallow(1,2,3)
(1, 2, 3, ()) (1, 2, 3, ())
...@@ -57,10 +57,10 @@ __doc__ = u""" ...@@ -57,10 +57,10 @@ __doc__ = u"""
(1, 2) (1, 2)
>>> onlyt(a=1) >>> onlyt(a=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function TypeError: onlyt() got an unexpected keyword argument 'a'
>>> onlyt(1, a=2) >>> onlyt(1, a=2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function TypeError: onlyt() got an unexpected keyword argument 'a'
>>> onlyk(a=1) >>> onlyk(a=1)
(('a', 1),) (('a', 1),)
......
...@@ -18,7 +18,7 @@ __doc__ = u""" ...@@ -18,7 +18,7 @@ __doc__ = u"""
TypeError: d() takes exactly 2 positional arguments (3 given) TypeError: d() takes exactly 2 positional arguments (3 given)
>>> d(1,2, d=1) >>> d(1,2, d=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'd' is an invalid keyword argument for this function TypeError: d() got an unexpected keyword argument 'd'
>>> e(1,2) >>> e(1,2)
>>> e(1,2, c=1) >>> e(1,2, c=1)
...@@ -40,7 +40,7 @@ __doc__ = u""" ...@@ -40,7 +40,7 @@ __doc__ = u"""
TypeError: f() needs keyword-only argument c TypeError: f() needs keyword-only argument c
>>> f(1,2, c=1, e=2) >>> f(1,2, c=1, e=2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'e' is an invalid keyword argument for this function TypeError: f() got an unexpected keyword argument 'e'
>>> g(1,2, c=1, f=2) >>> g(1,2, c=1, f=2)
>>> g(1,2, c=1, e=0, f=2, d=11) >>> g(1,2, c=1, e=0, f=2, d=11)
......
...@@ -36,7 +36,7 @@ __doc__ = u""" ...@@ -36,7 +36,7 @@ __doc__ = u"""
TypeError: d() takes exactly 2 positional arguments (3 given) TypeError: d() takes exactly 2 positional arguments (3 given)
>>> call2d(d) >>> call2d(d)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'd' is an invalid keyword argument for this function TypeError: d() got an unexpected keyword argument 'd'
>>> call0abc(e) >>> call0abc(e)
1 2 3 [] 1 2 3 []
...@@ -69,7 +69,7 @@ __doc__ = u""" ...@@ -69,7 +69,7 @@ __doc__ = u"""
TypeError: f() needs keyword-only argument c TypeError: f() needs keyword-only argument c
>>> call2ce(f) >>> call2ce(f)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'e' is an invalid keyword argument for this function TypeError: f() got an unexpected keyword argument 'e'
>>> call2cf(g) >>> call2cf(g)
1 2 1 42 17 2 [] 1 2 1 42 17 2 []
...@@ -133,7 +133,7 @@ __doc__ = u""" ...@@ -133,7 +133,7 @@ __doc__ = u"""
TypeError: m() needs keyword-only argument c TypeError: m() needs keyword-only argument c
>>> call2cd(m) >>> call2cd(m)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'd' is an invalid keyword argument for this function TypeError: m() got an unexpected keyword argument 'd'
""" """
# the calls: # the calls:
......
...@@ -9,7 +9,7 @@ __doc__ = u""" ...@@ -9,7 +9,7 @@ __doc__ = u"""
TypeError: spam() takes exactly 3 positional arguments (4 given) TypeError: spam() takes exactly 3 positional arguments (4 given)
>>> spam(1,2,3, a=1) >>> spam(1,2,3, a=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function TypeError: spam() got an unexpected keyword argument 'a'
>>> grail(1,2,3) >>> grail(1,2,3)
(1, 2, 3, ()) (1, 2, 3, ())
...@@ -22,7 +22,7 @@ __doc__ = u""" ...@@ -22,7 +22,7 @@ __doc__ = u"""
TypeError: grail() takes at least 3 positional arguments (2 given) TypeError: grail() takes at least 3 positional arguments (2 given)
>>> grail(1,2,3, a=1) >>> grail(1,2,3, a=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function TypeError: grail() got an unexpected keyword argument 'a'
>>> swallow(1,2,3) >>> swallow(1,2,3)
(1, 2, 3, ()) (1, 2, 3, ())
...@@ -53,10 +53,10 @@ __doc__ = u""" ...@@ -53,10 +53,10 @@ __doc__ = u"""
(1, 2) (1, 2)
>>> onlyt(a=1) >>> onlyt(a=1)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function TypeError: onlyt() got an unexpected keyword argument 'a'
>>> onlyt(1, a=2) >>> onlyt(1, a=2)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: 'a' is an invalid keyword argument for this function TypeError: onlyt() got an unexpected keyword argument 'a'
>>> onlyk(a=1) >>> onlyk(a=1)
(('a', 1),) (('a', 1),)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment