cpython: 9fda6658c01a (original) (raw)
Mercurial > cpython
changeset 88589:9fda6658c01a 3.3
Issue #20262: Warnings are raised now when duplicate names are added in the ZIP file or too long ZIP file comment is truncated. [#20262]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Mon, 20 Jan 2014 21:57:40 +0200 |
parents | 5f754f1e3194 |
children | f7cebf727bc6 033137c12d88 |
files | Lib/test/test_zipfile.py Lib/zipfile.py Misc/NEWS |
diffstat | 3 files changed, 14 insertions(+), 8 deletions(-)[+] [-] Lib/test/test_zipfile.py 7 Lib/zipfile.py 12 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -844,7 +844,9 @@ class OtherTests(unittest.TestCase): # Create the ZIP archive with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: zipfp.writestr("name", "foo")
zipfp.writestr("name", "bar")[](#l1.7)
with self.assertWarns(UserWarning):[](#l1.8)
zipfp.writestr("name", "bar")[](#l1.9)
self.assertEqual(zipfp.namelist(), ["name"] * 2)[](#l1.10)
with zipfile.ZipFile(TESTFN2, "r") as zipfp: infos = zipfp.infolist() @@ -1150,7 +1152,8 @@ class OtherTests(unittest.TestCase): # check a comment that is too long is truncated with zipfile.ZipFile(TESTFN, mode="w") as zipf:
zipf.comment = comment2 + b'oops'[](#l1.18)
with self.assertWarns(UserWarning):[](#l1.19)
zipf.comment = comment2 + b'oops'[](#l1.20) zipf.writestr("foo.txt", "O, for a Muse of Fire!")[](#l1.21) with zipfile.ZipFile(TESTFN, mode="r") as zipfr:[](#l1.22) self.assertEqual(zipfr.comment, comment2)[](#l1.23)
--- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -1102,10 +1102,10 @@ class ZipFile: if not isinstance(comment, bytes): raise TypeError("comment: expected bytes, got %s" % type(comment)) # check for valid comment length
if len(comment) >= ZIP_MAX_COMMENT:[](#l2.7)
if self.debug:[](#l2.8)
print('Archive comment is too long; truncating to %d bytes'[](#l2.9)
% ZIP_MAX_COMMENT)[](#l2.10)
if len(comment) > ZIP_MAX_COMMENT:[](#l2.11)
import warnings[](#l2.12)
warnings.warn('Archive comment is too long; truncating to %d bytes'[](#l2.13)
% ZIP_MAX_COMMENT, stacklevel=2)[](#l2.14) comment = comment[:ZIP_MAX_COMMENT][](#l2.15) self._comment = comment[](#l2.16) self._didModify = True[](#l2.17)
@@ -1290,8 +1290,8 @@ class ZipFile: def _writecheck(self, zinfo): """Check for errors before writing a file to the archive.""" if zinfo.filename in self.NameToInfo:
if self.debug: # Warning for duplicate names[](#l2.22)
print("Duplicate name:", zinfo.filename)[](#l2.23)
import warnings[](#l2.24)
warnings.warn('Duplicate name: %r' % zinfo.filename, stacklevel=3)[](#l2.25) if self.mode not in ("w", "a"):[](#l2.26) raise RuntimeError('write() requires mode "w" or "a"')[](#l2.27) if not self.fp:[](#l2.28)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -43,6 +43,9 @@ Core and Builtins Library ------- +- Issue #20262: Warnings are raised now when duplicate names are added in the