From c4da666470b49224d50da545fd2e6b0665972a90 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 12 Sep 2026 03:23:52 +0200 Subject: [PATCH] gh-155742: Use PyBytesWriter in codeobject.c Replace soft deprecated _PyBytes_Resize() with PyBytesWriter. --- Objects/codeobject.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 58811d63c7e318..6b4bae40cc77de 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -647,19 +647,19 @@ remove_column_info(PyObject *locations) { Py_ssize_t offset = 0; const uint8_t *data = (const uint8_t *)PyBytes_AS_STRING(locations); - PyObject *res = PyBytes_FromStringAndSize(NULL, 32); + PyBytesWriter *res = PyBytesWriter_Create(32); if (res == NULL) { - PyErr_NoMemory(); return NULL; } - uint8_t *output = (uint8_t *)PyBytes_AS_STRING(res); + uint8_t *output = (uint8_t *)PyBytesWriter_GetData(res); while (offset < PyBytes_GET_SIZE(locations)) { - Py_ssize_t write_offset = output - (uint8_t *)PyBytes_AS_STRING(res); - if (write_offset + 16 >= PyBytes_GET_SIZE(res)) { - if (_PyBytes_Resize(&res, PyBytes_GET_SIZE(res) * 2) < 0) { + Py_ssize_t write_offset = output - (uint8_t *)PyBytesWriter_GetData(res); + if (write_offset + 16 >= PyBytesWriter_GetSize(res)) { + if (PyBytesWriter_Resize(res, PyBytesWriter_GetSize(res) * 2) < 0) { + PyBytesWriter_Discard(res); return NULL; } - output = (uint8_t *)PyBytes_AS_STRING(res) + write_offset; + output = (uint8_t *)PyBytesWriter_GetData(res) + write_offset; } int code = (data[offset] >> 3) & 15; if (code == PY_CODE_LOCATION_INFO_NONE) { @@ -678,11 +678,7 @@ remove_column_info(PyObject *locations) offset++; } } - Py_ssize_t write_offset = output - (uint8_t *)PyBytes_AS_STRING(res); - if (_PyBytes_Resize(&res, write_offset)) { - return NULL; - } - return res; + return PyBytesWriter_FinishWithPointer(res, output); } static int