mirror of
https://github.com/python/cpython.git
synced 2025-12-07 17:57:56 +00:00
Issue #20262: Warnings are raised now when duplicate names are added in the
ZIP file or too long ZIP file comment is truncated.
This commit is contained in:
commit
c46d1faa4a
3 changed files with 15 additions and 9 deletions
|
|
@ -646,7 +646,7 @@ class PyZipFileTests(unittest.TestCase):
|
||||||
self.assertTrue('SyntaxError' not in reportStr)
|
self.assertTrue('SyntaxError' not in reportStr)
|
||||||
|
|
||||||
# then check that the filter works on individual files
|
# then check that the filter works on individual files
|
||||||
with captured_stdout() as reportSIO:
|
with captured_stdout() as reportSIO, self.assertWarns(UserWarning):
|
||||||
zipfp.writepy(packagedir, filterfunc=lambda fn:
|
zipfp.writepy(packagedir, filterfunc=lambda fn:
|
||||||
'bad' not in fn)
|
'bad' not in fn)
|
||||||
reportStr = reportSIO.getvalue()
|
reportStr = reportSIO.getvalue()
|
||||||
|
|
@ -896,7 +896,9 @@ class OtherTests(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 self.assertWarns(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()
|
||||||
|
|
@ -1213,6 +1215,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 self.assertWarns(UserWarning):
|
||||||
zipf.comment = comment2 + b'oops'
|
zipf.comment = comment2 + b'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 zipfr:
|
with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
|
||||||
|
|
|
||||||
|
|
@ -1104,10 +1104,10 @@ class ZipFile:
|
||||||
if not isinstance(comment, bytes):
|
if not isinstance(comment, bytes):
|
||||||
raise TypeError("comment: expected bytes, got %s" % type(comment))
|
raise TypeError("comment: expected bytes, got %s" % type(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
|
||||||
|
|
@ -1296,8 +1296,8 @@ class ZipFile:
|
||||||
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:
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,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 #20165: The unittest module no longer considers tests marked with
|
- Issue #20165: The unittest module no longer considers tests marked with
|
||||||
@expectedFailure successful if they pass.
|
@expectedFailure successful if they pass.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue