Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1477,12 +1477,16 @@ bytes_concat(PyObject *a, PyObject *b)
goto done;
}

result = PyBytes_FromStringAndSize(NULL, va.len + vb.len);
if (result != NULL) {
memcpy(PyBytes_AS_STRING(result), va.buf, va.len);
memcpy(PyBytes_AS_STRING(result) + va.len, vb.buf, vb.len);
PyBytesWriter *writer = PyBytesWriter_Create(va.len + vb.len);
if (writer == NULL) {
goto done;
}

char *data = PyBytesWriter_GetData(writer);
memcpy(data, va.buf, va.len);
memcpy(data + va.len, vb.buf, vb.len);
result = PyBytesWriter_Finish(writer);

done:
if (va.len != -1)
PyBuffer_Release(&va);
Expand Down Expand Up @@ -1659,8 +1663,6 @@ bytes_subscript(PyObject *op, PyObject* item)
Py_ssize_t start, stop, step, slicelength, i;
size_t cur;
const char* source_buf;
char* result_buf;
PyObject* result;

if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
return NULL;
Expand All @@ -1683,17 +1685,18 @@ bytes_subscript(PyObject *op, PyObject* item)
}
else {
source_buf = PyBytes_AS_STRING(self);
result = PyBytes_FromStringAndSize(NULL, slicelength);
if (result == NULL)
PyBytesWriter *writer = PyBytesWriter_Create(slicelength);
if (writer == NULL) {
return NULL;
}
char *buf = PyBytesWriter_GetData(writer);

result_buf = PyBytes_AS_STRING(result);
for (cur = start, i = 0; i < slicelength;
cur += step, i++) {
result_buf[i] = source_buf[cur];
buf[i] = source_buf[cur];
}

return result;
return PyBytesWriter_Finish(writer);
}
}
else {
Expand Down
Loading