{
Py_buffer *src = VIEW_ADDR(self);
char ord = 'C';
- PyObject *bytes;
CHECK_RELEASED(self);
}
}
- 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]
/*[clinic end generated code: output=430ca760f94f3ca7 input=539f6a3a5fb56946]*/
{
Py_buffer *src = VIEW_ADDR(self);
- PyObject *bytes;
- PyObject *ret;
CHECK_RELEASED(self);
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;
}