From 92c28cace45261ae06c5361aef661a01f58a5413 Mon Sep 17 00:00:00 2001 From: Petri Lehtinen Date: Mon, 29 Oct 2012 21:16:57 +0200 Subject: #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. --- Modules/_struct.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'Modules/_struct.c') diff --git a/Modules/_struct.c b/Modules/_struct.c index 558d24bf8f..edbe9b9884 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; } -- cgit v1.2.1