Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b82032f

Browse files
committed
Issue #22341: Drop Python 2 workaround and document CRC initial value
Also align the parameter naming in binascii to be consistent with zlib.
1 parent 7dda421 commit b82032f

6 files changed

Lines changed: 39 additions & 58 deletions

File tree

Doc/library/binascii.rst

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,31 +110,30 @@ The :mod:`binascii` module defines the following functions:
110110
possibly the last fragment).
111111

112112

113-
.. function:: crc_hqx(data, crc)
113+
.. function:: crc_hqx(data, value)
114114

115-
Compute the binhex4 crc value of *data*, starting with an initial *crc* and
116-
returning the result.
115+
Compute the binhex4 crc value of *data*, starting with *value* as the
116+
initial crc, and return the result.
117117

118118

119-
.. function:: crc32(data[, crc])
119+
.. function:: crc32(data[, value])
120120

121-
Compute CRC-32, the 32-bit checksum of data, starting with an initial crc. This
121+
Compute CRC-32, the 32-bit checksum of *data*, starting with an
122+
initial CRC of *value*. The default initial CRC is zero. The algorithm
122123
is consistent with the ZIP file checksum. Since the algorithm is designed for
123124
use as a checksum algorithm, it is not suitable for use as a general hash
124125
algorithm. Use as follows::
125126

126127
print(binascii.crc32(b"hello world"))
127128
# Or, in two pieces:
128129
crc = binascii.crc32(b"hello")
129-
crc = binascii.crc32(b" world", crc) & 0xffffffff
130+
crc = binascii.crc32(b" world", crc)
130131
print('crc32 = {:#010x}'.format(crc))
131132

132-
.. note::
133-
To generate the same numeric value across all Python versions and
134-
platforms use crc32(data) & 0xffffffff. If you are only using
135-
the checksum in packed binary format this is not necessary as the
136-
return value is the correct 32bit binary representation
137-
regardless of sign.
133+
.. versionchanged:: 3.0
134+
The result is always unsigned.
135+
To generate the same numeric value across all Python versions and
136+
platforms, use ``crc32(data) & 0xffffffff``.
138137

139138

140139
.. function:: b2a_hex(data)

Doc/library/zlib.rst

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,19 @@ The available exception and functions in this module are:
3131
.. function:: adler32(data[, value])
3232

3333
Computes an Adler-32 checksum of *data*. (An Adler-32 checksum is almost as
34-
reliable as a CRC32 but can be computed much more quickly.) If *value* is
35-
present, it is used as the starting value of the checksum; otherwise, a fixed
36-
default value is used. This allows computing a running checksum over the
34+
reliable as a CRC32 but can be computed much more quickly.) The result
35+
is an unsigned 32-bit integer. If *value* is present, it is used as
36+
the starting value of the checksum; otherwise, a default value of 1
37+
is used. Passing in *value* allows computing a running checksum over the
3738
concatenation of several inputs. The algorithm is not cryptographically
3839
strong, and should not be used for authentication or digital signatures. Since
3940
the algorithm is designed for use as a checksum algorithm, it is not suitable
4041
for use as a general hash algorithm.
4142

42-
Always returns an unsigned 32-bit integer.
43-
44-
.. note::
45-
To generate the same numeric value across all Python versions and
46-
platforms use adler32(data) & 0xffffffff. If you are only using
47-
the checksum in packed binary format this is not necessary as the
48-
return value is the correct 32bit binary representation
49-
regardless of sign.
43+
.. versionchanged:: 3.0
44+
Always returns an unsigned value.
45+
To generate the same numeric value across all Python versions and
46+
platforms, use ``adler32(data) & 0xffffffff``.
5047

5148

5249
.. function:: compress(data[, level])
@@ -97,23 +94,19 @@ The available exception and functions in this module are:
9794
single: Cyclic Redundancy Check
9895
single: checksum; Cyclic Redundancy Check
9996

100-
Computes a CRC (Cyclic Redundancy Check) checksum of *data*. If *value* is
101-
present, it is used as the starting value of the checksum; otherwise, a fixed
102-
default value is used. This allows computing a running checksum over the
97+
Computes a CRC (Cyclic Redundancy Check) checksum of *data*. The
98+
result is an unsigned 32-bit integer. If *value* is present, it is used
99+
as the starting value of the checksum; otherwise, a default value of 0
100+
is used. Passing in *value* allows computing a running checksum over the
103101
concatenation of several inputs. The algorithm is not cryptographically
104102
strong, and should not be used for authentication or digital signatures. Since
105103
the algorithm is designed for use as a checksum algorithm, it is not suitable
106104
for use as a general hash algorithm.
107105

108-
Always returns an unsigned 32-bit integer.
109-
110-
.. note::
111-
106+
.. versionchanged:: 3.0
107+
Always returns an unsigned value.
112108
To generate the same numeric value across all Python versions and
113-
platforms, use ``crc32(data) & 0xffffffff``. If you are only using
114-
the checksum in packed binary format this is not necessary as the
115-
return value is the correct 32-bit binary representation
116-
regardless of sign.
109+
platforms, use ``crc32(data) & 0xffffffff``.
117110

118111

119112
.. function:: decompress(data[, wbits[, bufsize]])

Lib/gzip.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def __repr__(self):
210210

211211
def _init_write(self, filename):
212212
self.name = filename
213-
self.crc = zlib.crc32(b"") & 0xffffffff
213+
self.crc = zlib.crc32(b"")
214214
self.size = 0
215215
self.writebuf = []
216216
self.bufsize = 0
@@ -261,7 +261,7 @@ def write(self,data):
261261
if length > 0:
262262
self.fileobj.write(self.compress.compress(data))
263263
self.size += length
264-
self.crc = zlib.crc32(data, self.crc) & 0xffffffff
264+
self.crc = zlib.crc32(data, self.crc)
265265
self.offset += length
266266

267267
return length
@@ -381,7 +381,7 @@ def __init__(self, fp):
381381
self._last_mtime = None
382382

383383
def _init_read(self):
384-
self._crc = zlib.crc32(b"") & 0xffffffff
384+
self._crc = zlib.crc32(b"")
385385
self._stream_size = 0 # Decompressed size of unconcatenated stream
386386

387387
def _read_exact(self, n):
@@ -485,7 +485,7 @@ def read(self, size=-1):
485485
return uncompress
486486

487487
def _add_read_data(self, data):
488-
self._crc = zlib.crc32(data, self._crc) & 0xffffffff
488+
self._crc = zlib.crc32(data, self._crc)
489489
self._stream_size = self._stream_size + len(data)
490490

491491
def _read_eof(self):

Lib/tarfile.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,7 @@ def close(self):
459459
self.fileobj.write(self.buf)
460460
self.buf = b""
461461
if self.comptype == "gz":
462-
# The native zlib crc is an unsigned 32-bit integer, but
463-
# the Python wrapper implicitly casts that to a signed C
464-
# long. So, on a 32-bit box self.crc may "look negative",
465-
# while the same crc on a 64-bit box may "look positive".
466-
# To avoid irksome warnings from the `struct` module, force
467-
# it to look positive on all boxes.
468-
self.fileobj.write(struct.pack("<L", self.crc & 0xffffffff))
462+
self.fileobj.write(struct.pack("<L", self.crc))
469463
self.fileobj.write(struct.pack("<L", self.pos & 0xffffFFFF))
470464
finally:
471465
if not self._extfileobj:

Lib/test/test_zlib.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,11 @@ def test_adler32empty(self):
4747
self.assertEqual(zlib.adler32(b"", 1), 1)
4848
self.assertEqual(zlib.adler32(b"", 432), 432)
4949

50-
def assertEqual32(self, seen, expected):
51-
# 32-bit values masked -- checksums on 32- vs 64- bit machines
52-
# This is important if bit 31 (0x08000000L) is set.
53-
self.assertEqual(seen & 0x0FFFFFFFF, expected & 0x0FFFFFFFF)
54-
5550
def test_penguins(self):
56-
self.assertEqual32(zlib.crc32(b"penguin", 0), 0x0e5c1a120)
57-
self.assertEqual32(zlib.crc32(b"penguin", 1), 0x43b6aa94)
58-
self.assertEqual32(zlib.adler32(b"penguin", 0), 0x0bcf02f6)
59-
self.assertEqual32(zlib.adler32(b"penguin", 1), 0x0bd602f7)
51+
self.assertEqual(zlib.crc32(b"penguin", 0), 0x0e5c1a120)
52+
self.assertEqual(zlib.crc32(b"penguin", 1), 0x43b6aa94)
53+
self.assertEqual(zlib.adler32(b"penguin", 0), 0x0bcf02f6)
54+
self.assertEqual(zlib.adler32(b"penguin", 1), 0x0bd602f7)
6055

6156
self.assertEqual(zlib.crc32(b"penguin"), zlib.crc32(b"penguin", 0))
6257
self.assertEqual(zlib.adler32(b"penguin"),zlib.adler32(b"penguin",1))

Lib/zipfile.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ def __init__(self, fileobj, mode, zipinfo, decrypter=None,
734734

735735
if hasattr(zipinfo, 'CRC'):
736736
self._expected_crc = zipinfo.CRC
737-
self._running_crc = crc32(b'') & 0xffffffff
737+
self._running_crc = crc32(b'')
738738
else:
739739
self._expected_crc = None
740740

@@ -856,7 +856,7 @@ def _update_crc(self, newdata):
856856
if self._expected_crc is None:
857857
# No need to compute the CRC if we don't have a reference value
858858
return
859-
self._running_crc = crc32(newdata, self._running_crc) & 0xffffffff
859+
self._running_crc = crc32(newdata, self._running_crc)
860860
# Check the CRC if we're at the end of the file
861861
if self._eof and self._running_crc != self._expected_crc:
862862
raise BadZipFile("Bad CRC-32 for file %r" % self.name)
@@ -1492,7 +1492,7 @@ def write(self, filename, arcname=None, compress_type=None):
14921492
if not buf:
14931493
break
14941494
file_size = file_size + len(buf)
1495-
CRC = crc32(buf, CRC) & 0xffffffff
1495+
CRC = crc32(buf, CRC)
14961496
if cmpr:
14971497
buf = cmpr.compress(buf)
14981498
compress_size = compress_size + len(buf)
@@ -1567,7 +1567,7 @@ def writestr(self, zinfo_or_arcname, data, compress_type=None):
15671567

15681568
self._writecheck(zinfo)
15691569
self._didModify = True
1570-
zinfo.CRC = crc32(data) & 0xffffffff # CRC-32 checksum
1570+
zinfo.CRC = crc32(data) # CRC-32 checksum
15711571
co = _get_compressor(zinfo.compress_type)
15721572
if co:
15731573
data = co.compress(data) + co.flush()

0 commit comments

Comments
 (0)