fix exception string formatting for Py_ssize_t values: needs %zd instead of %d on...
authorStefan Behnel <scoder@users.berlios.de>
Sat, 16 Feb 2008 18:15:52 +0000 (19:15 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Sat, 16 Feb 2008 18:15:52 +0000 (19:15 +0100)
Cython/Compiler/Nodes.py

index 942c1e4bb9ada3408873b4b0fe956a20fd372af7..ae7958af02f2a0de5891b11e4957fba10c37b859 100644 (file)
@@ -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