diff options
author | Petri Lehtinen <petri@digip.org> | 2012-10-29 19:16:57 (GMT) |
---|---|---|
committer | Petri Lehtinen <petri@digip.org> | 2012-10-29 19:24:07 (GMT) |
commit | 92c28cace45261ae06c5361aef661a01f58a5413 (patch) | |
tree | 3b35c7ba419651bd5531c83124c5f90b42e5ce7e | |
parent | 1b863ebfc8376700d571102d7ff0017ff1ef5b4e (diff) | |
download | cpython-92c28cace45261ae06c5361aef661a01f58a5413.zip cpython-92c28cace45261ae06c5361aef661a01f58a5413.tar.gz cpython-92c28cace45261ae06c5361aef661a01f58a5413.tar.bz2 |
#14897: Enhance error messages of struct.pack and struct.pack_into
Patch by Matti Mäki.
-rw-r--r-- | Misc/ACKS | 3 | ||||
-rw-r--r-- | Misc/NEWS | 3 | ||||
-rw-r--r-- | Modules/_struct.c | 18 |
3 files changed, 19 insertions, 5 deletions
@@ -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 @@ -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. diff --git a/Modules/_struct.c b/Modules/_struct.c index 558d24b..edbe9b9 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -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; } |