The module defines the following items:
-.. exception:: BadZipfile
+.. exception:: BadZipFile
The error raised for bad ZIP files (old name: ``zipfile.error``).
+ .. versionadded:: 3.2
+
+
+.. exception:: BadZipfile
+
+ This is an alias for :exc:`BadZipFile` that exists for compatibility with
+ Python versions prior to 3.2. Usage is deprecated.
+
.. exception:: LargeZipFile
try:
with zipfile.ZipFile(TESTFN2, "r") as zipfp2:
- raise zipfile.BadZipfile()
- except zipfile.BadZipfile:
+ raise zipfile.BadZipFile()
+ except zipfile.BadZipFile:
self.assertTrue(zipfp2.fp is None, 'zipfp is not closed')
def tearDown(self):
fp.write("this is not a legal zip file\n")
try:
zf = zipfile.ZipFile(TESTFN)
- except zipfile.BadZipfile:
+ except zipfile.BadZipFile:
pass
def test_is_zip_erroneous_file(self):
def test_empty_file_raises_BadZipFile(self):
f = open(TESTFN, 'w')
f.close()
- self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
+ self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)
with open(TESTFN, 'w') as fp:
fp.write("short file")
- self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
+ self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)
def test_closed_zip_raises_RuntimeError(self):
"""Verify that testzip() doesn't swallow inappropriate exceptions."""
self.check_testzip_with_bad_crc(zipfile.ZIP_DEFLATED)
def check_read_with_bad_crc(self, compression):
- """Tests that files with bad CRCs raise a BadZipfile exception when read."""
+ """Tests that files with bad CRCs raise a BadZipFile exception when read."""
zipdata = self.zips_with_bad_crc[compression]
# Using ZipFile.read()
with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
- self.assertRaises(zipfile.BadZipfile, zipf.read, 'afile')
+ self.assertRaises(zipfile.BadZipFile, zipf.read, 'afile')
# Using ZipExtFile.read()
with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
with zipf.open('afile', 'r') as corrupt_file:
- self.assertRaises(zipfile.BadZipfile, corrupt_file.read)
+ self.assertRaises(zipfile.BadZipFile, corrupt_file.read)
# Same with small reads (in order to exercise the buffering logic)
with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
with zipf.open('afile', 'r') as corrupt_file:
corrupt_file.MIN_READ_SIZE = 2
- with self.assertRaises(zipfile.BadZipfile):
+ with self.assertRaises(zipfile.BadZipFile):
while corrupt_file.read(2):
pass
def test_open_empty_file(self):
# Issue 1710703: Check that opening a file with less than 22 bytes
- # raises a BadZipfile exception (rather than the previously unhelpful
+ # raises a BadZipFile exception (rather than the previously unhelpful
# IOError)
f = open(TESTFN, 'w')
f.close()
- self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN, 'r')
+ self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN, 'r')
def tearDown(self):
unlink(TESTFN)
zlib = None
crc32 = binascii.crc32
-__all__ = ["BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED", "is_zipfile",
- "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile" ]
+__all__ = ["BadZipFile", "BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED",
+ "is_zipfile", "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile"]
-class BadZipfile(Exception):
+class BadZipFile(Exception):
pass
and those extensions are disabled.
"""
-error = BadZipfile # The exception raised by this module
+error = BadZipfile = BadZipFile # Pre-3.2 compatibility names
+
ZIP64_LIMIT = (1 << 31) - 1
ZIP_FILECOUNT_LIMIT = 1 << 16
return endrec
if diskno != 0 or disks != 1:
- raise BadZipfile("zipfiles that span multiple disks are not supported")
+ raise BadZipZile("zipfiles that span multiple disks are not supported")
# Assume no 'zip64 extensible data'
fpin.seek(offset - sizeEndCentDir64Locator - sizeEndCentDir64, 2)
self._running_crc = crc32(newdata, self._running_crc) & 0xffffffff
# Check the CRC if we're at the end of the file
if eof and self._running_crc != self._expected_crc:
- raise BadZipfile("Bad CRC-32 for file %r" % self.name)
+ raise BadZipFile("Bad CRC-32 for file %r" % self.name)
def read1(self, n):
"""Read up to n bytes with at most one read() system call."""
self._RealGetContents()
# seek to start of directory and overwrite
self.fp.seek(self.start_dir, 0)
- except BadZipfile:
+ except BadZipFile:
# file is not a zip file, just append
self.fp.seek(0, 2)
is bad."""
try:
self._RealGetContents()
- except BadZipfile:
+ except BadZipFile:
if not self._filePassed:
self.fp.close()
self.fp = None
try:
endrec = _EndRecData(fp)
except IOError:
- raise BadZipfile("File is not a zip file")
+ raise BadZipFile("File is not a zip file")
if not endrec:
- raise BadZipfile("File is not a zip file")
+ raise BadZipFile("File is not a zip file")
if self.debug > 1:
print(endrec)
size_cd = endrec[_ECD_SIZE] # bytes in central directory
while total < size_cd:
centdir = fp.read(sizeCentralDir)
if centdir[0:4] != stringCentralDir:
- raise BadZipfile("Bad magic number for central directory")
+ raise BadZipFile("Bad magic number for central directory")
centdir = struct.unpack(structCentralDir, centdir)
if self.debug > 2:
print(centdir)
f = self.open(zinfo.filename, "r")
while f.read(chunk_size): # Check CRC-32
pass
- except BadZipfile:
+ except BadZipFile:
return zinfo.filename
def getinfo(self, name):
# Skip the file header:
fheader = zef_file.read(sizeFileHeader)
if fheader[0:4] != stringFileHeader:
- raise BadZipfile("Bad magic number for file header")
+ raise BadZipFile("Bad magic number for file header")
fheader = struct.unpack(structFileHeader, fheader)
fname = zef_file.read(fheader[_FH_FILENAME_LENGTH])
zef_file.read(fheader[_FH_EXTRA_FIELD_LENGTH])
if fname != zinfo.orig_filename.encode("utf-8"):
- raise BadZipfile(
+ raise BadZipFile(
'File name in directory %r and header %r differ.'
% (zinfo.orig_filename, fname))
Library
-------
+- Issue #7351: Add ``zipfile.BadZipFile`` spelling of the exception name
+ and deprecate the old name ``zipfile.BadZipfile``.
+
- Issue #5027: The standard ``xml`` namespace is now understood by
xml.sax.saxutils.XMLGenerator as being bound to
http://www.w3.org/XML/1998/namespace. Patch by Troy J. Farrell.