https://github.com/python/cpython/commit/374851f3dbfbce3faf1f7a1875454aa67cf6ae4b
commit: 374851f3dbfbce3faf1f7a1875454aa67cf6ae4b
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-09-01T15:33:36+02:00
summary:

gh-155742: Use PyBytesWriter in CJK codecs (#155746)

Replace soft deprecated PyBytes_FromStringAndSize() with
PyBytesWriter.

files:
M Modules/cjkcodecs/multibytecodec.c

diff --git a/Modules/cjkcodecs/multibytecodec.c 
b/Modules/cjkcodecs/multibytecodec.c
index d90900457574d9..5eb1533bdfc118 100644
--- a/Modules/cjkcodecs/multibytecodec.c
+++ b/Modules/cjkcodecs/multibytecodec.c
@@ -1482,23 +1482,25 @@ mbstreamreader_iread(MultibyteStreamReaderObject *self,
         endoffile = (PyBytes_GET_SIZE(cres) == 0);
 
         if (self->pendingsize > 0) {
-            PyObject *ctr;
-            char *ctrdata;
-
             if (PyBytes_GET_SIZE(cres) > PY_SSIZE_T_MAX - self->pendingsize) {
                 PyErr_NoMemory();
                 goto errorexit;
             }
             rsize = PyBytes_GET_SIZE(cres) + self->pendingsize;
-            ctr = PyBytes_FromStringAndSize(NULL, rsize);
-            if (ctr == NULL)
+
+            PyBytesWriter *writer = PyBytesWriter_Create(rsize);
+            if (writer == NULL) {
                 goto errorexit;
-            ctrdata = PyBytes_AS_STRING(ctr);
+            }
+            char *ctrdata = PyBytesWriter_GetData(writer);
             memcpy(ctrdata, self->pending, self->pendingsize);
             memcpy(ctrdata + self->pendingsize,
                     PyBytes_AS_STRING(cres),
                     PyBytes_GET_SIZE(cres));
-            Py_SETREF(cres, ctr);
+            Py_SETREF(cres, PyBytesWriter_Finish(writer));
+            if (cres == NULL) {
+                goto errorexit;
+            }
             self->pendingsize = 0;
         }
 

_______________________________________________
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