@@ -96,9 +96,8 @@ typedef enum
96
96
static PyObject *
97
97
compress (PyObject * Py_UNUSED (self ), PyObject * args , PyObject * kwargs )
98
98
{
99
- const char * source ;
100
99
const char * mode = "default" ;
101
- int source_size , dest_size ;
100
+ int dest_size ;
102
101
int acceleration = 1 , compression = 0 ;
103
102
int store_size = 1 ;
104
103
PyObject * py_dest ;
@@ -114,12 +113,51 @@ compress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
114
113
NULL
115
114
};
116
115
116
+ #if IS_PY3
117
+ PyObject * py_source ;
118
+ Py_ssize_t source_size ;
119
+ char * source ;
120
+ if (!PyArg_ParseTupleAndKeywords (args , kwargs , "O|siii" , argnames ,
121
+ & py_source ,
122
+ & mode , & store_size , & acceleration , & compression ))
123
+ {
124
+ return NULL ;
125
+ }
126
+ if (PyBytes_Check (py_source ))
127
+ {
128
+ source = PyBytes_AsString (py_source );
129
+ if (source == NULL )
130
+ {
131
+ PyErr_SetString (PyExc_ValueError , "Failed to access source bytes object" );
132
+ return NULL ;
133
+ }
134
+ source_size = PyBytes_Size (py_source );
135
+ }
136
+ else if (PyByteArray_Check (py_source ))
137
+ {
138
+ source = PyByteArray_AsString (py_source );
139
+ if (source == NULL )
140
+ {
141
+ PyErr_SetString (PyExc_ValueError , "Failed to access source bytearray object" );
142
+ return NULL ;
143
+ }
144
+ source_size = PyByteArray_Size (py_source );
145
+ }
146
+ else
147
+ {
148
+ PyErr_SetString (PyExc_ValueError , "Incorrect type for source object" );
149
+ return NULL ;
150
+ }
151
+ #else
152
+ const char * source ;
153
+ int source_size ;
117
154
if (!PyArg_ParseTupleAndKeywords (args , kwargs , "s#|siii" , argnames ,
118
155
& source , & source_size ,
119
156
& mode , & store_size , & acceleration , & compression ))
120
157
{
121
158
return NULL ;
122
159
}
160
+ #endif
123
161
124
162
if (!strncmp (mode , "default" , sizeof ("default" )))
125
163
{
@@ -190,7 +228,7 @@ compress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
190
228
if (output_size <= 0 )
191
229
{
192
230
Py_BLOCK_THREADS
193
- PyErr_SetString (PyExc_ValueError , "Compression failed ");
231
+ PyErr_SetString (PyExc_ValueError , "Compression failed ");
194
232
Py_CLEAR (py_dest );
195
233
return NULL ;
196
234
}
@@ -218,10 +256,10 @@ compress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
218
256
static PyObject *
219
257
decompress (PyObject * Py_UNUSED (self ), PyObject * args , PyObject * kwargs )
220
258
{
221
- const char * source , * source_start ;
259
+ const char * source_start ;
222
260
PyObject * py_dest ;
223
261
char * dest ;
224
- int source_size , output_size ;
262
+ int output_size ;
225
263
size_t dest_size ;
226
264
int uncompressed_size = -1 ;
227
265
static char * argnames [] = {
@@ -230,12 +268,49 @@ decompress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
230
268
NULL
231
269
};
232
270
271
+ #if IS_PY3
272
+ PyObject * py_source ;
273
+ Py_ssize_t source_size ;
274
+ char * source ;
275
+ if (!PyArg_ParseTupleAndKeywords (args , kwargs , "O|i" , argnames ,
276
+ & py_source , & uncompressed_size ))
277
+ {
278
+ return NULL ;
279
+ }
280
+ if (PyBytes_Check (py_source ))
281
+ {
282
+ source = PyBytes_AsString (py_source );
283
+ if (source == NULL )
284
+ {
285
+ PyErr_SetString (PyExc_ValueError , "Failed to access source bytes object" );
286
+ return NULL ;
287
+ }
288
+ source_size = PyBytes_Size (py_source );
289
+ }
290
+ else if (PyByteArray_Check (py_source ))
291
+ {
292
+ source = PyByteArray_AsString (py_source );
293
+ if (source == NULL )
294
+ {
295
+ PyErr_SetString (PyExc_ValueError , "Failed to access source bytearray object" );
296
+ return NULL ;
297
+ }
298
+ source_size = PyByteArray_Size (py_source );
299
+ }
300
+ else
301
+ {
302
+ PyErr_SetString (PyExc_ValueError , "Incorrect type for source object" );
303
+ return NULL ;
304
+ }
305
+ #else
306
+ const char * source ;
307
+ int source_size = 0 ;
233
308
if (!PyArg_ParseTupleAndKeywords (args , kwargs , "s#|i" , argnames ,
234
309
& source , & source_size , & uncompressed_size ))
235
310
{
236
311
return NULL ;
237
312
}
238
-
313
+ #endif
239
314
if (uncompressed_size > 0 )
240
315
{
241
316
dest_size = uncompressed_size ;
@@ -297,7 +372,7 @@ PyDoc_STRVAR(compress__doc,
297
372
"Compress source, returning the compressed data as a string.\n" \
298
373
"Raises an exception if any error occurs.\n\n" \
299
374
"Args:\n" \
300
- " source (str): Data to compress\n" \
375
+ " source (str, bytes or bytearray ): Data to compress\n" \
301
376
" mode (str): If 'default' or unspecified use the default LZ4\n" \
302
377
" compression mode. Set to 'fast' to use the fast compression\n" \
303
378
" LZ4 mode at the expense of compression. Set to\n" \
@@ -322,7 +397,7 @@ PyDoc_STRVAR(decompress__doc,
322
397
"Decompress source, returning the uncompressed data as a string.\n" \
323
398
"Raises an exception if any error occurs.\n\n" \
324
399
"Args:\n" \
325
- " source (str): Data to decompress\n\n" \
400
+ " source (str, bytes or bytearray ): Data to decompress\n\n" \
326
401
" uncompressed_size (int): If not specified, the uncompressed data" \
327
402
" size is read from the start of the source block. If specified," \
328
403
" it is assumed that the full source data is compressed data."
0 commit comments