diff options
author | Martin Panter <vadmium+py@gmail.com> | 2015-12-11 05:19:29 (GMT) |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2015-12-11 05:19:29 (GMT) |
commit | b82032f935962d13220bba52d26ac607149485eb (patch) | |
tree | dd2b37f8a5c8f4e5621739c6f7c5b727727d9542 /Lib | |
parent | 7dda421bfff887da9a84e99c37ef1b0ef9f3cde9 (diff) | |
download | cpython-b82032f935962d13220bba52d26ac607149485eb.zip cpython-b82032f935962d13220bba52d26ac607149485eb.tar.gz cpython-b82032f935962d13220bba52d26ac607149485eb.tar.bz2 |
Issue #22341: Drop Python 2 workaround and document CRC initial value
Also align the parameter naming in binascii to be consistent with zlib.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/gzip.py | 8 | ||||
-rwxr-xr-x | Lib/tarfile.py | 8 | ||||
-rw-r--r-- | Lib/test/test_zlib.py | 13 | ||||
-rw-r--r-- | Lib/zipfile.py | 8 |
4 files changed, 13 insertions, 24 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py index 45152e4..3a2b322 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -210,7 +210,7 @@ class GzipFile(_compression.BaseStream): 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 @@ class GzipFile(_compression.BaseStream): 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 @@ class _GzipReader(_compression.DecompressReader): 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 @@ class _GzipReader(_compression.DecompressReader): 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 --git a/Lib/tarfile.py b/Lib/tarfile.py index ca45126..9d22c8e 100755 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -459,13 +459,7 @@ class _Stream: 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("<L", self.crc & 0xffffffff)) + self.fileobj.write(struct.pack("<L", self.crc)) self.fileobj.write(struct.pack("<L", self.pos & 0xffffFFFF)) finally: if not self._extfileobj: diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py index 88c415b..ecdb5a7 100644 --- a/Lib/test/test_zlib.py +++ b/Lib/test/test_zlib.py @@ -47,16 +47,11 @@ class ChecksumTestCase(unittest.TestCase): self.assertEqual(zlib.adler32(b"", 1), 1) self.assertEqual(zlib.adler32(b"", 432), 432) - def assertEqual32(self, seen, expected): - # 32-bit values masked -- checksums on 32- vs 64- bit machines - # This is important if bit 31 (0x08000000L) is set. - self.assertEqual(seen & 0x0FFFFFFFF, expected & 0x0FFFFFFFF) - def test_penguins(self): - self.assertEqual32(zlib.crc32(b"penguin", 0), 0x0e5c1a120) - self.assertEqual32(zlib.crc32(b"penguin", 1), 0x43b6aa94) - self.assertEqual32(zlib.adler32(b"penguin", 0), 0x0bcf02f6) - self.assertEqual32(zlib.adler32(b"penguin", 1), 0x0bd602f7) + self.assertEqual(zlib.crc32(b"penguin", 0), 0x0e5c1a120) + self.assertEqual(zlib.crc32(b"penguin", 1), 0x43b6aa94) + self.assertEqual(zlib.adler32(b"penguin", 0), 0x0bcf02f6) + self.assertEqual(zlib.adler32(b"penguin", 1), 0x0bd602f7) self.assertEqual(zlib.crc32(b"penguin"), zlib.crc32(b"penguin", 0)) self.assertEqual(zlib.adler32(b"penguin"),zlib.adler32(b"penguin",1)) diff --git a/Lib/zipfile.py b/Lib/zipfile.py index bee00d0..56a2479 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -734,7 +734,7 @@ class ZipExtFile(io.BufferedIOBase): if hasattr(zipinfo, 'CRC'): self._expected_crc = zipinfo.CRC - self._running_crc = crc32(b'') & 0xffffffff + self._running_crc = crc32(b'') else: self._expected_crc = None @@ -856,7 +856,7 @@ class ZipExtFile(io.BufferedIOBase): if self._expected_crc is None: # No need to compute the CRC if we don't have a reference value return - self._running_crc = crc32(newdata, self._running_crc) & 0xffffffff + self._running_crc = crc32(newdata, self._running_crc) # Check the CRC if we're at the end of the file if self._eof and self._running_crc != self._expected_crc: raise BadZipFile("Bad CRC-32 for file %r" % self.name) @@ -1492,7 +1492,7 @@ class ZipFile: if not buf: break file_size = file_size + len(buf) - CRC = crc32(buf, CRC) & 0xffffffff + CRC = crc32(buf, CRC) if cmpr: buf = cmpr.compress(buf) compress_size = compress_size + len(buf) @@ -1567,7 +1567,7 @@ class ZipFile: self._writecheck(zinfo) self._didModify = True - zinfo.CRC = crc32(data) & 0xffffffff # CRC-32 checksum + zinfo.CRC = crc32(data) # CRC-32 checksum co = _get_compressor(zinfo.compress_type) if co: data = co.compress(data) + co.flush() |