Repository navigation
fix: adler32 accepts bytes input instead of str #15721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,20 +11,60 @@ | |
| 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') | ||
| 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 | ||
|
|
||
| Returns: | ||
| Adler-32 checksum as a 32-bit integer | ||
|
|
||
| Raises: | ||
| TypeError: If data is not of type bytes | ||
|
|
||
| >>> adler32(b'Algorithms') | ||
| 363791387 | ||
|
|
||
| >>> adler32('go adler em all') | ||
| 708642122 | ||
| >>> adler32(b'Wikipedia') == 0x11E60398 | ||
| True | ||
|
|
||
| >>> import zlib | ||
|
|
||
| >>> adler32(b'go adler em all') == zlib.adler32(b'go adler em all') | ||
| True | ||
|
|
||
| The previous implementation summed Unicode code points instead of UTF-8 bytes. | ||
|
|
||
| >>> data = '€100'.encode('utf-8') | ||
| >>> adler32(data) == zlib.adler32(data) | ||
| True | ||
|
|
||
| >>> adler32(b'') == zlib.adler32(b'') | ||
| True | ||
|
|
||
| >>> adler32('not bytes') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| 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): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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 | ||
There was a problem hiding this comment.
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 adler32would 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.