1+ import abc
12import struct
23import zlib
3- from collections .abc import Callable
44from contextlib import suppress
55from types import ModuleType
6- from typing import TYPE_CHECKING , Literal , Optional
6+ from typing import Literal , Optional
77
88import numpy as np
9+ from typing_extensions import Buffer
910
1011from .abc import Codec
1112from .compat import ensure_contiguous_ndarray , ndarray_copy
1516with suppress (ImportError ):
1617 import crc32c as _crc32c # type: ignore[no-redef, unused-ignore]
1718
18- if TYPE_CHECKING : # pragma: no cover
19- from typing_extensions import Buffer
20-
2119CHECKSUM_LOCATION = Literal ['start' , 'end' ]
2220
2321
24- class Checksum32 (Codec ):
22+ class Checksum32 (Codec , abc . ABC ):
2523 # override in sub-class
26- checksum : Callable [["Buffer" , int ], int ] | None = None
2724 location : CHECKSUM_LOCATION = 'start'
2825
2926 def __init__ (self , location : CHECKSUM_LOCATION | None = None ):
@@ -67,6 +64,10 @@ def decode(self, buf, out=None):
6764 )
6865 return ndarray_copy (payload_view , out )
6966
67+ @staticmethod
68+ @abc .abstractmethod
69+ def checksum (data : Buffer , value : int ) -> int : ...
70+
7071
7172class CRC32 (Checksum32 ):
7273 """Codec add a crc32 checksum to the buffer.
@@ -78,9 +79,15 @@ class CRC32(Checksum32):
7879 """
7980
8081 codec_id = 'crc32'
81- checksum = zlib .crc32
8282 location = 'start'
8383
84+ @staticmethod
85+ def checksum (data : Buffer , value : int = 0 ) -> int :
86+ """
87+ Thin wrapper around ``zlib.crc32``.
88+ """
89+ return zlib .crc32 (data , value )
90+
8491
8592class Adler32 (Checksum32 ):
8693 """Codec add a adler32 checksum to the buffer.
@@ -92,9 +99,15 @@ class Adler32(Checksum32):
9299 """
93100
94101 codec_id = 'adler32'
95- checksum = zlib .adler32
96102 location = 'start'
97103
104+ @staticmethod
105+ def checksum (data : Buffer , value : int = 1 ) -> int :
106+ """
107+ Thin wrapper around ``zlib.adler32``.
108+ """
109+ return zlib .adler32 (data , value )
110+
98111
99112class JenkinsLookup3 (Checksum32 ):
100113 """Bob Jenkin's lookup3 checksum with 32-bit output
0 commit comments