Skip to content

Commit

Permalink
decompressobj: return bytes from flush()
Browse files Browse the repository at this point in the history
This makes behavior more inline with similar types in Python's
standard library.

Closes #78.
  • Loading branch information
indygreg committed Dec 27, 2020
1 parent 242d03f commit d7a8c43
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
3 changes: 3 additions & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ Backwards Compatibility Notes
and ``ZstdDecompressor.copy_stream()`` now raise the original exception
on error calling the source stream's ``read()`` instead of raising
``ZstdError``. This only affects the C backend.
* ``ZstdDecompressionObj.flush()`` now returns ``bytes`` instead of
``None``. This makes it behave more similarly to ``flush()`` methods
for similar types in the Python standard library. (#78)

Bug Fixes
---------
Expand Down
2 changes: 1 addition & 1 deletion c-ext/decompressobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ static PyObject *DecompressionObj_flush(ZstdDecompressionObj *self,
return NULL;
}

Py_RETURN_NONE;
return PyBytes_FromString("");
}

static PyMethodDef DecompressionObj_methods[] = {
Expand Down
16 changes: 8 additions & 8 deletions tests/test_decompressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,9 +877,9 @@ def test_simple(self):
dctx = zstd.ZstdDecompressor()
dobj = dctx.decompressobj()
self.assertEqual(dobj.decompress(data), b"foobar")
self.assertIsNone(dobj.flush())
self.assertIsNone(dobj.flush(10))
self.assertIsNone(dobj.flush(length=100))
self.assertEqual(dobj.flush(), b"")
self.assertEqual(dobj.flush(10), b"")
self.assertEqual(dobj.flush(length=100), b"")

def test_input_types(self):
compressed = zstd.ZstdCompressor(level=1).compress(b"foo")
Expand All @@ -897,11 +897,11 @@ def test_input_types(self):

for source in sources:
dobj = dctx.decompressobj()
self.assertIsNone(dobj.flush())
self.assertIsNone(dobj.flush(10))
self.assertIsNone(dobj.flush(length=100))
self.assertEqual(dobj.flush(), b"")
self.assertEqual(dobj.flush(10), b"")
self.assertEqual(dobj.flush(length=100), b"")
self.assertEqual(dobj.decompress(source), b"foo")
self.assertIsNone(dobj.flush())
self.assertEqual(dobj.flush(), b"")

def test_reuse(self):
data = zstd.ZstdCompressor(level=1).compress(b"foobar")
Expand All @@ -914,7 +914,7 @@ def test_reuse(self):
zstd.ZstdError, "cannot use a decompressobj"
):
dobj.decompress(data)
self.assertIsNone(dobj.flush())
self.assertEqual(dobj.flush(), b"")

def test_bad_write_size(self):
dctx = zstd.ZstdDecompressor()
Expand Down
2 changes: 1 addition & 1 deletion zstandard/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ class ZstdCompressor(object):

class ZstdDecompressionObj(object):
def decompress(self, data: ByteString) -> bytes: ...
def flush(self, length: int = 0): ...
def flush(self, length: int = 0) -> bytes: ...

class ZstdDecompressionReader(BinaryIO):
def __enter__(self) -> "ZstdDecompressionReader": ...
Expand Down
2 changes: 1 addition & 1 deletion zstandard/backend_cffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1901,7 +1901,7 @@ def decompress(self, data):
return b"".join(chunks)

def flush(self, length=0):
pass
return b""


class ZstdDecompressionReader(object):
Expand Down

0 comments on commit d7a8c43

Please sign in to comment.