Skip to content

Commit 7168e98

Browse files
authored
gh-129813, PEP 782: Use PyBytesWriter in lzma and zlib (#138832)
Replace PyBytes_FromStringAndSize(NULL, size) with the new public PyBytesWriter API.
1 parent 430900d commit 7168e98

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

Modules/_lzmamodule.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,28 +1442,28 @@ _lzma__encode_filter_properties_impl(PyObject *module, lzma_filter filter)
14421442
{
14431443
lzma_ret lzret;
14441444
uint32_t encoded_size;
1445-
PyObject *result = NULL;
1445+
PyBytesWriter *writer = NULL;
14461446
_lzma_state *state = get_lzma_state(module);
14471447
assert(state != NULL);
14481448

14491449
lzret = lzma_properties_size(&encoded_size, &filter);
14501450
if (catch_lzma_error(state, lzret))
14511451
goto error;
14521452

1453-
result = PyBytes_FromStringAndSize(NULL, encoded_size);
1454-
if (result == NULL)
1453+
writer = PyBytesWriter_Create(encoded_size);
1454+
if (writer == NULL) {
14551455
goto error;
1456+
}
14561457

1457-
lzret = lzma_properties_encode(
1458-
&filter, (uint8_t *)PyBytes_AS_STRING(result));
1458+
lzret = lzma_properties_encode(&filter, PyBytesWriter_GetData(writer));
14591459
if (catch_lzma_error(state, lzret)) {
14601460
goto error;
14611461
}
14621462

1463-
return result;
1463+
return PyBytesWriter_Finish(writer);
14641464

14651465
error:
1466-
Py_XDECREF(result);
1466+
PyBytesWriter_Discard(writer);
14671467
return NULL;
14681468
}
14691469

Modules/zlibmodule.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -833,22 +833,24 @@ save_unconsumed_input(compobject *self, Py_buffer *data, int err)
833833
input data in self->unused_data. */
834834
if (self->zst.avail_in > 0) {
835835
Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
836-
Py_ssize_t new_size, left_size;
837-
PyObject *new_data;
836+
Py_ssize_t left_size;
838837
left_size = (Byte *)data->buf + data->len - self->zst.next_in;
839838
if (left_size > (PY_SSIZE_T_MAX - old_size)) {
840839
PyErr_NoMemory();
841840
return -1;
842841
}
843-
new_size = old_size + left_size;
844-
new_data = PyBytes_FromStringAndSize(NULL, new_size);
845-
if (new_data == NULL)
842+
PyBytesWriter *writer = PyBytesWriter_Create(old_size + left_size);
843+
if (writer == NULL) {
846844
return -1;
847-
memcpy(PyBytes_AS_STRING(new_data),
848-
PyBytes_AS_STRING(self->unused_data), old_size);
849-
memcpy(PyBytes_AS_STRING(new_data) + old_size,
850-
self->zst.next_in, left_size);
851-
Py_SETREF(self->unused_data, new_data);
845+
}
846+
char *new_data = PyBytesWriter_GetData(writer);
847+
memcpy(new_data, PyBytes_AS_STRING(self->unused_data), old_size);
848+
memcpy(new_data + old_size, self->zst.next_in, left_size);
849+
PyObject *new_unused_data = PyBytesWriter_Finish(writer);
850+
if (new_unused_data == NULL) {
851+
return -1;
852+
}
853+
Py_SETREF(self->unused_data, new_unused_data);
852854
self->zst.avail_in = 0;
853855
}
854856
}

0 commit comments

Comments
 (0)