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:20:19 (GMT) |
commit | 5c89c19eae988245a826dc1f750c55ebb5f329bc (patch) | |
tree | 048025a8abe5a1aca3498129c5649eb977d27257 /Modules/_struct.c | |
parent | 64c0b2ca3da8ffcaf8a8cf150a59eeb300326529 (diff) | |
download | cpython-5c89c19eae988245a826dc1f750c55ebb5f329bc.zip cpython-5c89c19eae988245a826dc1f750c55ebb5f329bc.tar.gz cpython-5c89c19eae988245a826dc1f750c55ebb5f329bc.tar.bz2 |
#14897: Enhance error messages of struct.pack and struct.pack_into
Patch by Matti Mäki.
Diffstat (limited to 'Modules/_struct.c')
-rw-r--r-- | Modules/_struct.c | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/Modules/_struct.c b/Modules/_struct.c index c4c1dfb..d8c9324 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; } |