Skip to content

fix: adler32 accepts bytes input instead of str - #15721

Open
dbdefault wants to merge 2 commits into
TheAlgorithms:masterfrom
dbdefault:fix/15635/adler32-bytes
Open

dbdefault wants to merge 2 commits into
TheAlgorithms:masterfrom
dbdefault:fix/15635/adler32-bytes

Conversation

@dbdefault

@dbdefault dbdefault commented Nov 12, 2026 •

Copy link
Copy Markdown

Fixes #15635

Describe your change

  • hashes/adler32.py: accept bytes input instead of str, iterating over raw byte values rather than Unicode code points
  • For byte input, the checksum matches zlib.adler32(data), including UTF-8 bytes for non-ASCII text.
  • Compatibility: callers that previously passed str must now pass bytes explicitly, for example adler32(text.encode()).
  • Add TypeError for non-bytes input (consistent with hashes/crc32.py)
  • Add doctests for known checksums, UTF-8 and empty byte inputs, and rejection of str and int.

Checklist

  • I have read the contributing guidelines
  • I have run ruff check and ruff format on my changes
  • I have added doctests for any new or changed functions
  • My change is one algorithm file

Fixes #15635

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 jgohel9902 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread hashes/adler32.py
...
TypeError: data must be bytes, not str
"""
if not isinstance(data, bytes):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread hashes/adler32.py


def adler32(plain_text: str) -> int:
def adler32(data: bytes) -> int:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread hashes/adler32.py Outdated
>>> adler32(b'go adler em all') == zlib.adler32(b'go adler em all')
True
>>> data = bytes([226, 130, 172, 49, 48, 48])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread hashes/adler32.py Outdated
Raises:
TypeError: If data is not of type bytes
>>> import zlib

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread hashes/adler32.py
>>> adler32(b'') == zlib.adler32(b'')
True
>>> adler32('not bytes')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hashes/adler32.py: checksum diverges from the Adler-32 spec for any non-ASCII input (sums Unicode code points instead of bytes)

2 participants