Skip to content

Commit f38b1e9

Browse files
committed
Cleanup crc32c soft dependency
1 parent 29995e3 commit f38b1e9

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

numcodecs/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,17 @@
117117

118118
register_codec(MsgPack)
119119

120-
from numcodecs.checksum32 import CRC32, CRC32C, Adler32, JenkinsLookup3
120+
from numcodecs.checksum32 import CRC32, Adler32, JenkinsLookup3
121121

122122
register_codec(CRC32)
123-
register_codec(CRC32C)
124123
register_codec(Adler32)
125124
register_codec(JenkinsLookup3)
126125

126+
with suppress(ImportError):
127+
from numcodecs.checksum32 import CRC32C
128+
129+
register_codec(CRC32C)
130+
127131
from numcodecs.json import JSON
128132

129133
register_codec(JSON)

numcodecs/checksum32.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import struct
22
import zlib
33
from collections.abc import Callable
4+
from contextlib import suppress
45
from typing import TYPE_CHECKING, Literal
56

67
import numpy as np
@@ -76,26 +77,25 @@ class CRC32(Checksum32):
7677
location = 'start'
7778

7879

79-
class CRC32C(Checksum32):
80-
"""Codec add a crc32c checksum to the buffer.
80+
_crc32c: Optional[ModuleType] = None
81+
with suppress(ImportError):
82+
import crc32c as _crc32c
8183

82-
Parameters
83-
----------
84-
location : 'start' or 'end'
85-
Where to place the checksum in the buffer.
86-
"""
8784

88-
codec_id = 'crc32c'
85+
if _crc32c:
86+
class CRC32C(Checksum32):
87+
"""Codec add a crc32c checksum to the buffer.
8988
90-
def checksum(self, buf):
91-
try:
92-
from crc32c import crc32c as crc32c_
89+
Parameters
90+
----------
91+
location : 'start' or 'end'
92+
Where to place the checksum in the buffer.
93+
"""
9394

94-
return crc32c_(buf)
95-
except ImportError: # pragma: no cover
96-
raise ImportError("crc32c must be installed to use the CRC32C checksum codec.")
95+
codec_id = 'crc32c'
9796

98-
location = 'end'
97+
checksum = _crc32c.crc32c
98+
location = 'end'
9999

100100

101101
class Adler32(Checksum32):

0 commit comments

Comments
 (0)