Repository navigation
Conversation
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 TheAlgorithms#15635
jgohel9902
left a comment
There was a problem hiding this comment.
Thanks for the fix. The old str version computed wrong checksums for non-ASCII text, and the new bytes version matches zlib.adler32 on every input I tried. A few suggestions inline: accept other bytes-like types, note the breaking change, add literal known-answer doctests and explain the UTF-8 test bytes.
| ... | ||
| TypeError: data must be bytes, not str | ||
| """ | ||
| if not isinstance(data, bytes): |
There was a problem hiding this comment.
This check is stricter than zlib.adler32, which also accepts bytearray and memoryview. I checked that both work with the loop below, but currently raise "data must be bytes, not bytearray". Consider isinstance(data, (bytes, bytearray, memoryview)) (or collections.abc.Buffer on Python 3.12+), and update the TypeError message to match.
|
|
||
|
|
||
| def adler32(plain_text: str) -> int: | ||
| def adler32(data: bytes) -> int: |
There was a problem hiding this comment.
This is a breaking change: adler32("Algorithms") worked before and now raises TypeError. The old str behaviour was wrong for non-ASCII text, which is a good reason to change it. Could you confirm nothing else in the repo calls adler32 with a str (a quick grep -r adler32 would show that), and mention the breaking change in the PR description? A friendlier alternative is to accept str and encode it as UTF-8, but that is a maintainer choice and not required.
| >>> adler32(b'go adler em all') == zlib.adler32(b'go adler em all') | ||
| True | ||
| >>> data = bytes([226, 130, 172, 49, 48, 48]) |
There was a problem hiding this comment.
Nice test: these bytes are the UTF-8 encoding of "€100", which the old str version got wrong. Please add a comment saying so, e.g. # UTF-8 bytes of "€100", because the magic numbers aren't obvious to a reader. It would also help to say in the docstring that this case failed before.
| Raises: | ||
| TypeError: If data is not of type bytes | ||
| >>> import zlib |
There was a problem hiding this comment.
The old doctests showed literal expected values, and the new ones only compare against zlib. Comparing to zlib is a strong test, but a couple of literal known answers also help readers who don't run it. For example, adler32(b"Wikipedia") == 0x11E60398 (the standard example) and adler32(b"Algorithms") == 363791387, which was the old documented value.
| >>> adler32(b'') == zlib.adler32(b'') | ||
| True | ||
| >>> adler32('not bytes') |
There was a problem hiding this comment.
Please add a doctest for a bytearray input once comment 1 is decided, and a doctest for another wrong type such as an int, e.g. adler32(123), to show the type name in the error message.
Fixes #15635
Describe your change
hashes/adler32.py: acceptbytesinput instead ofstr, iterating over raw byte values rather than Unicode code pointszlib.adler32(data), including UTF-8 bytes for non-ASCII text.strmust now pass bytes explicitly, for exampleadler32(text.encode()).TypeErrorfor non-bytes input (consistent withhashes/crc32.py)strandint.Checklist
ruff checkandruff formaton my changesFixes #15635