diff options
| author | Petri Lehtinen <petri@digip.org> | 2012-10-29 21:25:01 +0200 |
|---|---|---|
| committer | Petri Lehtinen <petri@digip.org> | 2012-10-29 21:25:01 +0200 |
| commit | abaa19626bed9ef7d6b1cc62904d57de561200e6 (patch) | |
| tree | a60c52fa66e562344a7d54d771b0947a15d864ae | |
| parent | f7f136f6b8ae60b04b9fb2ce9a1223b5262c834d (diff) | |
| parent | d9d257efc980e5b40e0a2d0d7ff476cc220bacfa (diff) | |
| download | cpython-abaa19626bed9ef7d6b1cc62904d57de561200e6.tar.gz | |
#14897: Enhance error messages of struct.pack and struct.pack_into
Patch by Matti M?ki.
| -rw-r--r-- | Misc/ACKS | 1 | ||||
| -rw-r--r-- | Misc/NEWS | 3 | ||||
| -rw-r--r-- | Modules/_struct.c | 18 |
3 files changed, 18 insertions, 4 deletions
@@ -820,6 +820,7 @@ Michael Muller Neil Muller Louis Munro R. David Murray +Matti Mäki Dale Nagata John Nagle Takahiro Nakayama @@ -49,6 +49,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 0b20e26e6b..0cd0512dcf 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -1662,7 +1662,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; } @@ -1701,9 +1701,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; } |
