From: Stefan Behnel Date: Wed, 27 Aug 2008 07:04:41 +0000 (+0200) Subject: some more testing and tweaking of common cases, follow more descriptive Py3 error... X-Git-Tag: 0.9.9.2.beta~63^2~27 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=d7764340707bef33b08c0d7df69ddd5c10647b87;p=cython.git some more testing and tweaking of common cases, follow more descriptive Py3 error messages --- diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index 9ae0353c..24d4f7ba 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -1685,12 +1685,6 @@ class DefNode(FuncDefNode): def generate_arg_decref(self, arg, code): if arg: 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): if not self.star_arg: @@ -1703,9 +1697,17 @@ class DefNode(FuncDefNode): 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( - "if (unlikely(%s) && unlikely(!__Pyx_CheckKeywordStrings(%s, \"%s\", %d))) return %s;" % ( - Naming.kwds_cname, Naming.kwds_cname, self.name, + "if (%s && unlikely(!__Pyx_CheckKeywordStrings(%s, \"%s\", %d))) return %s;" % ( + kwarg_check, Naming.kwds_cname, self.name, bool(self.starstar_arg), self.error_value())) if self.starstar_arg: @@ -4423,10 +4425,10 @@ static INLINE void __Pyx_RaiseDoubleKeywordsError( 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*/ """,""" -static int __Pyx_CheckKeywordStrings( +static INLINE int __Pyx_CheckKeywordStrings( PyObject *kwdict, const char* function_name, int kw_allowed) @@ -4435,27 +4437,29 @@ static int __Pyx_CheckKeywordStrings( Py_ssize_t pos = 0; while (PyDict_Next(kwdict, &pos, &key, 0)) { #if PY_MAJOR_VERSION < 3 - if (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)); + if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key))) #else - "'%U' is an invalid keyword argument for this function", - key); + if (unlikely(!PyUnicode_CheckExact(key)) && unlikely(!PyUnicode_Check(key))) #endif - return 0; + goto invalid_keyword_type; } + if ((!kw_allowed) && unlikely(key)) + goto invalid_keyword; 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: invalid_keyword: PyErr_Format(PyExc_TypeError, #if PY_MAJOR_VERSION < 3 - "'%s' is an invalid keyword argument for this function", - PyString_AS_STRING(key)); + "%s() got an unexpected keyword argument '%s'", + function_name, PyString_AsString(key)); #else - "'%U' is an invalid keyword argument for this function", - key); + "%s() got an unexpected keyword argument '%U'", + function_name, key); #endif bad: return -1; diff --git a/tests/run/callargs.pyx b/tests/run/callargs.pyx index 9ec9f8af..27dff10a 100644 --- a/tests/run/callargs.pyx +++ b/tests/run/callargs.pyx @@ -58,6 +58,26 @@ __doc__ = u""" Traceback (most recent call last): 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) Traceback (most recent call last): TypeError: e() keywords must be strings diff --git a/tests/run/classkwonlyargs.pyx b/tests/run/classkwonlyargs.pyx index eac6c5c0..fe938290 100644 --- a/tests/run/classkwonlyargs.pyx +++ b/tests/run/classkwonlyargs.pyx @@ -21,7 +21,7 @@ __doc__ = u""" TypeError: d() takes exactly 3 positional arguments (4 given) >>> d(1,2, d=1) 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, c=1) @@ -43,7 +43,7 @@ __doc__ = u""" TypeError: f() needs keyword-only argument c >>> f(1,2, c=1, e=2) 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, e=0, f=2, d=11) diff --git a/tests/run/extkwonlyargs.pyx b/tests/run/extkwonlyargs.pyx index c3371e03..1a0c7271 100644 --- a/tests/run/extkwonlyargs.pyx +++ b/tests/run/extkwonlyargs.pyx @@ -21,7 +21,7 @@ __doc__ = u""" TypeError: d() takes exactly 2 positional arguments (3 given) >>> d(1,2, d=1) 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, c=1) @@ -43,7 +43,7 @@ __doc__ = u""" TypeError: f() needs keyword-only argument c >>> f(1,2, c=1, e=2) 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, e=0, f=2, d=11) diff --git a/tests/run/extstarargs.pyx b/tests/run/extstarargs.pyx index f1d29eb2..244c603b 100644 --- a/tests/run/extstarargs.pyx +++ b/tests/run/extstarargs.pyx @@ -13,7 +13,7 @@ __doc__ = u""" TypeError: spam() takes exactly 3 positional arguments (4 given) >>> spam(1,2,3, a=1) #doctest: +ELLIPSIS 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) (1, 2, 3, ()) @@ -26,7 +26,7 @@ __doc__ = u""" TypeError: grail() takes at least 3 positional arguments (2 given) >>> grail(1,2,3, a=1) #doctest: +ELLIPSIS 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) (1, 2, 3, ()) @@ -57,10 +57,10 @@ __doc__ = u""" (1, 2) >>> onlyt(a=1) 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) 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) (('a', 1),) diff --git a/tests/run/kwonlyargs.pyx b/tests/run/kwonlyargs.pyx index e89caf17..2994a559 100644 --- a/tests/run/kwonlyargs.pyx +++ b/tests/run/kwonlyargs.pyx @@ -18,7 +18,7 @@ __doc__ = u""" TypeError: d() takes exactly 2 positional arguments (3 given) >>> d(1,2, d=1) 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, c=1) @@ -40,7 +40,7 @@ __doc__ = u""" TypeError: f() needs keyword-only argument c >>> f(1,2, c=1, e=2) 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, e=0, f=2, d=11) diff --git a/tests/run/kwonlyargscall.pyx b/tests/run/kwonlyargscall.pyx index 0c8592f9..49a95013 100644 --- a/tests/run/kwonlyargscall.pyx +++ b/tests/run/kwonlyargscall.pyx @@ -36,7 +36,7 @@ __doc__ = u""" TypeError: d() takes exactly 2 positional arguments (3 given) >>> call2d(d) 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) 1 2 3 [] @@ -69,7 +69,7 @@ __doc__ = u""" TypeError: f() needs keyword-only argument c >>> call2ce(f) 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) 1 2 1 42 17 2 [] @@ -133,7 +133,7 @@ __doc__ = u""" TypeError: m() needs keyword-only argument c >>> call2cd(m) 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: diff --git a/tests/run/starargs.pyx b/tests/run/starargs.pyx index f8d810fa..c4908cc7 100644 --- a/tests/run/starargs.pyx +++ b/tests/run/starargs.pyx @@ -9,7 +9,7 @@ __doc__ = u""" TypeError: spam() takes exactly 3 positional arguments (4 given) >>> spam(1,2,3, a=1) 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) (1, 2, 3, ()) @@ -22,7 +22,7 @@ __doc__ = u""" TypeError: grail() takes at least 3 positional arguments (2 given) >>> grail(1,2,3, a=1) 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) (1, 2, 3, ()) @@ -53,10 +53,10 @@ __doc__ = u""" (1, 2) >>> onlyt(a=1) 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) 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) (('a', 1),)