Skip to content

Commit

Permalink
Merge pull request python#50 from python-lz4/py2bytearray
Browse files Browse the repository at this point in the history
Py2bytearray
  • Loading branch information
jonathanunderwood committed Nov 25, 2017
2 parents 321ab82 + af29145 commit 34d8eaf
Showing 1 changed file with 19 additions and 54 deletions.
73 changes: 19 additions & 54 deletions lz4/block/_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ compress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
int output_size;
Py_buffer source;
int source_size;

#if IS_PY3
int return_bytearray = 0;
static char *argnames[] = {
"source",
"mode",
Expand All @@ -122,27 +121,21 @@ compress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
"return_bytearray",
NULL
};
int return_bytearray = 0;

if (!PyArg_ParseTupleAndKeywords (args, kwargs, "y*|siiip", argnames,

#if IS_PY3
if (!PyArg_ParseTupleAndKeywords (args, kwargs, "y*|spiip", argnames,
&source,
&mode, &store_size, &acceleration, &compression,
&return_bytearray))
{
return NULL;
}
#else
static char *argnames[] = {
"source",
"mode",
"store_size",
"acceleration",
"compression",
NULL
};
if (!PyArg_ParseTupleAndKeywords (args, kwargs, "s*|siii", argnames,
if (!PyArg_ParseTupleAndKeywords (args, kwargs, "s*|siiii", argnames,
&source,
&mode, &store_size, &acceleration, &compression))
&mode, &store_size, &acceleration, &compression,
&return_bytearray))
{
return NULL;
}
Expand Down Expand Up @@ -188,7 +181,6 @@ compress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
total_size = dest_size;
}

#if IS_PY3
if (return_bytearray)
{
py_dest = PyByteArray_FromStringAndSize (NULL, total_size);
Expand All @@ -209,15 +201,6 @@ compress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
}
dest = PyBytes_AS_STRING (py_dest);
}
#else
py_dest = PyBytes_FromStringAndSize (NULL, total_size);
if (py_dest == NULL)
{
PyBuffer_Release(&source);
return PyErr_NoMemory();
}
dest = PyBytes_AS_STRING (py_dest);
#endif

Py_BEGIN_ALLOW_THREADS

Expand Down Expand Up @@ -266,7 +249,6 @@ compress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
/* Resizes are expensive; tolerate some slop to avoid. */
if (output_size < (dest_size / 4) * 3)
{
#if IS_PY3
if (return_bytearray)
{
PyByteArray_Resize (py_dest, output_size);
Expand All @@ -275,9 +257,6 @@ compress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
{
_PyBytes_Resize (&py_dest, output_size);
}
#else
_PyBytes_Resize (&py_dest, output_size);
#endif
}
else
{
Expand All @@ -298,29 +277,25 @@ decompress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
int output_size;
size_t dest_size;
int uncompressed_size = -1;

#if IS_PY3
int return_bytearray = 0;
static char *argnames[] = {
"source",
"uncompressed_size",
"return_bytearray",
NULL
};
int return_bytearray = 0;

#if IS_PY3
if (!PyArg_ParseTupleAndKeywords (args, kwargs, "y*|ip", argnames,
&source, &uncompressed_size,
&return_bytearray))
{
return NULL;
}
#else
static char *argnames[] = {
"source",
"uncompressed_size",
NULL
};
if (!PyArg_ParseTupleAndKeywords (args, kwargs, "s*|i", argnames,
&source, &uncompressed_size))
if (!PyArg_ParseTupleAndKeywords (args, kwargs, "s*|ii", argnames,
&source, &uncompressed_size,
&return_bytearray))
{
return NULL;
}
Expand Down Expand Up @@ -359,7 +334,6 @@ decompress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
return NULL;
}

#if IS_PY3
if (return_bytearray)
{
py_dest = PyByteArray_FromStringAndSize (NULL, dest_size);
Expand All @@ -380,15 +354,6 @@ decompress (PyObject * Py_UNUSED (self), PyObject * args, PyObject * kwargs)
}
dest = PyBytes_AS_STRING (py_dest);
}
#else
py_dest = PyBytes_FromStringAndSize (NULL, dest_size);
if (py_dest == NULL)
{
PyBuffer_Release(&source);
return PyErr_NoMemory();
}
dest = PyBytes_AS_STRING (py_dest);
#endif

Py_BEGIN_ALLOW_THREADS

Expand Down Expand Up @@ -448,9 +413,9 @@ PyDoc_STRVAR(compress__doc,
" store_size (bool): If True (the default) then the size of the\n" \
" uncompressed data is stored at the start of the compressed\n" \
" block.\n" \
" return_bytearray (bool): Python 3 only. If False (the default)\n" \
" then the function will return a bytes object. If True, then\n" \
" the function will return a bytearray object.\n\n" \
" return_bytearray (bool): If False (the default) then the function\n" \
" will return a bytes object. If True, then the function will\n" \
" return a bytearray object.\n\n" \
"Returns:\n" \
" bytes or bytearray: Compressed data.\n");

Expand All @@ -463,9 +428,9 @@ PyDoc_STRVAR(decompress__doc,
" uncompressed_size (int): If not specified or < 0, the uncompressed data\n" \
" size is read from the start of the source block. If specified,\n" \
" it is assumed that the full source data is compressed data.\n" \
" return_bytearray (bool): Python 3 only. If False (the default)\n" \
" then the function will return a bytes object. If True, then\n" \
" the function will return a bytearray object.\n\n" \
" return_bytearray (bool): If False (the default) then the function\n" \
" will return a bytes object. If True, then the function will\n" \
" return a bytearray object.\n\n" \
"Returns:\n" \
" bytes or bytearray: Decompressed data.\n");

Expand Down

0 comments on commit 34d8eaf

Please sign in to comment.