]> 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:20:19 +0000 (21:20 +0200)
Patch by Matti Mäki.

Misc/ACKS
Misc/NEWS
Modules/_struct.c

index 2abed58992670cc51375af47bef11cd154f5a10c..129a116d8dbefd7d7c573a214ddd794da02ad838 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -655,6 +655,7 @@ Brian Merrell
 Luke Mewburn
 Carl Meyer
 Mike Meyer
+Piotr Meyer
 Steven Miale
 Trent Mick
 Tom Middleton
@@ -685,7 +686,7 @@ Sape Mullender
 Michael Muller
 Neil Muller
 R. David Murray
-Piotr Meyer
+Matti Mäki
 Dale Nagata
 John Nagle
 Takahiro Nakayama
index f384a2e8708040682d387c4757619f7b9a02c519..11113f3006a090bc4032bb2bf24200bca8f553a3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -125,6 +125,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 c4c1dfbb955f268e530bf702f521a2e82f71fd47..d8c932427d6b7d3229e47bdff9eccdc903918607 100644 (file)
@@ -1603,7 +1603,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;
     }
 
@@ -1642,9 +1642,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;
     }