Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/release.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

### Fixes

* `Fletcher32.decode` raises `ValueError` when `out` is not exactly the size of the
decoded data, as `JenkinsLookup3.decode` does.
* `import numcodecs` no longer fails when a zfpy post-release such as 1.0.1.post0 is
installed. zfpy versions are now parsed with `packaging`, which becomes a dependency,
and the `zfpy` extra now requires zfpy >= 1.0.1. {issue}`892`
Expand Down
17 changes: 13 additions & 4 deletions src/numcodecs/fletcher32.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ class Fletcher32(Codec):
return out

def decode(self, buf, out=None):
"""Check fletcher checksum, and return buffer without it"""
"""Check fletcher checksum, and return buffer without it

If `out` is given, it must be a contiguous, writable buffer of exactly the
size of the decoded data; the data is copied into it and `out` is returned.
"""
b = ensure_contiguous_ndarray(buf).view('uint8')
cdef const uint8_t[::1] b_mv = b
cdef const uint8_t* b_ptr = &b_mv[0]
Expand All @@ -106,11 +110,16 @@ class Fletcher32(Codec):
)

cdef uint8_t[::1] out_mv
cdef uint8_t* out_ptr
cdef Py_ssize_t payload_len
if out is not None:
out_mv = ensure_contiguous_ndarray(out).view("uint8")
out_ptr = &out_mv[0]
memcpy(out_ptr, b_ptr, b_len - FOOTER_LENGTH)
payload_len = b_len - FOOTER_LENGTH
if out_mv.size != payload_len:
raise ValueError(
f"out has {out_mv.size} bytes, but the decoded data has {payload_len}"
)
if payload_len:
memcpy(&out_mv[0], b_ptr, payload_len)
else:
out = b[:-FOOTER_LENGTH]
return out
30 changes: 30 additions & 0 deletions tests/test_fletcher32.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import pytest

from numcodecs.compat import ensure_contiguous_ndarray
from numcodecs.fletcher32 import Fletcher32


Expand Down Expand Up @@ -54,3 +55,32 @@ def test_out():
f = Fletcher32()
result = f.encode(data)
f.decode(result, out=data)


@pytest.mark.parametrize(
"make_out",
[
lambda: np.empty(16, dtype="uint8"),
lambda: np.empty((2, 2), dtype="uint32", order="F"),
lambda: np.empty(2, dtype="float64"),
lambda: bytearray(16),
lambda: memoryview(bytearray(16)),
],
ids=["uint8", "uint32-F-order", "float64", "bytearray", "memoryview"],
)
def test_decode_into_out(make_out):
data = np.arange(16, dtype="uint8")
f = Fletcher32()
out = make_out()
assert f.decode(f.encode(data), out=out) is out
assert ensure_contiguous_ndarray(out).view("uint8").tobytes() == data.tobytes()


@pytest.mark.parametrize("shape", [(15,), (17,), ()], ids=["smaller", "larger", "0-d"])
def test_decode_rejects_out_of_wrong_size(shape):
data = np.arange(16, dtype="uint8")
f = Fletcher32()
out = np.full(shape, 0xAA, dtype="uint8")
with pytest.raises(ValueError, match=f"out has {out.size} bytes, but the decoded data has 16"):
f.decode(f.encode(data), out=out)
assert (out == 0xAA).all()
Loading