Skip to content

Commit 44c7d93

Browse files
Return bytearray instead from block (de)compress for Python3 (python#16)
Previously we returned a string (bytes) object.
1 parent f5d81de commit 44c7d93

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

lz4/block/_block.c

+37-13
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static PyObject *
9797
compress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
9898
{
9999
const char *mode = "default";
100-
int dest_size;
100+
int dest_size, total_size;
101101
int acceleration = 1, compression = 0;
102102
int store_size = 1;
103103
PyObject *py_dest;
@@ -183,19 +183,29 @@ compress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
183183

184184
if (store_size)
185185
{
186-
py_dest = PyBytes_FromStringAndSize (NULL, dest_size + hdr_size);
186+
total_size = dest_size + hdr_size;
187187
}
188188
else
189189
{
190-
py_dest = PyBytes_FromStringAndSize (NULL, dest_size);
190+
total_size = dest_size + hdr_size;
191191
}
192192

193+
#if IS_PY3
194+
py_dest = PyByteArray_FromStringAndSize (NULL, total_size);
195+
#else
196+
py_dest = PyBytes_FromStringAndSize (NULL, total_size);
197+
#endif
198+
193199
if (py_dest == NULL)
194200
{
195201
return PyErr_NoMemory();
196202
}
197203

204+
#if IS_PY3
205+
dest = PyByteArray_AS_STRING (py_dest);
206+
#else
198207
dest = PyBytes_AS_STRING (py_dest);
208+
#endif
199209

200210
Py_BEGIN_ALLOW_THREADS
201211

@@ -243,7 +253,11 @@ compress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
243253
/* Resizes are expensive; tolerate some slop to avoid. */
244254
if (output_size < (dest_size / 4) * 3)
245255
{
256+
#if IS_PY3
257+
PyByteArray_Resize (py_dest, output_size);
258+
#else
246259
_PyBytes_Resize (&py_dest, output_size);
260+
#endif
247261
}
248262
else
249263
{
@@ -335,13 +349,21 @@ decompress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
335349
return NULL;
336350
}
337351

352+
#if IS_PY3
353+
py_dest = PyByteArray_FromStringAndSize (NULL, dest_size);
354+
#else
338355
py_dest = PyBytes_FromStringAndSize (NULL, dest_size);
356+
#endif
339357
if (py_dest == NULL)
340358
{
341359
return PyErr_NoMemory();
342360
}
343361

362+
#if IS_PY3
363+
dest = PyByteArray_AS_STRING (py_dest);
364+
#else
344365
dest = PyBytes_AS_STRING (py_dest);
366+
#endif
345367

346368
Py_BEGIN_ALLOW_THREADS
347369

@@ -386,23 +408,25 @@ PyDoc_STRVAR(compress__doc,
386408
" argument specifies the compression. Valid values are between\n" \
387409
" 0 and 16. Values between 4-9 are recommended, and 0 is the\n" \
388410
" default.\n\n" \
389-
" store_size (bool): If True (the default) then the size of the" \
390-
" uncompressed data is stored at the start of the compressed" \
391-
" block." \
411+
" store_size (bool): If True (the default) then the size of the\n" \
412+
" uncompressed data is stored at the start of the compressed\n" \
413+
" block.\n\n" \
392414
"Returns:\n" \
393-
" str: Compressed data\n");
415+
" str or bytearray: Compressed data. For Python 2 a str is returned\n" \
416+
" and for Python 3 a bytearray is returned\n");
394417

395418
PyDoc_STRVAR(decompress__doc,
396-
"decompress(source)\n\n" \
419+
"decompress(source, uncompressed_size=-1)\n\n" \
397420
"Decompress source, returning the uncompressed data as a string.\n" \
398421
"Raises an exception if any error occurs.\n\n" \
399422
"Args:\n" \
400423
" source (str, bytes or bytearray): Data to decompress\n\n" \
401-
" uncompressed_size (int): If not specified, the uncompressed data" \
402-
" size is read from the start of the source block. If specified," \
403-
" it is assumed that the full source data is compressed data."
404-
"Returns:\n" \
405-
" str: decompressed data\n");
424+
" uncompressed_size (int): If not specified or < 0, the uncompressed data\n" \
425+
" size is read from the start of the source block. If specified,\n" \
426+
" it is assumed that the full source data is compressed data.\n\n" \
427+
"Returns:\n" \
428+
" str or bytearray: Decompressed data. For Python 2 a str is returned\n" \
429+
" and for Python 3 a bytearray is returned\n");
406430

407431
PyDoc_STRVAR(lz4block__doc,
408432
"A Python wrapper for the LZ4 block protocol"

0 commit comments

Comments
 (0)