Skip to content

Commit 9b4154c

Browse files
committed
gh-155742: Use PyBytesWriter in codecs
Replace soft deprecated PyBytes_FromStringAndSize() with PyBytesWriter.
1 parent cd98657 commit 9b4154c

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

Python/codecs.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,26 +1426,27 @@ _PyCodec_SurrogateEscapeUnicodeEncodeError(PyObject *exc)
14261426
return NULL;
14271427
}
14281428

1429-
PyObject *res = PyBytes_FromStringAndSize(NULL, slen);
1430-
if (res == NULL) {
1429+
PyBytesWriter *writer = PyBytesWriter_Create(slen);
1430+
if (writer == NULL) {
14311431
Py_DECREF(obj);
14321432
return NULL;
14331433
}
14341434

1435-
char *outp = PyBytes_AsString(res);
1435+
char *outp = PyBytesWriter_GetData(writer);
14361436
for (Py_ssize_t i = start; i < end; i++) {
14371437
Py_UCS4 ch = PyUnicode_READ_CHAR(obj, i);
14381438
if (ch < 0xdc80 || ch > 0xdcff) {
14391439
/* Not a UTF-8b surrogate, fail with original exception. */
14401440
Py_DECREF(obj);
1441-
Py_DECREF(res);
1441+
PyBytesWriter_Discard(writer);
14421442
PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
14431443
return NULL;
14441444
}
14451445
*outp++ = ch - 0xdc00;
14461446
}
14471447
Py_DECREF(obj);
14481448

1449+
PyObject *res = PyBytesWriter_Finish(writer); // can be NULL
14491450
return Py_BuildValue("(Nn)", res, end);
14501451
}
14511452

0 commit comments

Comments
 (0)