https://github.com/python/cpython/commit/263242613f1d81214dd0334b159f35854e72986a
commit: 263242613f1d81214dd0334b159f35854e72986a
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2025-09-17T17:43:30+02:00
summary:

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

Replace PyBytes_FromStringAndSize(NULL, size) with the new public
PyBytesWriter API.

files:
M Objects/bytes_methods.c

diff --git a/Objects/bytes_methods.c b/Objects/bytes_methods.c
index c239ae18a593e3..56a461d0dd08a7 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

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to