Skip to content

Commit 5c79ced

Browse files
authored
Merge pull request #59 from icgood/crc
Fix bad exception when crc32c module missing
2 parents 01efad0 + ebdf297 commit 5c79ced

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

proxyprotocol/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#: The package version string.
2-
__version__ = '0.11.1'
2+
__version__ = '0.11.2'

proxyprotocol/tlv.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from typing import ClassVar, Any, Hashable, Optional, Iterator, Sequence, \
99
Mapping, Dict, List
1010

11-
from .checksum import crc32c
11+
from .checksum import crc32c as crc32c_checksum
1212
from .typing import PeerCert
1313

1414
__all__ = ['Type', 'SSLClient', 'ExtType', 'TLV', 'ProxyProtocolTLV',
@@ -202,7 +202,9 @@ def __init__(self, data: bytes = b'',
202202
if ext:
203203
results[Type.PP2_TYPE_NOOP] = bytes(ext)
204204
super().__init__(data, results)
205-
self._auto_crc32c = auto_crc32c and crc32c is None
205+
self._auto_crc32c = auto_crc32c \
206+
and crc32c is None \
207+
and crc32c_checksum is not None
206208

207209
def _pack(self) -> bytes:
208210
if self._auto_crc32c:
@@ -215,17 +217,17 @@ def _zero_crc32c(self) -> ProxyProtocolTLV:
215217

216218
@property
217219
def size(self) -> int:
218-
if self.crc32c is None and self._auto_crc32c and crc32c is not None:
220+
if self.crc32c is None and self._auto_crc32c:
219221
return self._zero_crc32c.size
220222
else:
221223
return super().size
222224

223225
def _compute_checksum(self, before: Sequence[bytes]) -> int:
224-
assert crc32c is not None
225-
crc = crc32c(b'')
226+
assert crc32c_checksum is not None
227+
crc = crc32c_checksum(b'')
226228
for data in before:
227-
crc = crc32c(data, crc)
228-
return crc32c(bytes(self._zero_crc32c), crc)
229+
crc = crc32c_checksum(data, crc)
230+
return crc32c_checksum(bytes(self._zero_crc32c), crc)
229231

230232
def with_checksum(self, *before: bytes) -> ProxyProtocolTLV:
231233
"""Return a copy of the current TLV values with the :attr:`.crc32c`
@@ -236,7 +238,7 @@ def with_checksum(self, *before: bytes) -> ProxyProtocolTLV:
236238
is included in the checksum.
237239
238240
"""
239-
if not self._auto_crc32c or crc32c is None:
241+
if not self._auto_crc32c:
240242
return self
241243
crc = self._compute_checksum(before)
242244
return ProxyProtocolTLV(init=self, crc32c=crc)
@@ -251,7 +253,7 @@ def verify_checksum(self, *before: bytes) -> bool:
251253
is included in the checksum.
252254
253255
"""
254-
if self.crc32c is None or crc32c is None:
256+
if self.crc32c is None or crc32c_checksum is None:
255257
return True
256258
crc = self._compute_checksum(before)
257259
return self.crc32c == crc

0 commit comments

Comments
 (0)