Skip to content

Commit 237e1dd

Browse files
authored
gh-155742: Use PyBytesWriter in CJK codecs (#157535)
Replace soft deprecated PyBytes_FromStringAndSize() with PyBytesWriter. Replace PyBytes_FromStringAndSize(NULL, 0) with Py_GetConstant(Py_CONSTANT_EMPTY_BYTES).
1 parent 0fc5ebc commit 237e1dd

1 file changed

Lines changed: 17 additions & 23 deletions

File tree

Modules/cjkcodecs/multibytecodec.c

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ typedef struct {
8282
PyObject *inobj;
8383
Py_ssize_t inpos, inlen;
8484
unsigned char *outbuf, *outbuf_end;
85-
PyObject *excobj, *outobj;
85+
PyObject *excobj;
86+
PyBytesWriter *writer;
8687
} MultibyteEncodeBuffer;
8788

8889
typedef struct {
@@ -209,21 +210,21 @@ expand_encodebuffer(MultibyteEncodeBuffer *buf, Py_ssize_t esize)
209210
Py_ssize_t orgpos, orgsize, incsize;
210211

211212
orgpos = (Py_ssize_t)((char *)buf->outbuf -
212-
PyBytes_AS_STRING(buf->outobj));
213-
orgsize = PyBytes_GET_SIZE(buf->outobj);
213+
(char *)PyBytesWriter_GetData(buf->writer));
214+
orgsize = PyBytesWriter_GetSize(buf->writer);
214215
incsize = (esize < (orgsize >> 1) ? (orgsize >> 1) | 1 : esize);
215216

216217
if (orgsize > PY_SSIZE_T_MAX - incsize) {
217218
PyErr_NoMemory();
218219
return -1;
219220
}
220221

221-
if (_PyBytes_Resize(&buf->outobj, orgsize + incsize) == -1)
222+
if (PyBytesWriter_Resize(buf->writer, orgsize + incsize) == -1)
222223
return -1;
223224

224-
buf->outbuf = (unsigned char *)PyBytes_AS_STRING(buf->outobj) +orgpos;
225-
buf->outbuf_end = (unsigned char *)PyBytes_AS_STRING(buf->outobj)
226-
+ PyBytes_GET_SIZE(buf->outobj);
225+
unsigned char *data = PyBytesWriter_GetData(buf->writer);
226+
buf->outbuf = data + orgpos;
227+
buf->outbuf_end = data + PyBytesWriter_GetSize(buf->writer);
227228

228229
return 0;
229230
}
@@ -503,18 +504,18 @@ multibytecodec_encode(const MultibyteCodec *codec,
503504
PyObject *errors, int flags)
504505
{
505506
MultibyteEncodeBuffer buf;
506-
Py_ssize_t finalsize, r = 0;
507+
Py_ssize_t r = 0;
507508
Py_ssize_t datalen;
508509
int kind;
509510
const void *data;
510511

511512
datalen = PyUnicode_GET_LENGTH(text);
512513

513514
if (datalen == 0 && !(flags & MBENC_RESET))
514-
return PyBytes_FromStringAndSize(NULL, 0);
515+
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
515516

516517
buf.excobj = NULL;
517-
buf.outobj = NULL;
518+
buf.writer = NULL;
518519
buf.inobj = text; /* borrowed reference */
519520
buf.inpos = 0;
520521
buf.inlen = datalen;
@@ -526,11 +527,11 @@ multibytecodec_encode(const MultibyteCodec *codec,
526527
goto errorexit;
527528
}
528529

529-
buf.outobj = PyBytes_FromStringAndSize(NULL, datalen * 2 + 16);
530-
if (buf.outobj == NULL)
530+
buf.writer = PyBytesWriter_Create(datalen * 2 + 16);
531+
if (buf.writer == NULL)
531532
goto errorexit;
532-
buf.outbuf = (unsigned char *)PyBytes_AS_STRING(buf.outobj);
533-
buf.outbuf_end = buf.outbuf + PyBytes_GET_SIZE(buf.outobj);
533+
buf.outbuf = (unsigned char *)PyBytesWriter_GetData(buf.writer);
534+
buf.outbuf_end = buf.outbuf + PyBytesWriter_GetSize(buf.writer);
534535

535536
while (buf.inpos < buf.inlen) {
536537
/* we don't reuse inleft and outleft here.
@@ -563,21 +564,14 @@ multibytecodec_encode(const MultibyteCodec *codec,
563564
goto errorexit;
564565
}
565566

566-
finalsize = (Py_ssize_t)((char *)buf.outbuf -
567-
PyBytes_AS_STRING(buf.outobj));
568-
569-
if (finalsize != PyBytes_GET_SIZE(buf.outobj))
570-
if (_PyBytes_Resize(&buf.outobj, finalsize) == -1)
571-
goto errorexit;
572-
573567
if (inpos_t)
574568
*inpos_t = buf.inpos;
575569
Py_XDECREF(buf.excobj);
576-
return buf.outobj;
570+
return PyBytesWriter_FinishWithPointer(buf.writer, buf.outbuf);
577571

578572
errorexit:
579573
Py_XDECREF(buf.excobj);
580-
Py_XDECREF(buf.outobj);
574+
PyBytesWriter_Discard(buf.writer);
581575
return NULL;
582576
}
583577

0 commit comments

Comments
 (0)