Skip to content

Commit

Permalink
Full-circle back to bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
dcwatson committed May 9, 2024
1 parent 50e7d7e commit c5f49c1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
5 changes: 0 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

* Switched to [scikit-build-core](https://github.com/scikit-build/scikit-build-core) and
CMake (#42, #43) - huge thanks to @henryiii
* Use only [stable Python API](https://docs.python.org/3/c-api/stable.html)
* _Note that this can't actually be enabled until Python 3.11 is the minimum
supported version._
* Return [bytearray](https://docs.python.org/3/library/stdtypes.html#bytearray) from
compression and decompression methods, to avoid copying (resizing bytes is not stable)
* Raise `ValueError` instead of `DeflateError` on invalid gzip data


Expand Down
19 changes: 10 additions & 9 deletions src/deflate.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#define PY_SSIZE_T_CLEAN

// Can't use this until 3.11, when Py_buffer was stabilized
// Also _PyBytes_Resize is not public API (although it seems like it should be)
// #define Py_LIMITED_API 0x030b00f0
#include <Python.h>

Expand Down Expand Up @@ -28,14 +29,14 @@ static PyObject *compress(Py_buffer *data, int compression_level,
libdeflate_alloc_compressor(compression_level);
size_t bound = (*boundfunc)(compressor, data->len);

PyObject *bytes = PyByteArray_FromStringAndSize(NULL, bound);
PyObject *bytes = PyBytes_FromStringAndSize(NULL, bound);
if (bytes == NULL) {
libdeflate_free_compressor(compressor);
return PyErr_NoMemory();
}

size_t compressed_size = (*compressfunc)(compressor, data->buf, data->len,
PyByteArray_AsString(bytes), bound);
PyBytes_AsString(bytes), bound);
libdeflate_free_compressor(compressor);

if (compressed_size == 0) {
Expand All @@ -45,7 +46,7 @@ static PyObject *compress(Py_buffer *data, int compression_level,
}

if (compressed_size != bound) {
PyByteArray_Resize(bytes, compressed_size);
_PyBytes_Resize(&bytes, compressed_size);
}

return bytes;
Expand All @@ -55,19 +56,19 @@ static PyObject *decompress(Py_buffer *data, unsigned int originalsize,
DecompressFunc decompressfunc) {
// Nothing in, nothing out.
if (originalsize == 0) {
return PyByteArray_FromStringAndSize(NULL, 0);
return PyBytes_FromStringAndSize(NULL, 0);
}

PyObject *output = PyByteArray_FromStringAndSize(NULL, originalsize);
PyObject *output = PyBytes_FromStringAndSize(NULL, originalsize);
if (output == NULL) {
return PyErr_NoMemory();
}

size_t decompressed_size;
struct libdeflate_decompressor *decompressor = libdeflate_alloc_decompressor();
enum libdeflate_result result = (*decompressfunc)(
decompressor, data->buf, data->len, PyByteArray_AsString(output), originalsize,
&decompressed_size);
enum libdeflate_result result =
(*decompressfunc)(decompressor, data->buf, data->len, PyBytes_AsString(output),
originalsize, &decompressed_size);
libdeflate_free_decompressor(decompressor);

if (result != LIBDEFLATE_SUCCESS) {
Expand All @@ -77,7 +78,7 @@ static PyObject *decompress(Py_buffer *data, unsigned int originalsize,
}

if (decompressed_size != originalsize) {
PyByteArray_Resize(output, decompressed_size);
_PyBytes_Resize(&output, decompressed_size);
}

return output;
Expand Down

0 comments on commit c5f49c1

Please sign in to comment.