Skip to content

Commit

Permalink
decompressobj: use buffer protocol in decompress() (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
indygreg committed Jun 11, 2017
1 parent 634a167 commit d248077
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
13 changes: 6 additions & 7 deletions c-ext/decompressobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ static void DecompressionObj_dealloc(ZstdDecompressionObj* self) {
}

static PyObject* DecompressionObj_decompress(ZstdDecompressionObj* self, PyObject* args) {
const char* source;
Py_ssize_t sourceSize;
Py_buffer source;
size_t zresult;
ZSTD_inBuffer input;
ZSTD_outBuffer output;
Expand All @@ -39,16 +38,15 @@ static PyObject* DecompressionObj_decompress(ZstdDecompressionObj* self, PyObjec
}

#if PY_MAJOR_VERSION >= 3
if (!PyArg_ParseTuple(args, "y#:decompress",
if (!PyArg_ParseTuple(args, "y*:decompress", &source)) {
#else
if (!PyArg_ParseTuple(args, "s#:decompress",
if (!PyArg_ParseTuple(args, "s*:decompress", &source)) {
#endif
&source, &sourceSize)) {
return NULL;
}

input.src = source;
input.size = sourceSize;
input.src = source.buf;
input.size = source.len;
input.pos = 0;

output.dst = PyMem_Malloc(outSize);
Expand Down Expand Up @@ -107,6 +105,7 @@ static PyObject* DecompressionObj_decompress(ZstdDecompressionObj* self, PyObjec

finally:
PyMem_Free(output.dst);
PyBuffer_Release(&source);

return result;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/test_decompressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,20 @@ def test_simple(self):
dobj = dctx.decompressobj()
self.assertEqual(dobj.decompress(data), b'foobar')

def test_input_types(self):
compressed = zstd.ZstdCompressor(level=1).compress(b'foo')

dctx = zstd.ZstdDecompressor()

sources = [
memoryview(compressed),
bytearray(compressed),
]

for source in sources:
dobj = dctx.decompressobj()
self.assertEqual(dobj.decompress(source), b'foo')

def test_reuse(self):
data = zstd.ZstdCompressor(level=1).compress(b'foobar')

Expand Down

0 comments on commit d248077

Please sign in to comment.