Mercurial > cpython-lru_cache-weakref
diff Modules/_struct.c @ 41391:80ff75fa2754 legacy-trunk
Bug #1563759: struct.unpack doens't support buffer protocol objects
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Thu, 05 Apr 2007 18:00:03 +0000 |
| parents | 0bffca64ccc4 |
| children | 708c8c5fc173 |
line wrap: on
line diff
--- a/Modules/_struct.c Wed Apr 04 18:38:47 2007 +0000 +++ b/Modules/_struct.c Thu Apr 05 18:00:03 2007 +0000 @@ -1534,17 +1534,35 @@ static PyObject * s_unpack(PyObject *self, PyObject *inputstr) { + char *start; + Py_ssize_t len; + PyObject *args=NULL, *result; 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) { - PyErr_Format(StructError, - "unpack requires a string argument of length %zd", - soself->s_size); + if (inputstr == NULL) + goto fail; + if (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; - } - return s_unpack_internal(soself, PyString_AS_STRING(inputstr)); + if (!PyArg_ParseTuple(args, "s#:unpack", &start, &len)) + goto fail; + if (soself->s_size != len) + goto fail; + result = s_unpack_internal(soself, start); + Py_DECREF(args); + return result; + +fail: + Py_XDECREF(args); + PyErr_Format(StructError, + "unpack requires a string argument of length %zd", + soself->s_size); + return NULL; } PyDoc_STRVAR(s_unpack_from__doc__,
