[Python-checkins] cpython (merge 3.3 -> default): #14897: Enhance error messages of struct.pack and struct.pack_into

petri.lehtinen python-checkins at python.org
Mon Oct 29 20:29:10 CET 2012


http://hg.python.org/cpython/rev/ebc588a3db51
changeset:   80053:ebc588a3db51
parent:      80049:1bee1a176306
parent:      80052:70d5906e0461
user:        Petri Lehtinen <petri at digip.org>
date:        Mon Oct 29 21:26:56 2012 +0200
summary:
  #14897: Enhance error messages of struct.pack and struct.pack_into

Patch by Matti Mäki.

files:
  Misc/ACKS         |   1 +
  Misc/NEWS         |   3 +++
  Modules/_struct.c |  18 ++++++++++++++----
  3 files changed, 18 insertions(+), 4 deletions(-)


diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -820,6 +820,7 @@
 Neil Muller
 Louis Munro
 R. David Murray
+Matti Mäki
 Dale Nagata
 John Nagle
 Takahiro Nakayama
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -62,6 +62,9 @@
 Library
 -------
 
+- Issue #14897: Enhance error messages of struct.pack and
+  struct.pack_into. Patch by Matti Mäki.
+
 - Issue #16316: mimetypes now recognizes the .xz and .txz (.tar.xz) extensions.
   Patch by Serhiy Storchaka.
 
diff --git a/Modules/_struct.c b/Modules/_struct.c
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -1661,7 +1661,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;
     }
 
@@ -1700,9 +1700,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;
     }
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list