]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-129813, PEP 782: Use PyBytesWriter in _Py_bytes_maketrans() (#139044)
authorVictor Stinner <vstinner@python.org>
Wed, 17 Sep 2025 15:43:30 +0000 (16:43 +0100)
committerGitHub <noreply@github.com>
Wed, 17 Sep 2025 15:43:30 +0000 (17:43 +0200)
Replace PyBytes_FromStringAndSize(NULL, size) with the new public
PyBytesWriter API.

Objects/bytes_methods.c

index c239ae18a593e3cd7e70f73363c318dbea9694d6..56a461d0dd08a78af3c8e5b6d6ac89e98891b705 100644 (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