Skip to content

Commit 4b77e76

Browse files
committed
Document Adler-32 bytes compatibility and expand doctests
1 parent 7fef3f0 commit 4b77e76

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

‎hashes/adler32.py‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ def adler32(data: bytes) -> int:
1616
Function implements adler-32 hash.
1717
Iterates and evaluates a new value for each byte of the input.
1818
19+
Unlike the former str API, this function requires bytes. Encode text explicitly
20+
(for example, with text.encode("utf-8")) before calling it.
21+
1922
Args:
2023
data: Byte data to calculate the hash for
2124
@@ -25,14 +28,20 @@ def adler32(data: bytes) -> int:
2528
Raises:
2629
TypeError: If data is not of type bytes
2730
28-
>>> import zlib
29-
>>> adler32(b'Algorithms') == zlib.adler32(b'Algorithms')
31+
>>> adler32(b'Algorithms')
32+
363791387
33+
34+
>>> adler32(b'Wikipedia') == 0x11E60398
3035
True
3136
37+
>>> import zlib
38+
3239
>>> adler32(b'go adler em all') == zlib.adler32(b'go adler em all')
3340
True
3441
35-
>>> data = bytes([226, 130, 172, 49, 48, 48])
42+
The previous implementation summed Unicode code points instead of UTF-8 bytes.
43+
44+
>>> data = '€100'.encode('utf-8')
3645
>>> adler32(data) == zlib.adler32(data)
3746
True
3847
@@ -43,6 +52,11 @@ def adler32(data: bytes) -> int:
4352
Traceback (most recent call last):
4453
...
4554
TypeError: data must be bytes, not str
55+
56+
>>> adler32(123)
57+
Traceback (most recent call last):
58+
...
59+
TypeError: data must be bytes, not int
4660
"""
4761
if not isinstance(data, bytes):
4862
msg = f"data must be bytes, not {type(data).__name__}"

0 commit comments

Comments
 (0)