summaryrefslogtreecommitdiffstats
path: root/Modules/_struct.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_struct.c')
-rw-r--r--Modules/_struct.c22
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;
}