From 7fef3f050ee840f824018ccb41a2b65f1657d891 Mon Sep 17 00:00:00 2001 From: dbdefault Date: Thu, 12 Nov 2026 15:52:09 +0800 Subject: [PATCH 1/2] fix: adler32 accepts bytes input instead of str Accept bytes input instead of str, iterating over raw byte values rather than Unicode code points. This makes output match zlib.adler32 for all inputs including non-ASCII text. Add TypeError for non-bytes input (consistent with hashes/crc32.py). Add doctests comparing against zlib.adler32 for ASCII, non-ASCII, and empty inputs. Fixes #15635 --- hashes/adler32.py | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/hashes/adler32.py b/hashes/adler32.py index 38d76ab12aa0..6c67407cea28 100644 --- a/hashes/adler32.py +++ b/hashes/adler32.py @@ -11,20 +11,46 @@ MOD_ADLER = 65521 -def adler32(plain_text: str) -> int: +def adler32(data: bytes) -> int: """ Function implements adler-32 hash. - Iterates and evaluates a new value for each character + Iterates and evaluates a new value for each byte of the input. - >>> adler32('Algorithms') - 363791387 + Args: + data: Byte data to calculate the hash for - >>> adler32('go adler em all') - 708642122 + Returns: + Adler-32 checksum as a 32-bit integer + + Raises: + TypeError: If data is not of type bytes + + >>> import zlib + >>> adler32(b'Algorithms') == zlib.adler32(b'Algorithms') + True + + >>> adler32(b'go adler em all') == zlib.adler32(b'go adler em all') + True + + >>> data = bytes([226, 130, 172, 49, 48, 48]) + >>> adler32(data) == zlib.adler32(data) + True + + >>> adler32(b'') == zlib.adler32(b'') + True + + >>> adler32('not bytes') + Traceback (most recent call last): + ... + TypeError: data must be bytes, not str """ + if not isinstance(data, bytes): + msg = f"data must be bytes, not {type(data).__name__}" + raise TypeError(msg) + a = 1 b = 0 - for plain_chr in plain_text: - a = (a + ord(plain_chr)) % MOD_ADLER + for byte in data: + a = (a + byte) % MOD_ADLER b = (b + a) % MOD_ADLER return (b << 16) | a From 4b77e7693b7d66c0eb20756a96bd4a44a0330ee1 Mon Sep 17 00:00:00 2001 From: dbdefault <246079841+dbdefault@users.noreply.github.com> Date: Thu, 19 Nov 2026 07:51:43 +0800 Subject: [PATCH 2/2] Document Adler-32 bytes compatibility and expand doctests --- hashes/adler32.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/hashes/adler32.py b/hashes/adler32.py index 6c67407cea28..b3dd18f9b371 100644 --- a/hashes/adler32.py +++ b/hashes/adler32.py @@ -16,6 +16,9 @@ def adler32(data: bytes) -> int: Function implements adler-32 hash. Iterates and evaluates a new value for each byte of the input. + Unlike the former str API, this function requires bytes. Encode text explicitly + (for example, with text.encode("utf-8")) before calling it. + Args: data: Byte data to calculate the hash for @@ -25,14 +28,20 @@ def adler32(data: bytes) -> int: Raises: TypeError: If data is not of type bytes - >>> import zlib - >>> adler32(b'Algorithms') == zlib.adler32(b'Algorithms') + >>> adler32(b'Algorithms') + 363791387 + + >>> adler32(b'Wikipedia') == 0x11E60398 True + >>> import zlib + >>> adler32(b'go adler em all') == zlib.adler32(b'go adler em all') True - >>> data = bytes([226, 130, 172, 49, 48, 48]) + The previous implementation summed Unicode code points instead of UTF-8 bytes. + + >>> data = '€100'.encode('utf-8') >>> adler32(data) == zlib.adler32(data) True @@ -43,6 +52,11 @@ def adler32(data: bytes) -> int: Traceback (most recent call last): ... TypeError: data must be bytes, not str + + >>> adler32(123) + Traceback (most recent call last): + ... + TypeError: data must be bytes, not int """ if not isinstance(data, bytes): msg = f"data must be bytes, not {type(data).__name__}"