Skip to content

Commit b3f2d4c

Browse files
Ma Lingpshead
Ma Lin
andauthored
bpo-47040: improve document of checksum functions (gh-31955)
Clarifies a versionchanged note on crc32 & adler32 docs that the workaround is only needed for Python 2 and earlier. Also cleans up an unnecessary intermediate variable in the implementation. Authored-By: Ma Lin / animalize Co-authored-by: Gregory P. Smith <greg@krypto.org>
1 parent 82e9b0b commit b3f2d4c

File tree

4 files changed

+14
-17
lines changed

4 files changed

+14
-17
lines changed

Doc/library/binascii.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ The :mod:`binascii` module defines the following functions:
107107

108108
.. function:: crc32(data[, value])
109109

110-
Compute CRC-32, the 32-bit checksum of *data*, starting with an
110+
Compute CRC-32, the unsigned 32-bit checksum of *data*, starting with an
111111
initial CRC of *value*. The default initial CRC is zero. The algorithm
112112
is consistent with the ZIP file checksum. Since the algorithm is designed for
113113
use as a checksum algorithm, it is not suitable for use as a general hash
@@ -121,9 +121,8 @@ The :mod:`binascii` module defines the following functions:
121121

122122
.. versionchanged:: 3.0
123123
The result is always unsigned.
124-
To generate the same numeric value across all Python versions and
125-
platforms, use ``crc32(data) & 0xffffffff``.
126-
124+
To generate the same numeric value when using Python 2 or earlier,
125+
use ``crc32(data) & 0xffffffff``.
127126

128127
.. function:: b2a_hex(data[, sep[, bytes_per_sep=1]])
129128
hexlify(data[, sep[, bytes_per_sep=1]])

Doc/library/zlib.rst

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ The available exception and functions in this module are:
4242
for use as a general hash algorithm.
4343

4444
.. versionchanged:: 3.0
45-
Always returns an unsigned value.
46-
To generate the same numeric value across all Python versions and
47-
platforms, use ``adler32(data) & 0xffffffff``.
48-
45+
The result is always unsigned.
46+
To generate the same numeric value when using Python 2 or earlier,
47+
use ``adler32(data) & 0xffffffff``.
4948

5049
.. function:: compress(data, /, level=-1, wbits=MAX_WBITS)
5150

@@ -137,10 +136,9 @@ The available exception and functions in this module are:
137136
for use as a general hash algorithm.
138137

139138
.. versionchanged:: 3.0
140-
Always returns an unsigned value.
141-
To generate the same numeric value across all Python versions and
142-
platforms, use ``crc32(data) & 0xffffffff``.
143-
139+
The result is always unsigned.
140+
To generate the same numeric value when using Python 2 or earlier,
141+
use ``crc32(data) & 0xffffffff``.
144142

145143
.. function:: decompress(data, /, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE)
146144

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Clarified the old Python versions compatiblity note of :func:`binascii.crc32` /
2+
:func:`zlib.adler32` / :func:`zlib.crc32` functions.

Modules/zlibmodule.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,8 +1436,6 @@ static PyObject *
14361436
zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
14371437
/*[clinic end generated code: output=63499fa20af7ea25 input=26c3ed430fa00b4c]*/
14381438
{
1439-
int signed_val;
1440-
14411439
/* Releasing the GIL for very small buffers is inefficient
14421440
and may lower performance */
14431441
if (data->len > 1024*5) {
@@ -1452,12 +1450,12 @@ zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
14521450
buf += (size_t) UINT_MAX;
14531451
len -= (size_t) UINT_MAX;
14541452
}
1455-
signed_val = crc32(value, buf, (unsigned int)len);
1453+
value = crc32(value, buf, (unsigned int)len);
14561454
Py_END_ALLOW_THREADS
14571455
} else {
1458-
signed_val = crc32(value, data->buf, (unsigned int)data->len);
1456+
value = crc32(value, data->buf, (unsigned int)data->len);
14591457
}
1460-
return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
1458+
return PyLong_FromUnsignedLong(value & 0xffffffffU);
14611459
}
14621460

14631461

0 commit comments

Comments
 (0)