Skip to content

Commit c4da666

Browse files
committed
gh-155742: Use PyBytesWriter in codeobject.c
Replace soft deprecated _PyBytes_Resize() with PyBytesWriter.
1 parent f5dd52d commit c4da666

1 file changed

Lines changed: 8 additions & 12 deletions

File tree

Objects/codeobject.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -647,19 +647,19 @@ remove_column_info(PyObject *locations)
647647
{
648648
Py_ssize_t offset = 0;
649649
const uint8_t *data = (const uint8_t *)PyBytes_AS_STRING(locations);
650-
PyObject *res = PyBytes_FromStringAndSize(NULL, 32);
650+
PyBytesWriter *res = PyBytesWriter_Create(32);
651651
if (res == NULL) {
652-
PyErr_NoMemory();
653652
return NULL;
654653
}
655-
uint8_t *output = (uint8_t *)PyBytes_AS_STRING(res);
654+
uint8_t *output = (uint8_t *)PyBytesWriter_GetData(res);
656655
while (offset < PyBytes_GET_SIZE(locations)) {
657-
Py_ssize_t write_offset = output - (uint8_t *)PyBytes_AS_STRING(res);
658-
if (write_offset + 16 >= PyBytes_GET_SIZE(res)) {
659-
if (_PyBytes_Resize(&res, PyBytes_GET_SIZE(res) * 2) < 0) {
656+
Py_ssize_t write_offset = output - (uint8_t *)PyBytesWriter_GetData(res);
657+
if (write_offset + 16 >= PyBytesWriter_GetSize(res)) {
658+
if (PyBytesWriter_Resize(res, PyBytesWriter_GetSize(res) * 2) < 0) {
659+
PyBytesWriter_Discard(res);
660660
return NULL;
661661
}
662-
output = (uint8_t *)PyBytes_AS_STRING(res) + write_offset;
662+
output = (uint8_t *)PyBytesWriter_GetData(res) + write_offset;
663663
}
664664
int code = (data[offset] >> 3) & 15;
665665
if (code == PY_CODE_LOCATION_INFO_NONE) {
@@ -678,11 +678,7 @@ remove_column_info(PyObject *locations)
678678
offset++;
679679
}
680680
}
681-
Py_ssize_t write_offset = output - (uint8_t *)PyBytes_AS_STRING(res);
682-
if (_PyBytes_Resize(&res, write_offset)) {
683-
return NULL;
684-
}
685-
return res;
681+
return PyBytesWriter_FinishWithPointer(res, output);
686682
}
687683

688684
static int

0 commit comments

Comments
 (0)