@@ -97,7 +97,7 @@ static PyObject *
97
97
compress (PyObject * Py_UNUSED (self ), PyObject * args , PyObject * kwargs )
98
98
{
99
99
const char * mode = "default" ;
100
- int dest_size ;
100
+ int dest_size , total_size ;
101
101
int acceleration = 1 , compression = 0 ;
102
102
int store_size = 1 ;
103
103
PyObject * py_dest ;
@@ -183,19 +183,29 @@ compress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
183
183
184
184
if (store_size )
185
185
{
186
- py_dest = PyBytes_FromStringAndSize ( NULL , dest_size + hdr_size ) ;
186
+ total_size = dest_size + hdr_size ;
187
187
}
188
188
else
189
189
{
190
- py_dest = PyBytes_FromStringAndSize ( NULL , dest_size ) ;
190
+ total_size = dest_size + hdr_size ;
191
191
}
192
192
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
+
193
199
if (py_dest == NULL )
194
200
{
195
201
return PyErr_NoMemory ();
196
202
}
197
203
204
+ #if IS_PY3
205
+ dest = PyByteArray_AS_STRING (py_dest );
206
+ #else
198
207
dest = PyBytes_AS_STRING (py_dest );
208
+ #endif
199
209
200
210
Py_BEGIN_ALLOW_THREADS
201
211
@@ -243,7 +253,11 @@ compress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
243
253
/* Resizes are expensive; tolerate some slop to avoid. */
244
254
if (output_size < (dest_size / 4 ) * 3 )
245
255
{
256
+ #if IS_PY3
257
+ PyByteArray_Resize (py_dest , output_size );
258
+ #else
246
259
_PyBytes_Resize (& py_dest , output_size );
260
+ #endif
247
261
}
248
262
else
249
263
{
@@ -335,13 +349,21 @@ decompress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
335
349
return NULL ;
336
350
}
337
351
352
+ #if IS_PY3
353
+ py_dest = PyByteArray_FromStringAndSize (NULL , dest_size );
354
+ #else
338
355
py_dest = PyBytes_FromStringAndSize (NULL , dest_size );
356
+ #endif
339
357
if (py_dest == NULL )
340
358
{
341
359
return PyErr_NoMemory ();
342
360
}
343
361
362
+ #if IS_PY3
363
+ dest = PyByteArray_AS_STRING (py_dest );
364
+ #else
344
365
dest = PyBytes_AS_STRING (py_dest );
366
+ #endif
345
367
346
368
Py_BEGIN_ALLOW_THREADS
347
369
@@ -386,23 +408,25 @@ PyDoc_STRVAR(compress__doc,
386
408
" argument specifies the compression. Valid values are between\n" \
387
409
" 0 and 16. Values between 4-9 are recommended, and 0 is the\n" \
388
410
" 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" \
392
414
"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" );
394
417
395
418
PyDoc_STRVAR (decompress__doc ,
396
- "decompress(source)\n\n" \
419
+ "decompress(source, uncompressed_size=-1 )\n\n" \
397
420
"Decompress source, returning the uncompressed data as a string.\n" \
398
421
"Raises an exception if any error occurs.\n\n" \
399
422
"Args:\n" \
400
423
" 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" );
406
430
407
431
PyDoc_STRVAR (lz4block__doc ,
408
432
"A Python wrapper for the LZ4 block protocol"
0 commit comments