gh-129813, PEP 782: Use PyBytesWriter in _Py_bytes_maketrans() (#139044)

Replace PyBytes_FromStringAndSize(NULL, size) with the new public
PyBytesWriter API.
This commit is contained in:
Victor Stinner
2025-09-17 16:43:30 +01:00
committed by GitHub
parent dd0840bf67
commit 263242613f

View File

@@ -356,26 +356,24 @@ The bytes objects frm and to must be of the same length.");
PyObject *
_Py_bytes_maketrans(Py_buffer *frm, Py_buffer *to)
{
PyObject *res = NULL;
Py_ssize_t i;
char *p;
if (frm->len != to->len) {
PyErr_Format(PyExc_ValueError,
"maketrans arguments must have same length");
return NULL;
}
res = PyBytes_FromStringAndSize(NULL, 256);
if (!res)
PyBytesWriter *writer = PyBytesWriter_Create(256);
if (!writer) {
return NULL;
p = PyBytes_AS_STRING(res);
}
char *p = PyBytesWriter_GetData(writer);
Py_ssize_t i;
for (i = 0; i < 256; i++)
p[i] = (char) i;
for (i = 0; i < frm->len; i++) {
p[((unsigned char *)frm->buf)[i]] = ((char *)to->buf)[i];
}
return res;
return PyBytesWriter_Finish(writer);
}
#define FASTSEARCH fastsearch