FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-129813, PEP 782: Use PyBytesWriter in lzma and zlib by vstinner · Pull Request #138832 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 7 additions & 7 deletions Modules/_lzmamodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1445,28 +1445,28 @@ _lzma__encode_filter_properties_impl(PyObject *module, lzma_filter filter)
{
lzma_ret lzret;
uint32_t encoded_size;
PyObject *result = NULL;
PyBytesWriter *writer = NULL;
_lzma_state *state = get_lzma_state(module);
assert(state != NULL);

lzret = lzma_properties_size(&encoded_size, &filter);
if (catch_lzma_error(state, lzret))
goto error;

result = PyBytes_FromStringAndSize(NULL, encoded_size);
if (result == NULL)
writer = PyBytesWriter_Create(encoded_size);
if (writer == NULL) {
goto error;
}

lzret = lzma_properties_encode(
&filter, (uint8_t *)PyBytes_AS_STRING(result));
lzret = lzma_properties_encode(&filter, PyBytesWriter_GetData(writer));
if (catch_lzma_error(state, lzret)) {
goto error;
}

return result;
return PyBytesWriter_Finish(writer);

error:
Py_XDECREF(result);
PyBytesWriter_Discard(writer);
return NULL;
}

Expand Down
22 changes: 12 additions & 10 deletions Modules/zlibmodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -833,22 +833,24 @@ save_unconsumed_input(compobject *self, Py_buffer *data, int err)
input data in self->unused_data. */
if (self->zst.avail_in > 0) {
Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
Py_ssize_t new_size, left_size;
PyObject *new_data;
Py_ssize_t left_size;
left_size = (Byte *)data->buf + data->len - self->zst.next_in;
if (left_size > (PY_SSIZE_T_MAX - old_size)) {
PyErr_NoMemory();
return -1;
}
new_size = old_size + left_size;
new_data = PyBytes_FromStringAndSize(NULL, new_size);
if (new_data == NULL)
PyBytesWriter *writer = PyBytesWriter_Create(old_size + left_size);
if (writer == NULL) {
return -1;
memcpy(PyBytes_AS_STRING(new_data),
PyBytes_AS_STRING(self->unused_data), old_size);
memcpy(PyBytes_AS_STRING(new_data) + old_size,
self->zst.next_in, left_size);
Py_SETREF(self->unused_data, new_data);
}
char *new_data = PyBytesWriter_GetData(writer);
memcpy(new_data, PyBytes_AS_STRING(self->unused_data), old_size);
memcpy(new_data + old_size, self->zst.next_in, left_size);
PyObject *new_unused_data = PyBytesWriter_Finish(writer);
if (new_unused_data == NULL) {
return -1;
}
Py_SETREF(self->unused_data, new_unused_data);
self->zst.avail_in = 0;
}
}
Expand Down
Loading

Back | FazBrowse Home | New Git URL