cpython: ac0ec1f31b0a (original) (raw)
Mercurial > cpython
changeset 76269:ac0ec1f31b0a 2.7
#14399: zipfile now correctly handles comments added to empty zipfiles. Patch by Serhiy Storchaka. [#14399]
R David Murray rdmurray@bitdance.com | |
---|---|
date | Thu, 12 Apr 2012 18:42:47 -0400 |
parents | cc2e3c6d2669 |
children | dd15e1cddefb |
files | Lib/test/test_zipfile.py Lib/zipfile.py Misc/ACKS Misc/NEWS |
diffstat | 4 files changed, 42 insertions(+), 12 deletions(-)[+] [-] Lib/test/test_zipfile.py 16 Lib/zipfile.py 33 Misc/ACKS 1 Misc/NEWS 4 |
line wrap: on
line diff
--- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -908,6 +908,22 @@ class OtherTests(unittest.TestCase): with zipfile.ZipFile(TESTFN, mode="r") as zipf: self.assertEqual(zipf.comment, comment2)
- def test_change_comment_in_empty_archive(self):
with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf:[](#l1.8)
self.assertFalse(zipf.filelist)[](#l1.9)
zipf.comment = b"this is a comment"[](#l1.10)
with zipfile.ZipFile(TESTFN, "r") as zipf:[](#l1.11)
self.assertEqual(zipf.comment, b"this is a comment")[](#l1.12)
- def test_change_comment_in_nonempty_archive(self):
with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf:[](#l1.15)
zipf.writestr("foo.txt", "O, for a Muse of Fire!")[](#l1.16)
with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf:[](#l1.17)
self.assertTrue(zipf.filelist)[](#l1.18)
zipf.comment = b"this is a comment"[](#l1.19)
with zipfile.ZipFile(TESTFN, "r") as zipf:[](#l1.20)
self.assertEqual(zipf.comment, b"this is a comment")[](#l1.21)
+ def check_testzip_with_bad_crc(self, compression): """Tests that files with bad CRCs return their name from testzip.""" zipdata = self.zips_with_bad_crc[compression]
--- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -651,7 +651,7 @@ class ZipExtFile(io.BufferedIOBase): -class ZipFile: +class ZipFile(object): """ Class with methods to open, read, write, close, list zip files. z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=False) @@ -690,7 +690,7 @@ class ZipFile: self.compression = compression # Method of compression self.mode = key = mode.replace('b', '')[0] self.pwd = None
self.comment = ''[](#l2.16)
self._comment = ''[](#l2.17)
# Check if we were passed a file-like object if isinstance(file, basestring): @@ -765,7 +765,7 @@ class ZipFile: print endrec size_cd = endrec[_ECD_SIZE] # bytes in central directory offset_cd = endrec[_ECD_OFFSET] # offset of central directory
self.comment = endrec[_ECD_COMMENT] # archive comment[](#l2.25)
self._comment = endrec[_ECD_COMMENT] # archive comment[](#l2.26)
# "concat" is zero, unless zip was concatenated to another file concat = endrec[_ECD_LOCATION] - size_cd - offset_cd @@ -864,6 +864,22 @@ class ZipFile: """Set default password for encrypted files.""" self.pwd = pwd
- @property
- def comment(self):
"""The comment text associated with the ZIP file."""[](#l2.36)
return self._comment[](#l2.37)
- @comment.setter
- def comment(self, comment):
# check for valid comment length[](#l2.41)
if len(comment) >= ZIP_MAX_COMMENT:[](#l2.42)
if self.debug:[](#l2.43)
print('Archive comment is too long; truncating to %d bytes'[](#l2.44)
% ZIP_MAX_COMMENT)[](#l2.45)
comment = comment[:ZIP_MAX_COMMENT][](#l2.46)
self._comment = comment[](#l2.47)
self._didModify = True[](#l2.48)
+ def read(self, name, pwd=None): """Return file bytes (as a string) for name.""" return self.open(name, "r", pwd).read() @@ -1243,18 +1259,11 @@ class ZipFile: centDirSize = min(centDirSize, 0xFFFFFFFF) centDirOffset = min(centDirOffset, 0xFFFFFFFF)
# check for valid comment length[](#l2.57)
if len(self.comment) >= ZIP_MAX_COMMENT:[](#l2.58)
if self.debug > 0:[](#l2.59)
msg = 'Archive comment is too long; truncating to %d bytes' \[](#l2.60)
% ZIP_MAX_COMMENT[](#l2.61)
self.comment = self.comment[:ZIP_MAX_COMMENT][](#l2.62)
- endrec = struct.pack(structEndArchive, stringEndArchive, 0, 0, centDirCount, centDirCount,
centDirSize, centDirOffset, len(self.comment))[](#l2.66)
centDirSize, centDirOffset, len(self._comment))[](#l2.67) self.fp.write(endrec)[](#l2.68)
self.fp.write(self.comment)[](#l2.69)
self.fp.write(self._comment)[](#l2.70) self.fp.flush()[](#l2.71)
--- a/Misc/ACKS +++ b/Misc/ACKS @@ -814,6 +814,7 @@ Richard Stoakley Peter Stoehr Casper Stoel Michael Stone +Serhiy Storchaka Ken Stox Patrick Strawderman Dan Stromberg
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -44,6 +44,10 @@ Core and Builtins Library ------- +- Issue #14399: zipfile now correctly adds a comment even when the zipfile