Skip to content

Commit cf03730

Browse files
authored
chore(crc32c): replace crc32c with google-crc32c dependency (#792)
1 parent 259b432 commit cf03730

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ repos:
3030
hooks:
3131
- id: mypy
3232
args: [--config-file, pyproject.toml]
33-
additional_dependencies: [numpy, pytest, crc32c, zfpy, 'zarr>=3.1.3']
33+
additional_dependencies: [numpy, pytest, google-crc32c, crc32c, zfpy, 'zarr>=3.1.3']

numcodecs/checksum32.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

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

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

@@ -179,5 +179,11 @@ class CRC32C(Checksum32):
179179
"""
180180

181181
codec_id = 'crc32c'
182-
checksum = _crc32c.crc32c # type: ignore[union-attr]
183182
location = 'end'
183+
184+
@staticmethod
185+
def checksum(data: Buffer, value: int = 0) -> int:
186+
if value == 0:
187+
return _crc32c.value(data) # type: ignore[union-attr]
188+
else:
189+
return _crc32c.extend(value, data) # type: ignore[union-attr]

numcodecs/tests/test_checksum32.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,25 @@ def test_crc32c_checksum():
145145
assert np.frombuffer(buf, dtype="<u4", offset=(len(buf) - 4))[0] == np.uint32(4218238699)
146146

147147

148+
@pytest.mark.skipif(not has_crc32c, reason="Needs `crc32c` installed")
149+
def test_crc32c_incremental():
150+
"""Test that CRC32C.checksum supports incremental calculation via value parameter."""
151+
# Test incremental checksum calculation (for API compatibility)
152+
data1 = np.frombuffer(b"hello", dtype='uint8')
153+
data2 = np.frombuffer(b" world", dtype='uint8')
154+
full_data = np.frombuffer(b"hello world", dtype='uint8')
155+
156+
# Calculate checksum in one go
157+
checksum_full = CRC32C.checksum(full_data)
158+
159+
# Calculate incrementally using the value parameter
160+
checksum_part1 = CRC32C.checksum(data1, 0)
161+
checksum_part2 = CRC32C.checksum(data2, checksum_part1)
162+
163+
# Both methods should produce the same result
164+
assert checksum_full == checksum_part2
165+
166+
148167
@pytest.mark.parametrize("codec", codecs)
149168
def test_err_checksum(codec):
150169
arr = np.arange(0, 64, dtype="uint8")

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ test = [
6161
]
6262
test_extras = [
6363
"importlib_metadata",
64+
"crc32c", # TODO: remove once zarr-python does not depend on crc32c anymore
6465
]
6566

6667
[dependency-groups]
@@ -87,7 +88,7 @@ pcodec = [
8788
"pcodec>=0.3,<0.4",
8889
]
8990
crc32c = [
90-
"crc32c>=2.7",
91+
"google-crc32c>=1.5",
9192
]
9293

9394
[tool.setuptools]

0 commit comments

Comments
 (0)