From 5c89c19eae988245a826dc1f750c55ebb5f329bc Mon Sep 17 00:00:00 2001 From: Petri Lehtinen Date: Mon, 29 Oct 2012 21:16:57 +0200 Subject: [PATCH] #14897: Enhance error messages of struct.pack and struct.pack_into MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Patch by Matti Mäki. --- 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 index 2abed589926..129a116d8db 100644 --- 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 diff --git a/Misc/NEWS b/Misc/NEWS index f384a2e8708..11113f3006a 100644 --- 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

tags in text mode when the logdir option is specified. diff --git a/Modules/_struct.c b/Modules/_struct.c index c4c1dfbb955..d8c932427d6 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -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; }