summaryrefslogtreecommitdiff
path: root/Modules/_struct.c
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2007-04-04 20:32:03 +0000
committerRaymond Hettinger <python@rcn.com>2007-04-04 20:32:03 +0000
commit685bdb06ad2efa9602f2cfd938518de39000d67b (patch)
tree498d64c6731654dc7094d760cd8933ed4d8f7caf /Modules/_struct.c
parente3dcb878f60a72264e363bd2a9a7d566808a9d26 (diff)
downloadcpython-685bdb06ad2efa9602f2cfd938518de39000d67b.tar.gz
Bug #1563759: struct.unpack doens't support buffer protocol objects
Diffstat (limited to 'Modules/_struct.c')
-rw-r--r--Modules/_struct.c20
1 files changed, 17 insertions, 3 deletions
diff --git a/Modules/_struct.c b/Modules/_struct.c
index fb509872a7..ba276b364e 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__,