[Python-checkins] cpython (3.3): Issue #20262: Warnings are raised now when duplicate names are added in the

serhiy.storchaka python-checkins at python.org
Mon Jan 20 21:14:07 CET 2014


http://hg.python.org/cpython/rev/9fda6658c01a
changeset: 88589:9fda6658c01a
branch: 3.3
parent: 88586:5f754f1e3194
user: Serhiy Storchaka <storchaka at gmail.com>
date: Mon Jan 20 21:57:40 2014 +0200
summary:
 Issue #20262: Warnings are raised now when duplicate names are added in the
ZIP file or too long ZIP file comment is truncated.
files:
 Lib/test/test_zipfile.py | 7 +++++--
 Lib/zipfile.py | 12 ++++++------
 Misc/NEWS | 3 +++
 3 files changed, 14 insertions(+), 8 deletions(-)
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -844,7 +844,9 @@
 # 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 @@
 
 # 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)
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -1102,10 +1102,10 @@
 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 @@
 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:
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -43,6 +43,9 @@
 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.
 
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /