]> granicus.if.org Git - python/commitdiff
#14897: Enhance error messages of struct.pack and struct.pack_into
authorPetri Lehtinen <petri@digip.org>
Mon, 29 Oct 2012 19:16:57 +0000 (21:16 +0200)
committerPetri Lehtinen <petri@digip.org>
Mon, 29 Oct 2012 19:24:07 +0000 (21:24 +0200)
Patch by Matti Mäki.

Misc/ACKS
Misc/NEWS
Modules/_struct.c

index 2b97f51a3777999e216f83df206f283398ee1f5e..a070be811c1d7f3f70581d52ed17ccec6aba48ba 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -710,6 +710,7 @@ Brian Merrell
 Luke Mewburn
 Carl Meyer
 Mike Meyer
+Piotr Meyer
 Steven Miale
 Trent Mick
 Tom Middleton
@@ -742,7 +743,7 @@ Sape Mullender
 Michael Muller
 Neil Muller
 R. David Murray
-Piotr Meyer
+Matti Mäki
 Dale Nagata
 John Nagle
 Takahiro Nakayama
index f2ba9826adf3bf07236d9621c9c16b0df87aee3a..4484813950c3d08d5a34b04864a45c4e81138cbc 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -135,6 +135,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #14897: Enhance error messages of struct.pack and
+  struct.pack_into. Patch by Matti Mäki.
+
 - Issue #12890: cgitb no longer prints spurious <p> tags in text
   mode when the logdir option is specified.
 
index 558d24bf8f4eb087aae7074a36313b0acb346a48..edbe9b9884b5a90ce3f74d6fd85ad31fdc877f2f 100644 (file)
@@ -1575,7 +1575,7 @@ s_pack(PyObject *self, PyObject *args)
     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;
     }
 
@@ -1614,9 +1614,19 @@ s_pack_into(PyObject *self, PyObject *args)
     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;
     }