1
+ import abc
1
2
import struct
2
3
import zlib
3
- from collections .abc import Callable
4
4
from contextlib import suppress
5
5
from types import ModuleType
6
- from typing import TYPE_CHECKING , Literal , Optional
6
+ from typing import Literal , Optional
7
7
8
8
import numpy as np
9
+ from typing_extensions import Buffer
9
10
10
11
from .abc import Codec
11
12
from .compat import ensure_contiguous_ndarray , ndarray_copy
15
16
with suppress (ImportError ):
16
17
import crc32c as _crc32c # type: ignore[no-redef, unused-ignore]
17
18
18
- if TYPE_CHECKING : # pragma: no cover
19
- from typing_extensions import Buffer
20
-
21
19
CHECKSUM_LOCATION = Literal ['start' , 'end' ]
22
20
23
21
24
- class Checksum32 (Codec ):
22
+ class Checksum32 (Codec , abc . ABC ):
25
23
# override in sub-class
26
- checksum : Callable [["Buffer" , int ], int ] | None = None
27
24
location : CHECKSUM_LOCATION = 'start'
28
25
29
26
def __init__ (self , location : CHECKSUM_LOCATION | None = None ):
@@ -67,6 +64,10 @@ def decode(self, buf, out=None):
67
64
)
68
65
return ndarray_copy (payload_view , out )
69
66
67
+ @staticmethod
68
+ @abc .abstractmethod
69
+ def checksum (data : Buffer , value : int ) -> int : ...
70
+
70
71
71
72
class CRC32 (Checksum32 ):
72
73
"""Codec add a crc32 checksum to the buffer.
@@ -78,9 +79,15 @@ class CRC32(Checksum32):
78
79
"""
79
80
80
81
codec_id = 'crc32'
81
- checksum = zlib .crc32
82
82
location = 'start'
83
83
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
+
84
91
85
92
class Adler32 (Checksum32 ):
86
93
"""Codec add a adler32 checksum to the buffer.
@@ -92,9 +99,15 @@ class Adler32(Checksum32):
92
99
"""
93
100
94
101
codec_id = 'adler32'
95
- checksum = zlib .adler32
96
102
location = 'start'
97
103
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
+
98
111
99
112
class JenkinsLookup3 (Checksum32 ):
100
113
"""Bob Jenkin's lookup3 checksum with 32-bit output
0 commit comments