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

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


http://hg.python.org/cpython/rev/a555bd4026b0
changeset:   80051:a555bd4026b0
branch:      3.2
parent:      80047:c6787a4c544e
user:        Petri Lehtinen <petri at digip.org>
date:        Mon Oct 29 21:16:57 2012 +0200
summary:
  #14897: Enhance error messages of struct.pack and struct.pack_into

Patch by Matti Mäki.

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


diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -710,6 +710,7 @@
 Luke Mewburn
 Carl Meyer
 Mike Meyer
+Piotr Meyer
 Steven Miale
 Trent Mick
 Tom Middleton
@@ -742,7 +743,7 @@
 Michael Muller
 Neil Muller
 R. David Murray
-Piotr Meyer
+Matti Mäki
 Dale Nagata
 John Nagle
 Takahiro Nakayama
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -135,6 +135,9 @@
 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.
 
diff --git a/Modules/_struct.c b/Modules/_struct.c
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -1575,7 +1575,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;
     }
 
@@ -1614,9 +1614,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