1+ from contextlib import suppress
12import itertools
23
34import numpy as np
45import pytest
56
6- try :
7- from numcodecs .checksum32 import CRC32 , CRC32C , Adler32
8- except ImportError : # pragma: no cover
9- pytest .skip ("numcodecs.checksum32 not available" , allow_module_level = True )
7+ from numcodecs .checksum32 import CRC32 , Adler32
8+
9+ has_crc32c = False
10+ with suppress (ImportError ):
11+ from numcodecs .checksum32 import CRC32C
12+ has_crc32c = True
1013
1114from numcodecs .tests .common import (
1215 check_backwards_compatibility ,
3942codecs = [
4043 CRC32 (),
4144 CRC32 (location = "end" ),
42- CRC32C (location = "start" ),
43- CRC32C (),
4445 Adler32 (),
4546 Adler32 (location = "end" ),
4647]
48+ if has_crc32c :
49+ codecs .extend ([
50+ CRC32C (location = "start" ),
51+ CRC32C (),
52+ ])
4753
4854
4955@pytest .mark .parametrize (("codec" , "arr" ), itertools .product (codecs , arrays ))
@@ -86,27 +92,30 @@ def test_err_encode_list(codec):
8692
8793
8894def test_err_location ():
89- with pytest .raises (ValueError ):
90- CRC32 (location = "foo" )
91- with pytest .raises (ValueError ):
92- CRC32C (location = "foo" )
9395 with pytest .raises (ValueError ):
9496 Adler32 (location = "foo" )
97+ if has_crc32c :
98+ with pytest .raises (ValueError ):
99+ CRC32 (location = "foo" )
100+ with pytest .raises (ValueError ):
101+ CRC32C (location = "foo" )
95102
96103
97104def test_repr ():
98105 check_repr ("CRC32(location='start')" )
99- check_repr ("CRC32C(location='start')" )
100- check_repr ("Adler32(location='start')" )
101106 check_repr ("CRC32(location='end')" )
102- check_repr ("CRC32C (location='end ')" )
107+ check_repr ("Adler32 (location='start ')" )
103108 check_repr ("Adler32(location='end')" )
109+ if has_crc32c :
110+ check_repr ("CRC32C(location='start')" )
111+ check_repr ("CRC32C(location='end')" )
104112
105113
106114def test_backwards_compatibility ():
107115 check_backwards_compatibility (CRC32 .codec_id , arrays , [CRC32 ()])
108116 check_backwards_compatibility (Adler32 .codec_id , arrays , [Adler32 ()])
109- check_backwards_compatibility (CRC32C .codec_id , arrays , [CRC32C ()])
117+ if has_crc32c :
118+ check_backwards_compatibility (CRC32C .codec_id , arrays , [CRC32C ()])
110119
111120
112121@pytest .mark .parametrize ("codec" , codecs )
@@ -127,6 +136,7 @@ def test_err_out_too_small(codec):
127136 codec .decode (codec .encode (arr ), out )
128137
129138
139+ @pytest .mark .skipif (not has_crc32 , reason = "Needs `crc32c` installed" )
130140def test_crc32c_checksum ():
131141 arr = np .arange (0 , 64 , dtype = "uint8" )
132142 buf = CRC32C (location = "end" ).encode (arr )
0 commit comments