diff Modules/_struct.c @ 80050:626ed0e06fd6 2.7

#14897: Enhance error messages of struct.pack and struct.pack_into Patch by Matti Mäki.
author Petri Lehtinen <petri@digip.org>
date Mon, 29 Oct 2012 21:16:57 +0200
parents 37554bda2014
children be4bec689de3
line wrap: on
line diff
--- a/Modules/_struct.c	Sun Oct 28 11:16:36 2012 -0700
+++ b/Modules/_struct.c	Mon Oct 29 21:16:57 2012 +0200
@@ -1603,7 +1603,7 @@
     if (PyTuple_GET_SIZE(args) != soself->s_len)
     {
         PyErr_Format(StructError,
-            "pack requires exactly %zd arguments", soself->s_len);
+            "pack expected %zd items for packing (got %zd)", soself->s_len, PyTuple_GET_SIZE(args));
         return NULL;
     }
 
@@ -1642,9 +1642,19 @@
     assert(soself->s_codes != NULL);
     if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
     {
-        PyErr_Format(StructError,
-                     "pack_into requires exactly %zd arguments",
-                     (soself->s_len + 2));
+        if (PyTuple_GET_SIZE(args) == 0) {
+            PyErr_Format(StructError,
+                        "pack_into expected buffer argument");
+        }
+        else if (PyTuple_GET_SIZE(args) == 1) {
+            PyErr_Format(StructError,
+                        "pack_into expected offset argument");
+        }
+        else {
+            PyErr_Format(StructError,
+                        "pack_into expected %zd items for packing (got %zd)",
+                        soself->s_len, (PyTuple_GET_SIZE(args) - 2));
+        }
         return NULL;
     }