Commit 49259359 authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #20262: Warnings are raised now when duplicate names are added in the

ZIP file or too long ZIP file comment is truncated.
parent ad715d9e
...@@ -19,7 +19,7 @@ from random import randint, random ...@@ -19,7 +19,7 @@ from random import randint, random
from unittest import skipUnless from unittest import skipUnless
from test.test_support import TESTFN, TESTFN_UNICODE, TESTFN_ENCODING, \ from test.test_support import TESTFN, TESTFN_UNICODE, TESTFN_ENCODING, \
run_unittest, findfile, unlink run_unittest, findfile, unlink, check_warnings
try: try:
TESTFN_UNICODE.encode(TESTFN_ENCODING) TESTFN_UNICODE.encode(TESTFN_ENCODING)
except (UnicodeError, TypeError): except (UnicodeError, TypeError):
...@@ -148,7 +148,9 @@ class TestsWithSourceFile(unittest.TestCase): ...@@ -148,7 +148,9 @@ class TestsWithSourceFile(unittest.TestCase):
# Create the ZIP archive # Create the ZIP archive
with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
zipfp.writestr("name", "foo") zipfp.writestr("name", "foo")
with check_warnings(('', UserWarning)):
zipfp.writestr("name", "bar") zipfp.writestr("name", "bar")
self.assertEqual(zipfp.namelist(), ["name"] * 2)
with zipfile.ZipFile(TESTFN2, "r") as zipfp: with zipfile.ZipFile(TESTFN2, "r") as zipfp:
infos = zipfp.infolist() infos = zipfp.infolist()
...@@ -1035,6 +1037,7 @@ class OtherTests(unittest.TestCase): ...@@ -1035,6 +1037,7 @@ class OtherTests(unittest.TestCase):
# check a comment that is too long is truncated # check a comment that is too long is truncated
with zipfile.ZipFile(TESTFN, mode="w") as zipf: with zipfile.ZipFile(TESTFN, mode="w") as zipf:
with check_warnings(('', UserWarning)):
zipf.comment = comment2 + 'oops' zipf.comment = comment2 + 'oops'
zipf.writestr("foo.txt", "O, for a Muse of Fire!") zipf.writestr("foo.txt", "O, for a Muse of Fire!")
with zipfile.ZipFile(TESTFN, mode="r") as zipf: with zipfile.ZipFile(TESTFN, mode="r") as zipf:
......
...@@ -922,10 +922,10 @@ class ZipFile(object): ...@@ -922,10 +922,10 @@ class ZipFile(object):
@comment.setter @comment.setter
def comment(self, comment): def comment(self, comment):
# check for valid comment length # check for valid comment length
if len(comment) >= ZIP_MAX_COMMENT: if len(comment) > ZIP_MAX_COMMENT:
if self.debug: import warnings
print('Archive comment is too long; truncating to %d bytes' warnings.warn('Archive comment is too long; truncating to %d bytes'
% ZIP_MAX_COMMENT) % ZIP_MAX_COMMENT, stacklevel=2)
comment = comment[:ZIP_MAX_COMMENT] comment = comment[:ZIP_MAX_COMMENT]
self._comment = comment self._comment = comment
self._didModify = True self._didModify = True
...@@ -1088,8 +1088,8 @@ class ZipFile(object): ...@@ -1088,8 +1088,8 @@ class ZipFile(object):
def _writecheck(self, zinfo): def _writecheck(self, zinfo):
"""Check for errors before writing a file to the archive.""" """Check for errors before writing a file to the archive."""
if zinfo.filename in self.NameToInfo: if zinfo.filename in self.NameToInfo:
if self.debug: # Warning for duplicate names import warnings
print "Duplicate name:", zinfo.filename warnings.warn('Duplicate name: %r' % zinfo.filename, stacklevel=3)
if self.mode not in ("w", "a"): if self.mode not in ("w", "a"):
raise RuntimeError, 'write() requires mode "w" or "a"' raise RuntimeError, 'write() requires mode "w" or "a"'
if not self.fp: if not self.fp:
......
...@@ -35,6 +35,9 @@ Core and Builtins ...@@ -35,6 +35,9 @@ Core and Builtins
Library 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 #20270: urllib and urlparse now support empty ports.
- Issue #20243: TarFile no longer raise ReadError when opened in write mode. - Issue #20243: TarFile no longer raise ReadError when opened in write mode.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment