# HG changeset patch # Parent 1ad3d8d82b18c5a0326ea739e7aaf1590543a896 # Parent 1d0d8b27a4e69e81755e01b9f4df1649a67e11fa Issue #22341: Drop Python 2 workaround and document CRC initial value Also align the parameter naming in binascii to be consistent with zlib. diff -r 1d0d8b27a4e6 Doc/library/binascii.rst --- a/Doc/library/binascii.rst Tue Dec 08 12:14:50 2015 +0200 +++ b/Doc/library/binascii.rst Tue Dec 08 21:19:31 2015 +0000 @@ -110,15 +110,16 @@ possibly the last fragment). -.. function:: crc_hqx(data, crc) +.. function:: crc_hqx(data, value) - Compute the binhex4 crc value of *data*, starting with an initial *crc* and - returning the result. + Compute the binhex4 crc value of *data*, starting with *value* as the + initial crc, and return the result. -.. function:: crc32(data[, crc]) +.. function:: crc32(data[, value]) - Compute CRC-32, the 32-bit checksum of data, starting with an initial crc. This + Compute CRC-32, the 32-bit checksum of *data*, starting with an + initial CRC of *value*. The default initial CRC is zero. The algorithm is consistent with the ZIP file checksum. Since the algorithm is designed for use as a checksum algorithm, it is not suitable for use as a general hash algorithm. Use as follows:: @@ -126,15 +127,13 @@ print(binascii.crc32(b"hello world")) # Or, in two pieces: crc = binascii.crc32(b"hello") - crc = binascii.crc32(b" world", crc) & 0xffffffff + crc = binascii.crc32(b" world", crc) print('crc32 = {:#010x}'.format(crc)) -.. note:: - To generate the same numeric value across all Python versions and - platforms use crc32(data) & 0xffffffff. If you are only using - the checksum in packed binary format this is not necessary as the - return value is the correct 32bit binary representation - regardless of sign. + .. versionchanged:: 3.0 + The result is always unsigned. + To generate the same numeric value across all Python versions and + platforms, use ``crc32(data) & 0xffffffff``. .. function:: b2a_hex(data) diff -r 1d0d8b27a4e6 Doc/library/zlib.rst --- a/Doc/library/zlib.rst Tue Dec 08 12:14:50 2015 +0200 +++ b/Doc/library/zlib.rst Tue Dec 08 21:19:31 2015 +0000 @@ -31,22 +31,19 @@ .. function:: adler32(data[, value]) Computes an Adler-32 checksum of *data*. (An Adler-32 checksum is almost as - reliable as a CRC32 but can be computed much more quickly.) If *value* is - present, it is used as the starting value of the checksum; otherwise, a fixed - default value is used. This allows computing a running checksum over the + reliable as a CRC32 but can be computed much more quickly.) The + result is an unsigned 32-bit integer. If *value* is present, it is used + as the starting value of the checksum; otherwise, a default value + of 1 is used. This allows computing a running checksum over the concatenation of several inputs. The algorithm is not cryptographically strong, and should not be used for authentication or digital signatures. Since the algorithm is designed for use as a checksum algorithm, it is not suitable for use as a general hash algorithm. - Always returns an unsigned 32-bit integer. - -.. note:: - To generate the same numeric value across all Python versions and - platforms use adler32(data) & 0xffffffff. If you are only using - the checksum in packed binary format this is not necessary as the - return value is the correct 32bit binary representation - regardless of sign. + .. versionchanged:: 3.0 + Always returns an unsigned value. + To generate the same numeric value across all Python versions and + platforms, use ``adler32(data) & 0xffffffff``. .. function:: compress(data[, level]) @@ -97,23 +94,19 @@ single: Cyclic Redundancy Check single: checksum; Cyclic Redundancy Check - Computes a CRC (Cyclic Redundancy Check) checksum of *data*. If *value* is - present, it is used as the starting value of the checksum; otherwise, a fixed - default value is used. This allows computing a running checksum over the + Computes a CRC (Cyclic Redundancy Check) checksum of *data*. + The result is an unsigned 32-bit integer. If *value* is present, it is + used as the starting value of the checksum; otherwise, a default value + of zero is used. This allows computing a running checksum over the concatenation of several inputs. The algorithm is not cryptographically strong, and should not be used for authentication or digital signatures. Since the algorithm is designed for use as a checksum algorithm, it is not suitable for use as a general hash algorithm. - Always returns an unsigned 32-bit integer. - - .. note:: - + .. versionchanged:: 3.0 + Always returns an unsigned value. To generate the same numeric value across all Python versions and - platforms, use ``crc32(data) & 0xffffffff``. If you are only using - the checksum in packed binary format this is not necessary as the - return value is the correct 32-bit binary representation - regardless of sign. + platforms, use ``crc32(data) & 0xffffffff``. .. function:: decompress(data[, wbits[, bufsize]]) diff -r 1d0d8b27a4e6 Lib/gzip.py --- a/Lib/gzip.py Tue Dec 08 12:14:50 2015 +0200 +++ b/Lib/gzip.py Tue Dec 08 21:19:31 2015 +0000 @@ -210,7 +210,7 @@ def _init_write(self, filename): self.name = filename - self.crc = zlib.crc32(b"") & 0xffffffff + self.crc = zlib.crc32(b"") self.size = 0 self.writebuf = [] self.bufsize = 0 @@ -261,7 +261,7 @@ if length > 0: self.fileobj.write(self.compress.compress(data)) self.size += length - self.crc = zlib.crc32(data, self.crc) & 0xffffffff + self.crc = zlib.crc32(data, self.crc) self.offset += length return length @@ -381,7 +381,7 @@ self._last_mtime = None def _init_read(self): - self._crc = zlib.crc32(b"") & 0xffffffff + self._crc = zlib.crc32(b"") self._stream_size = 0 # Decompressed size of unconcatenated stream def _read_exact(self, n): @@ -485,7 +485,7 @@ return uncompress def _add_read_data(self, data): - self._crc = zlib.crc32(data, self._crc) & 0xffffffff + self._crc = zlib.crc32(data, self._crc) self._stream_size = self._stream_size + len(data) def _read_eof(self): diff -r 1d0d8b27a4e6 Lib/tarfile.py --- a/Lib/tarfile.py Tue Dec 08 12:14:50 2015 +0200 +++ b/Lib/tarfile.py Tue Dec 08 21:19:31 2015 +0000 @@ -459,13 +459,7 @@ self.fileobj.write(self.buf) self.buf = b"" if self.comptype == "gz": - # The native zlib crc is an unsigned 32-bit integer, but - # the Python wrapper implicitly casts that to a signed C - # long. So, on a 32-bit box self.crc may "look negative", - # while the same crc on a 64-bit box may "look positive". - # To avoid irksome warnings from the `struct` module, force - # it to look positive on all boxes. - self.fileobj.write(struct.pack("