]> 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:40 +0000 (21:57 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 20 Jan 2014 19:57:40 +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 a561d59bc7684077b70ee564b999b7b6fdbaa6dd..12a0f71f5d438e0d5303f11712c61b919f58567d 100644 (file)
@@ -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")
+            with self.assertWarns(UserWarning):
+                zipfp.writestr("name", "bar")
+            self.assertEqual(zipfp.namelist(), ["name"] * 2)
 
         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'
+            with self.assertWarns(UserWarning):
+                zipf.comment = comment2 + b'oops'
             zipf.writestr("foo.txt", "O, for a Muse of Fire!")
         with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
             self.assertEqual(zipfr.comment, comment2)
index a1b34147f835c68d087ab927936b96e20cf78f3c..ff64c90829d93e895857b76675fc362d8565f10b 100644 (file)
@@ -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:
-            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
@@ -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
-                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 c95e45f5a158bf764c5c594ffe4abffe1b0f98e6..62ffaa508a3b2bc4b40f605dec388a1614279e8d 100644 (file)
--- 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
+  ZIP file or too long ZIP file comment is truncated.
+
 - Issue #18574: Added missing newline in 100-Continue reply from
   http.server.BaseHTTPRequestHandler. Patch by Nikolaus Rath.