Skip to content
Merged
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
34 changes: 18 additions & 16 deletions Objects/memoryobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2285,7 +2285,6 @@ memoryview_tobytes_impl(PyMemoryViewObject *self, const char *order)
{
Py_buffer *src = VIEW_ADDR(self);
char ord = 'C';
PyObject *bytes;

CHECK_RELEASED(self);

Expand All @@ -2303,16 +2302,18 @@ memoryview_tobytes_impl(PyMemoryViewObject *self, const char *order)
}
}

bytes = PyBytes_FromStringAndSize(NULL, src->len);
if (bytes == NULL)
PyBytesWriter *writer = PyBytesWriter_Create(src->len);
if (writer == NULL) {
return NULL;
}

if (PyBuffer_ToContiguous(PyBytes_AS_STRING(bytes), src, src->len, ord) < 0) {
Py_DECREF(bytes);
if (PyBuffer_ToContiguous(PyBytesWriter_GetData(writer),
src, src->len, ord) < 0) {
PyBytesWriter_Discard(writer);
return NULL;
}

return bytes;
return PyBytesWriter_Finish(writer);
}

/*[clinic input]
Expand Down Expand Up @@ -2344,28 +2345,29 @@ memoryview_hex_impl(PyMemoryViewObject *self, PyObject *sep,
/*[clinic end generated code: output=430ca760f94f3ca7 input=539f6a3a5fb56946]*/
{
Py_buffer *src = VIEW_ADDR(self);
PyObject *bytes;
PyObject *ret;

CHECK_RELEASED(self);

if (MV_C_CONTIGUOUS(self->flags)) {
return _Py_strhex_with_sep(src->buf, src->len, sep, bytes_per_sep);
}

bytes = PyBytes_FromStringAndSize(NULL, src->len);
if (bytes == NULL)
PyBytesWriter *writer = PyBytesWriter_Create(src->len);
if (writer == NULL) {
return NULL;
}

if (PyBuffer_ToContiguous(PyBytes_AS_STRING(bytes), src, src->len, 'C') < 0) {
Py_DECREF(bytes);
if (PyBuffer_ToContiguous(PyBytesWriter_GetData(writer),
src, src->len, 'C') < 0) {
PyBytesWriter_Discard(writer);
return NULL;
}

ret = _Py_strhex_with_sep(
PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes),
sep, bytes_per_sep);
Py_DECREF(bytes);
PyObject *ret = _Py_strhex_with_sep(
PyBytesWriter_GetData(writer),
PyBytesWriter_GetSize(writer),
sep, bytes_per_sep);
PyBytesWriter_Discard(writer);

return ret;
}
Expand Down
Loading