Mercurial > cpython-lru_cache-weakref
diff Modules/_struct.c @ 41386:4c7ebc397d94 2.5
Bug #1563759: struct.unpack doens't support buffer protocol objects
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Wed, 04 Apr 2007 20:32:03 +0000 |
| parents | a2f26f944b65 |
| children | 812027f1eea5 |
line wrap: on
line diff
--- a/Modules/_struct.c Wed Apr 04 18:30:56 2007 +0000 +++ b/Modules/_struct.c Wed Apr 04 20:32:03 2007 +0000 @@ -1485,17 +1485,31 @@ 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__,
