Skip to content
Open
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: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ repos:
hooks:
- id: mypy
args: [--config-file, pyproject.toml]
additional_dependencies: [numpy, pytest, crc32c, zfpy, 'zarr>=3']
additional_dependencies: [numpy, pytest, google-crc32c, zfpy, 'zarr>=3']
10 changes: 8 additions & 2 deletions numcodecs/checksum32.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

_crc32c: ModuleType | None = None
with suppress(ImportError):
import crc32c as _crc32c # type: ignore[no-redef, unused-ignore]
import google_crc32c as _crc32c # type: ignore[no-redef, unused-ignore]

CHECKSUM_LOCATION = Literal['start', 'end']

Expand Down Expand Up @@ -179,5 +179,11 @@ class CRC32C(Checksum32):
"""

codec_id = 'crc32c'
checksum = _crc32c.crc32c # type: ignore[union-attr]
location = 'end'

@staticmethod
def checksum(data: Buffer, value: int = 0) -> int:
if value == 0:
return _crc32c.value(data) # type: ignore[union-attr]
else:
return _crc32c.extend(value, data) # type: ignore[union-attr]
19 changes: 19 additions & 0 deletions numcodecs/tests/test_checksum32.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,25 @@ def test_crc32c_checksum():
assert np.frombuffer(buf, dtype="<u4", offset=(len(buf) - 4))[0] == np.uint32(4218238699)


@pytest.mark.skipif(not has_crc32c, reason="Needs `crc32c` installed")
def test_crc32c_incremental():
"""Test that CRC32C.checksum supports incremental calculation via value parameter."""
# Test incremental checksum calculation (for API compatibility)
data1 = np.frombuffer(b"hello", dtype='uint8')
data2 = np.frombuffer(b" world", dtype='uint8')
full_data = np.frombuffer(b"hello world", dtype='uint8')

# Calculate checksum in one go
checksum_full = CRC32C.checksum(full_data)

# Calculate incrementally using the value parameter
checksum_part1 = CRC32C.checksum(data1, 0)
checksum_part2 = CRC32C.checksum(data2, checksum_part1)

# Both methods should produce the same result
assert checksum_full == checksum_part2


@pytest.mark.parametrize("codec", codecs)
def test_err_checksum(codec):
arr = np.arange(0, 64, dtype="uint8")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pcodec = [
"pcodec>=0.3,<0.4",
]
crc32c = [
"crc32c>=2.7",
"google-crc32c>=1.5",
]

[project.entry-points."zarr.codecs"]
Expand Down
Loading