Skip to content

Commit 82e1920

Browse files
authored
gh-129813, PEP 782: Use PyBytesWriter in _testclinic (#139048)
Replace PyBytes_FromStringAndSize(NULL, size) with the new public PyBytesWriter API.
1 parent 8eb1062 commit 82e1920

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Modules/_testclinic.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -663,16 +663,16 @@ str_converter_encoding_impl(PyObject *module, char *a, char *b, char *c,
663663
static PyObject *
664664
bytes_from_buffer(Py_buffer *buf)
665665
{
666-
PyObject *bytes_obj = PyBytes_FromStringAndSize(NULL, buf->len);
667-
if (!bytes_obj) {
666+
PyBytesWriter *writer = PyBytesWriter_Create(buf->len);
667+
if (writer == NULL) {
668668
return NULL;
669669
}
670-
void *bytes_obj_buf = ((PyBytesObject *)bytes_obj)->ob_sval;
671-
if (PyBuffer_ToContiguous(bytes_obj_buf, buf, buf->len, 'C') < 0) {
672-
Py_DECREF(bytes_obj);
670+
void *data = PyBytesWriter_GetData(writer);
671+
if (PyBuffer_ToContiguous(data, buf, buf->len, 'C') < 0) {
672+
PyBytesWriter_Discard(writer);
673673
return NULL;
674674
}
675-
return bytes_obj;
675+
return PyBytesWriter_Finish(writer);
676676
}
677677

678678
/*[clinic input]

0 commit comments

Comments
 (0)