From: Victor Stinner Date: Wed, 17 Sep 2025 15:43:30 +0000 (+0100) Subject: gh-129813, PEP 782: Use PyBytesWriter in _Py_bytes_maketrans() (#139044) X-Git-Tag: v3.15.0a1~306 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=263242613f1d81214dd0334b159f35854e72986a;p=thirdparty%2FPython%2Fcpython.git gh-129813, PEP 782: Use PyBytesWriter in _Py_bytes_maketrans() (#139044) Replace PyBytes_FromStringAndSize(NULL, size) with the new public PyBytesWriter API. --- diff --git a/Objects/bytes_methods.c b/Objects/bytes_methods.c index c239ae18a593..56a461d0dd08 100644 --- a/Objects/bytes_methods.c +++ b/Objects/bytes_methods.c @@ -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