]> granicus.if.org Git - python/commitdiff
Issue #20262: Warnings are raised now when duplicate names are added in the
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 20 Jan 2014 19:57:09 +0000 (21:57 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 20 Jan 2014 19:57:09 +0000 (21:57 +0200)
ZIP file or too long ZIP file comment is truncated.

Lib/test/test_zipfile.py
Lib/zipfile.py
Misc/NEWS

index 9e3a28b4383e7ffbab50eb0f1b781429f8d0d41f..97a71e6439353fe6cd3a655cf4a3c98f2ff93a01 100644 (file)
@@ -19,7 +19,7 @@ from random import randint, random
 from unittest import skipUnless
 
 from test.test_support import TESTFN, TESTFN_UNICODE, TESTFN_ENCODING, \
-                              run_unittest, findfile, unlink
+                              run_unittest, findfile, unlink, check_warnings
 try:
     TESTFN_UNICODE.encode(TESTFN_ENCODING)
 except (UnicodeError, TypeError):
@@ -148,7 +148,9 @@ class TestsWithSourceFile(unittest.TestCase):
         # Create the ZIP archive
         with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
             zipfp.writestr("name", "foo")
-            zipfp.writestr("name", "bar")
+            with check_warnings(('', UserWarning)):
+                zipfp.writestr("name", "bar")
+            self.assertEqual(zipfp.namelist(), ["name"] * 2)
 
         with zipfile.ZipFile(TESTFN2, "r") as zipfp:
             infos = zipfp.infolist()
@@ -1035,7 +1037,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 + 'oops'
+            with check_warnings(('', UserWarning)):
+                zipf.comment = comment2 + 'oops'
             zipf.writestr("foo.txt", "O, for a Muse of Fire!")
         with zipfile.ZipFile(TESTFN, mode="r") as zipf:
             self.assertEqual(zipf.comment, comment2)
index 82d240f46693b7bf6b545dde85ac70b4b26f8705..46328f5323f3eabee19f98f5a2ebe9cc0c59d10d 100644 (file)
@@ -922,10 +922,10 @@ class ZipFile(object):
     @comment.setter
     def comment(self, comment):
         # check for valid comment length
-        if len(comment) >= ZIP_MAX_COMMENT:
-            if self.debug:
-                print('Archive comment is too long; truncating to %d bytes'
-                        % ZIP_MAX_COMMENT)
+        if len(comment) > ZIP_MAX_COMMENT:
+            import warnings
+            warnings.warn('Archive comment is too long; truncating to %d bytes'
+                          % ZIP_MAX_COMMENT, stacklevel=2)
             comment = comment[:ZIP_MAX_COMMENT]
         self._comment = comment
         self._didModify = True
@@ -1088,8 +1088,8 @@ class ZipFile(object):
     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
-                print "Duplicate name:", zinfo.filename
+            import warnings
+            warnings.warn('Duplicate name: %r' % zinfo.filename, stacklevel=3)
         if self.mode not in ("w", "a"):
             raise RuntimeError, 'write() requires mode "w" or "a"'
         if not self.fp:
index 40a9c5d190e524fe89075694665fa60842c774fc..26bd48543e266a2f707ad3dc44b7d67ea61dcb50 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -35,6 +35,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #20262: Warnings are raised now when duplicate names are added in the
+  ZIP file or too long ZIP file comment is truncated.
+
 - Issue #20270: urllib and urlparse now support empty ports.
 
 - Issue #20243: TarFile no longer raise ReadError when opened in write mode.