diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-02-21 17:51:17 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-02-21 17:51:17 (GMT) |
commit | b8285d96f40a635157b0394f7cf9e69d0e6492cf (patch) | |
tree | 488dc04feb71eb05a252e37b48eabc8d9bf4e673 /Modules/_struct.c | |
parent | 4809d1fccd81bd3e1e4b08152545cfd88b69231c (diff) | |
download | cpython-b8285d96f40a635157b0394f7cf9e69d0e6492cf.zip cpython-b8285d96f40a635157b0394f7cf9e69d0e6492cf.tar.gz cpython-b8285d96f40a635157b0394f7cf9e69d0e6492cf.tar.bz2 |
Issue #22113: struct.pack_into() now supports new buffer protocol (in
particular accepts writable memoryview).
Diffstat (limited to 'Modules/_struct.c')
-rw-r--r-- | Modules/_struct.c | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/Modules/_struct.c b/Modules/_struct.c index f035847..bca7a2e 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -1657,8 +1657,8 @@ static PyObject * s_pack_into(PyObject *self, PyObject *args) { PyStructObject *soself; - char *buffer; - Py_ssize_t buffer_len, offset; + Py_buffer buf; + Py_ssize_t offset; /* Validate arguments. +1 is for the first arg as buffer. */ soself = (PyStructObject *)self; @@ -1683,33 +1683,35 @@ s_pack_into(PyObject *self, PyObject *args) } /* Extract a writable memory buffer from the first argument */ - if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0), - (void**)&buffer, &buffer_len) == -1 ) { + if (!PyArg_Parse(PyTuple_GET_ITEM(args, 0), "w*", &buf)) return NULL; - } - assert( buffer_len >= 0 ); /* Extract the offset from the first argument */ offset = PyInt_AsSsize_t(PyTuple_GET_ITEM(args, 1)); - if (offset == -1 && PyErr_Occurred()) + if (offset == -1 && PyErr_Occurred()) { + PyBuffer_Release(&buf); return NULL; + } /* Support negative offsets. */ if (offset < 0) - offset += buffer_len; + offset += buf.len; /* Check boundaries */ - if (offset < 0 || (buffer_len - offset) < soself->s_size) { + if (offset < 0 || (buf.len - offset) < soself->s_size) { PyErr_Format(StructError, "pack_into requires a buffer of at least %zd bytes", soself->s_size); + PyBuffer_Release(&buf); return NULL; } /* Call the guts */ - if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) { + if (s_pack_internal(soself, args, 2, (char *)buf.buf + offset) != 0) { + PyBuffer_Release(&buf); return NULL; } + PyBuffer_Release(&buf); Py_RETURN_NONE; } |