Skip to content

Commit 2632426

Browse files
authored
gh-129813, PEP 782: Use PyBytesWriter in _Py_bytes_maketrans() (#139044)
Replace PyBytes_FromStringAndSize(NULL, size) with the new public PyBytesWriter API.
1 parent dd0840b commit 2632426

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

Objects/bytes_methods.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -356,26 +356,24 @@ The bytes objects frm and to must be of the same length.");
356356
PyObject *
357357
_Py_bytes_maketrans(Py_buffer *frm, Py_buffer *to)
358358
{
359-
PyObject *res = NULL;
360-
Py_ssize_t i;
361-
char *p;
362-
363359
if (frm->len != to->len) {
364360
PyErr_Format(PyExc_ValueError,
365361
"maketrans arguments must have same length");
366362
return NULL;
367363
}
368-
res = PyBytes_FromStringAndSize(NULL, 256);
369-
if (!res)
364+
PyBytesWriter *writer = PyBytesWriter_Create(256);
365+
if (!writer) {
370366
return NULL;
371-
p = PyBytes_AS_STRING(res);
367+
}
368+
char *p = PyBytesWriter_GetData(writer);
369+
Py_ssize_t i;
372370
for (i = 0; i < 256; i++)
373371
p[i] = (char) i;
374372
for (i = 0; i < frm->len; i++) {
375373
p[((unsigned char *)frm->buf)[i]] = ((char *)to->buf)[i];
376374
}
377375

378-
return res;
376+
return PyBytesWriter_Finish(writer);
379377
}
380378

381379
#define FASTSEARCH fastsearch

0 commit comments

Comments
 (0)