Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d54088

Browse files
committed
#7351: add more consistent exception name alias.
1 parent b156a46 commit 4d54088

4 files changed

Lines changed: 38 additions & 26 deletions

File tree

Doc/library/zipfile.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,18 @@ For other archive formats, see the :mod:`bz2`, :mod:`gzip`, and
2424

2525
The module defines the following items:
2626

27-
.. exception:: BadZipfile
27+
.. exception:: BadZipFile
2828

2929
The error raised for bad ZIP files (old name: ``zipfile.error``).
3030

31+
.. versionadded:: 3.2
32+
33+
34+
.. exception:: BadZipfile
35+
36+
This is an alias for :exc:`BadZipFile` that exists for compatibility with
37+
Python versions prior to 3.2. Usage is deprecated.
38+
3139

3240
.. exception:: LargeZipFile
3341

Lib/test/test_zipfile.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,8 @@ def test_close_on_exception(self):
463463

464464
try:
465465
with zipfile.ZipFile(TESTFN2, "r") as zipfp2:
466-
raise zipfile.BadZipfile()
467-
except zipfile.BadZipfile:
466+
raise zipfile.BadZipFile()
467+
except zipfile.BadZipFile:
468468
self.assertTrue(zipfp2.fp is None, 'zipfp is not closed')
469469

470470
def tearDown(self):
@@ -723,7 +723,7 @@ def test_close_erroneous_file(self):
723723
fp.write("this is not a legal zip file\n")
724724
try:
725725
zf = zipfile.ZipFile(TESTFN)
726-
except zipfile.BadZipfile:
726+
except zipfile.BadZipFile:
727727
pass
728728

729729
def test_is_zip_erroneous_file(self):
@@ -786,11 +786,11 @@ def test_non_existent_file_raises_IOError(self):
786786
def test_empty_file_raises_BadZipFile(self):
787787
f = open(TESTFN, 'w')
788788
f.close()
789-
self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
789+
self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)
790790

791791
with open(TESTFN, 'w') as fp:
792792
fp.write("short file")
793-
self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
793+
self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)
794794

795795
def test_closed_zip_raises_RuntimeError(self):
796796
"""Verify that testzip() doesn't swallow inappropriate exceptions."""
@@ -912,23 +912,23 @@ def test_testzip_with_bad_crc_deflated(self):
912912
self.check_testzip_with_bad_crc(zipfile.ZIP_DEFLATED)
913913

914914
def check_read_with_bad_crc(self, compression):
915-
"""Tests that files with bad CRCs raise a BadZipfile exception when read."""
915+
"""Tests that files with bad CRCs raise a BadZipFile exception when read."""
916916
zipdata = self.zips_with_bad_crc[compression]
917917

918918
# Using ZipFile.read()
919919
with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
920-
self.assertRaises(zipfile.BadZipfile, zipf.read, 'afile')
920+
self.assertRaises(zipfile.BadZipFile, zipf.read, 'afile')
921921

922922
# Using ZipExtFile.read()
923923
with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
924924
with zipf.open('afile', 'r') as corrupt_file:
925-
self.assertRaises(zipfile.BadZipfile, corrupt_file.read)
925+
self.assertRaises(zipfile.BadZipFile, corrupt_file.read)
926926

927927
# Same with small reads (in order to exercise the buffering logic)
928928
with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
929929
with zipf.open('afile', 'r') as corrupt_file:
930930
corrupt_file.MIN_READ_SIZE = 2
931-
with self.assertRaises(zipfile.BadZipfile):
931+
with self.assertRaises(zipfile.BadZipFile):
932932
while corrupt_file.read(2):
933933
pass
934934

@@ -978,11 +978,11 @@ def test_empty_zipfile(self):
978978

979979
def test_open_empty_file(self):
980980
# Issue 1710703: Check that opening a file with less than 22 bytes
981-
# raises a BadZipfile exception (rather than the previously unhelpful
981+
# raises a BadZipFile exception (rather than the previously unhelpful
982982
# IOError)
983983
f = open(TESTFN, 'w')
984984
f.close()
985-
self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN, 'r')
985+
self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN, 'r')
986986

987987
def tearDown(self):
988988
unlink(TESTFN)

Lib/zipfile.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
zlib = None
2323
crc32 = binascii.crc32
2424

25-
__all__ = ["BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED", "is_zipfile",
26-
"ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile" ]
25+
__all__ = ["BadZipFile", "BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED",
26+
"is_zipfile", "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile"]
2727

28-
class BadZipfile(Exception):
28+
class BadZipFile(Exception):
2929
pass
3030

3131

@@ -35,7 +35,8 @@ class LargeZipFile(Exception):
3535
and those extensions are disabled.
3636
"""
3737

38-
error = BadZipfile # The exception raised by this module
38+
error = BadZipfile = BadZipFile # Pre-3.2 compatibility names
39+
3940

4041
ZIP64_LIMIT = (1 << 31) - 1
4142
ZIP_FILECOUNT_LIMIT = 1 << 16
@@ -180,7 +181,7 @@ def _EndRecData64(fpin, offset, endrec):
180181
return endrec
181182

182183
if diskno != 0 or disks != 1:
183-
raise BadZipfile("zipfiles that span multiple disks are not supported")
184+
raise BadZipZile("zipfiles that span multiple disks are not supported")
184185

185186
# Assume no 'zip64 extensible data'
186187
fpin.seek(offset - sizeEndCentDir64Locator - sizeEndCentDir64, 2)
@@ -592,7 +593,7 @@ def _update_crc(self, newdata, eof):
592593
self._running_crc = crc32(newdata, self._running_crc) & 0xffffffff
593594
# Check the CRC if we're at the end of the file
594595
if eof and self._running_crc != self._expected_crc:
595-
raise BadZipfile("Bad CRC-32 for file %r" % self.name)
596+
raise BadZipFile("Bad CRC-32 for file %r" % self.name)
596597

597598
def read1(self, n):
598599
"""Read up to n bytes with at most one read() system call."""
@@ -720,7 +721,7 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=False):
720721
self._RealGetContents()
721722
# seek to start of directory and overwrite
722723
self.fp.seek(self.start_dir, 0)
723-
except BadZipfile:
724+
except BadZipFile:
724725
# file is not a zip file, just append
725726
self.fp.seek(0, 2)
726727

@@ -744,7 +745,7 @@ def _GetContents(self):
744745
is bad."""
745746
try:
746747
self._RealGetContents()
747-
except BadZipfile:
748+
except BadZipFile:
748749
if not self._filePassed:
749750
self.fp.close()
750751
self.fp = None
@@ -756,9 +757,9 @@ def _RealGetContents(self):
756757
try:
757758
endrec = _EndRecData(fp)
758759
except IOError:
759-
raise BadZipfile("File is not a zip file")
760+
raise BadZipFile("File is not a zip file")
760761
if not endrec:
761-
raise BadZipfile("File is not a zip file")
762+
raise BadZipFile("File is not a zip file")
762763
if self.debug > 1:
763764
print(endrec)
764765
size_cd = endrec[_ECD_SIZE] # bytes in central directory
@@ -783,7 +784,7 @@ def _RealGetContents(self):
783784
while total < size_cd:
784785
centdir = fp.read(sizeCentralDir)
785786
if centdir[0:4] != stringCentralDir:
786-
raise BadZipfile("Bad magic number for central directory")
787+
raise BadZipFile("Bad magic number for central directory")
787788
centdir = struct.unpack(structCentralDir, centdir)
788789
if self.debug > 2:
789790
print(centdir)
@@ -854,7 +855,7 @@ def testzip(self):
854855
f = self.open(zinfo.filename, "r")
855856
while f.read(chunk_size): # Check CRC-32
856857
pass
857-
except BadZipfile:
858+
except BadZipFile:
858859
return zinfo.filename
859860

860861
def getinfo(self, name):
@@ -903,15 +904,15 @@ def open(self, name, mode="r", pwd=None):
903904
# Skip the file header:
904905
fheader = zef_file.read(sizeFileHeader)
905906
if fheader[0:4] != stringFileHeader:
906-
raise BadZipfile("Bad magic number for file header")
907+
raise BadZipFile("Bad magic number for file header")
907908

908909
fheader = struct.unpack(structFileHeader, fheader)
909910
fname = zef_file.read(fheader[_FH_FILENAME_LENGTH])
910911
if fheader[_FH_EXTRA_FIELD_LENGTH]:
911912
zef_file.read(fheader[_FH_EXTRA_FIELD_LENGTH])
912913

913914
if fname != zinfo.orig_filename.encode("utf-8"):
914-
raise BadZipfile(
915+
raise BadZipFile(
915916
'File name in directory %r and header %r differ.'
916917
% (zinfo.orig_filename, fname))
917918

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ Core and Builtins
5151
Library
5252
-------
5353

54+
- Issue #7351: Add ``zipfile.BadZipFile`` spelling of the exception name
55+
and deprecate the old name ``zipfile.BadZipfile``.
56+
5457
- Issue #5027: The standard ``xml`` namespace is now understood by
5558
xml.sax.saxutils.XMLGenerator as being bound to
5659
http://www.w3.org/XML/1998/namespace. Patch by Troy J. Farrell.

0 commit comments

Comments
 (0)