From: Gregory P. Smith Date: Wed, 19 Mar 2008 03:14:41 +0000 (+0000) Subject: Fix the struct module DeprecationWarnings that zipfile was triggering by X-Git-Tag: v2.6a2~240 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bf02e3bb21b2d75cba4ce409a14ae64dbc2dd6d2;p=python Fix the struct module DeprecationWarnings that zipfile was triggering by removing all use of signed struct values. test_zipfile and test_zipfile64 pass. no more warnings. --- diff --git a/Lib/zipfile.py b/Lib/zipfile.py index b601e7471e..9735a6a59f 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -36,13 +36,13 @@ ZIP_DEFLATED = 8 # Here are some struct module formats for reading headers structEndArchive = "<4s4H2LH" # 9 items, end of archive, 22 bytes stringEndArchive = "PK\005\006" # magic number for end of archive record -structCentralDir = "<4s4B4HlLL5HLL"# 19 items, central directory, 46 bytes +structCentralDir = "<4s4B4HLLL5HLL"# 19 items, central directory, 46 bytes stringCentralDir = "PK\001\002" # magic number for central directory -structFileHeader = "<4s2B4HlLL2H" # 12 items, file header record, 30 bytes +structFileHeader = "<4s2B4HLLL2H" # 12 items, file header record, 30 bytes stringFileHeader = "PK\003\004" # magic number for file header -structEndArchive64Locator = "<4slql" # 4 items, locate Zip64 header, 20 bytes +structEndArchive64Locator = "<4sLQL" # 4 items, locate Zip64 header, 20 bytes stringEndArchive64Locator = "PK\x06\x07" # magic token for locator header -structEndArchive64 = "<4sqhhllqqqq" # 10 items, end of archive (Zip64), 56 bytes +structEndArchive64 = "<4sQHHLLQQQQ" # 10 items, end of archive (Zip64), 56 bytes stringEndArchive64 = "PK\x06\x06" # magic token for Zip64 header @@ -140,7 +140,7 @@ def _EndRecData(fpin): endrec = list(endrec) endrec.append("") # Append the archive comment endrec.append(filesize - 22) # Append the record start offset - if endrec[-4] == -1 or endrec[-4] == 0xffffffff: + if endrec[-4] == 0xffffffff: return _EndRecData64(fpin, -22, endrec) return endrec # Search the last END_BLOCK bytes of the file for the record signature. @@ -160,7 +160,7 @@ def _EndRecData(fpin): # Append the archive comment and start offset endrec.append(comment) endrec.append(filesize - END_BLOCK + start) - if endrec[-4] == -1 or endrec[-4] == 0xffffffff: + if endrec[-4] == 0xffffffff: return _EndRecData64(fpin, - END_BLOCK + start, endrec) return endrec return # Error, return None @@ -247,7 +247,7 @@ class ZipInfo (object): if file_size > ZIP64_LIMIT or compress_size > ZIP64_LIMIT: # File is larger than what fits into a 4 byte integer, # fall back to the ZIP64 extension - fmt = '= 24: - counts = unpack(' ZIP64_LIMIT: extra.append(zinfo.header_offset) - header_offset = -1 # struct "l" format: 32 one bits + header_offset = 0xffffffffL # -1 32 bit else: header_offset = zinfo.header_offset @@ -1052,14 +1053,25 @@ class ZipFile: extract_version = zinfo.extract_version create_version = zinfo.create_version - centdir = struct.pack(structCentralDir, - stringCentralDir, create_version, - zinfo.create_system, extract_version, zinfo.reserved, - zinfo.flag_bits, zinfo.compress_type, dostime, dosdate, - zinfo.CRC, compress_size, file_size, - len(zinfo.filename), len(extra_data), len(zinfo.comment), - 0, zinfo.internal_attr, zinfo.external_attr, - header_offset) + try: + centdir = struct.pack(structCentralDir, + stringCentralDir, create_version, + zinfo.create_system, extract_version, zinfo.reserved, + zinfo.flag_bits, zinfo.compress_type, dostime, dosdate, + zinfo.CRC, compress_size, file_size, + len(zinfo.filename), len(extra_data), len(zinfo.comment), + 0, zinfo.internal_attr, zinfo.external_attr, + header_offset) + except DeprecationWarning: + print >>sys.stderr, (structCentralDir, + stringCentralDir, create_version, + zinfo.create_system, extract_version, zinfo.reserved, + zinfo.flag_bits, zinfo.compress_type, dostime, dosdate, + zinfo.CRC, compress_size, file_size, + len(zinfo.filename), len(extra_data), len(zinfo.comment), + 0, zinfo.internal_attr, zinfo.external_attr, + header_offset) + raise self.fp.write(centdir) self.fp.write(zinfo.filename) self.fp.write(extra_data) @@ -1079,10 +1091,8 @@ class ZipFile: stringEndArchive64Locator, 0, pos2, 1) self.fp.write(zip64locrec) - # XXX Why is `pos3` computed next? It's never referenced. - pos3 = self.fp.tell() endrec = struct.pack(structEndArchive, stringEndArchive, - 0, 0, count, count, pos2 - pos1, -1, 0) + 0, 0, count, count, pos2 - pos1, 0xffffffffL, 0) self.fp.write(endrec) else: