@@ -96,9 +96,8 @@ typedef enum
9696static PyObject *
9797compress (PyObject * Py_UNUSED (self ), PyObject * args , PyObject * kwargs )
9898{
99- const char * source ;
10099 const char * mode = "default" ;
101- int source_size , dest_size ;
100+ int dest_size ;
102101 int acceleration = 1 , compression = 0 ;
103102 int store_size = 1 ;
104103 PyObject * py_dest ;
@@ -114,12 +113,51 @@ compress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
114113 NULL
115114 };
116115
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 ;
117154 if (!PyArg_ParseTupleAndKeywords (args , kwargs , "s#|siii" , argnames ,
118155 & source , & source_size ,
119156 & mode , & store_size , & acceleration , & compression ))
120157 {
121158 return NULL ;
122159 }
160+ #endif
123161
124162 if (!strncmp (mode , "default" , sizeof ("default" )))
125163 {
@@ -190,7 +228,7 @@ compress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
190228 if (output_size <= 0 )
191229 {
192230 Py_BLOCK_THREADS
193- PyErr_SetString (PyExc_ValueError , "Compression failed ");
231+ PyErr_SetString (PyExc_ValueError , "Compression failed ");
194232 Py_CLEAR (py_dest );
195233 return NULL ;
196234 }
@@ -218,10 +256,10 @@ compress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
218256static PyObject *
219257decompress (PyObject * Py_UNUSED (self ), PyObject * args , PyObject * kwargs )
220258{
221- const char * source , * source_start ;
259+ const char * source_start ;
222260 PyObject * py_dest ;
223261 char * dest ;
224- int source_size , output_size ;
262+ int output_size ;
225263 size_t dest_size ;
226264 int uncompressed_size = -1 ;
227265 static char * argnames [] = {
@@ -230,12 +268,49 @@ decompress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
230268 NULL
231269 };
232270
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 ;
233308 if (!PyArg_ParseTupleAndKeywords (args , kwargs , "s#|i" , argnames ,
234309 & source , & source_size , & uncompressed_size ))
235310 {
236311 return NULL ;
237312 }
238-
313+ #endif
239314 if (uncompressed_size > 0 )
240315 {
241316 dest_size = uncompressed_size ;
@@ -297,7 +372,7 @@ PyDoc_STRVAR(compress__doc,
297372 "Compress source, returning the compressed data as a string.\n" \
298373 "Raises an exception if any error occurs.\n\n" \
299374 "Args:\n" \
300- " source (str): Data to compress\n" \
375+ " source (str, bytes or bytearray ): Data to compress\n" \
301376 " mode (str): If 'default' or unspecified use the default LZ4\n" \
302377 " compression mode. Set to 'fast' to use the fast compression\n" \
303378 " LZ4 mode at the expense of compression. Set to\n" \
@@ -322,7 +397,7 @@ PyDoc_STRVAR(decompress__doc,
322397 "Decompress source, returning the uncompressed data as a string.\n" \
323398 "Raises an exception if any error occurs.\n\n" \
324399 "Args:\n" \
325- " source (str): Data to decompress\n\n" \
400+ " source (str, bytes or bytearray ): Data to decompress\n\n" \
326401 " uncompressed_size (int): If not specified, the uncompressed data" \
327402 " size is read from the start of the source block. If specified," \
328403 " it is assumed that the full source data is compressed data."
0 commit comments