diff options
author | Raymond Hettinger <python@rcn.com> | 2007-04-04 20:32:03 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2007-04-04 20:32:03 (GMT) |
commit | 3608f0570e634ac127ed9bce10d305d16a19e984 (patch) | |
tree | 9962e7dac2e1f8957cfefc47fffe22a1a47edab8 /Modules | |
parent | 8863544522d78e89907bfbe18754426fdce6edea (diff) | |
download | cpython-3608f0570e634ac127ed9bce10d305d16a19e984.zip cpython-3608f0570e634ac127ed9bce10d305d16a19e984.tar.gz cpython-3608f0570e634ac127ed9bce10d305d16a19e984.tar.bz2 |
Bug #1563759: struct.unpack doens't support buffer protocol objects
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_struct.c | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/Modules/_struct.c b/Modules/_struct.c index fb50987..ba276b3 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -1485,17 +1485,31 @@ strings."); static PyObject * s_unpack(PyObject *self, PyObject *inputstr) { + char *start; + int len; + PyObject * args; PyStructObject *soself = (PyStructObject *)self; assert(PyStruct_Check(self)); assert(soself->s_codes != NULL); - if (inputstr == NULL || !PyString_Check(inputstr) || - PyString_GET_SIZE(inputstr) != soself->s_size) { + if (inputstr != NULL && PyString_Check(inputstr) && + PyString_GET_SIZE(inputstr) == soself->s_size) { + return s_unpack_internal(soself, PyString_AS_STRING(inputstr)); + } + args = PyTuple_Pack(1, inputstr); + if (args == NULL) + return NULL; + if (!PyArg_ParseTuple(args, "s#:unpack", &start, &len)) { + Py_DECREF(args); + return NULL; + } + Py_DECREF(args); + if (soself->s_size != len) { PyErr_Format(StructError, "unpack requires a string argument of length %zd", soself->s_size); return NULL; } - return s_unpack_internal(soself, PyString_AS_STRING(inputstr)); + return s_unpack_internal(soself, start); } PyDoc_STRVAR(s_unpack_from__doc__, |