From 010be57d1b8e32718954d1538344a6cde06c07ca Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Sat, 16 Feb 2008 19:15:52 +0100 Subject: [PATCH] fix exception string formatting for Py_ssize_t values: needs %zd instead of %d on Python >= 2.5 --- Cython/Compiler/Nodes.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index 942c1e4b..ae7958af 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -1094,6 +1094,8 @@ class DefNode(FuncDefNode): if self.signature_has_generic_args(): if self.star_arg: env.use_utility_code(get_stararg_utility_code) + elif self.signature_has_generic_args(): + env.use_utility_code(raise_argtuple_too_long_utility_code) if not self.signature_has_nongeneric_args(): env.use_utility_code(get_keyword_string_check_utility_code) elif self.starstar_arg: @@ -1566,9 +1568,8 @@ class DefNode(FuncDefNode): def generate_positional_args_check(self, code, nargs): code.putln("if (unlikely(PyTuple_GET_SIZE(%s) > %d)) {" % ( Naming.args_cname, nargs)) - error_message = "function takes at most %d positional arguments (%d given)" - code.putln("PyErr_Format(PyExc_TypeError, \"%s\", %d, PyTuple_GET_SIZE(%s));" % ( - error_message, nargs, Naming.args_cname)) + code.putln("__Pyx_RaiseArgtupleTooLong(%d, PyTuple_GET_SIZE(%s));" % ( + nargs, Naming.args_cname)) code.putln("return %s;" % self.error_value()) code.putln("}") @@ -3641,6 +3642,30 @@ static INLINE int __Pyx_SplitStarArg( } """] +#------------------------------------------------------------------------------------ +# +# __Pyx_RaiseArgtupleTooLong raises the correct exception when too +# many positional arguments were found. This handles Py_ssize_t +# formatting correctly. + +raise_argtuple_too_long_utility_code = [ +""" +static INLINE void __Pyx_RaiseArgtupleTooLong(Py_ssize_t num_expected, Py_ssize_t num_found); /*proto*/ +""",""" +static INLINE void __Pyx_RaiseArgtupleTooLong( + Py_ssize_t num_expected, + Py_ssize_t num_found) +{ + const char* error_message = + #if PY_VERSION_HEX < 0x02050000 + "function takes at most %d positional arguments (%d given)"; + #else + "function takes at most %zd positional arguments (%zd given)"; + #endif + PyErr_Format(PyExc_TypeError, error_message, num_expected, num_found); +} +"""] + #------------------------------------------------------------------------------------ # # __Pyx_CheckKeywordStrings raises an error if non-string keywords -- 2.26.2