Skip to content

Commit

Permalink
Replace deprecated BadZipfile with BadZipFile (#11821)
Browse files Browse the repository at this point in the history
  • Loading branch information
glyph committed Mar 12, 2023
2 parents 9660271 + 0fe0bbd commit 22956b4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/twisted/newsfragments/11821.bugfix
@@ -0,0 +1,2 @@
BadZipfile (with a small f) has been deprecated since Python 3.2,
use BadZipFile (big F) instead, added in 3.2.
12 changes: 6 additions & 6 deletions src/twisted/python/test/test_zipstream.py
Expand Up @@ -175,7 +175,7 @@ def test_closedArchive(self):

def test_invalidHeader(self):
"""
A zipfile entry with the wrong magic number should raise BadZipfile for
A zipfile entry with the wrong magic number should raise BadZipFile for
readfile(), but that should not affect other files in the archive.
"""
fn = self.makeZipFile(["test contents", "more contents"])
Expand All @@ -186,14 +186,14 @@ def test_invalidHeader(self):
scribble.seek(zeroOffset, 0)
scribble.write(b"0" * 4)
with zipstream.ChunkingZipFile(fn) as czf:
self.assertRaises(zipfile.BadZipfile, czf.readfile, "0")
self.assertRaises(zipfile.BadZipFile, czf.readfile, "0")
with czf.readfile("1") as zfe:
self.assertEqual(zfe.read(), b"more contents")

def test_filenameMismatch(self):
"""
A zipfile entry with a different filename than is found in the central
directory should raise BadZipfile.
directory should raise BadZipFile.
"""
fn = self.makeZipFile([b"test contents", b"more contents"])
with zipfile.ZipFile(fn, "r") as zf:
Expand All @@ -204,14 +204,14 @@ def test_filenameMismatch(self):
scribble.write(info.FileHeader())

with zipstream.ChunkingZipFile(fn) as czf:
self.assertRaises(zipfile.BadZipfile, czf.readfile, "0")
self.assertRaises(zipfile.BadZipFile, czf.readfile, "0")
with czf.readfile("1") as zfe:
self.assertEqual(zfe.read(), b"more contents")

def test_unsupportedCompression(self):
"""
A zipfile which describes an unsupported compression mechanism should
raise BadZipfile.
raise BadZipFile.
"""
fn = self.mktemp()
with zipfile.ZipFile(fn, "w") as zf:
Expand All @@ -223,7 +223,7 @@ def test_unsupportedCompression(self):
zi.compress_type = 1234

with zipstream.ChunkingZipFile(fn) as czf:
self.assertRaises(zipfile.BadZipfile, czf.readfile, "0")
self.assertRaises(zipfile.BadZipFile, czf.readfile, "0")

def test_extraData(self):
"""
Expand Down
6 changes: 3 additions & 3 deletions src/twisted/python/zipstream.py
Expand Up @@ -33,7 +33,7 @@ def readfile(self, name):

fheader = self.fp.read(zipfile.sizeFileHeader)
if fheader[0:4] != zipfile.stringFileHeader:
raise zipfile.BadZipfile("Bad magic number for file header")
raise zipfile.BadZipFile("Bad magic number for file header")

fheader = struct.unpack(zipfile.structFileHeader, fheader)
fname = self.fp.read(fheader[zipfile._FH_FILENAME_LENGTH])
Expand All @@ -48,7 +48,7 @@ def readfile(self, name):
fname_str = fname.decode("cp437")

if fname_str != zinfo.orig_filename:
raise zipfile.BadZipfile(
raise zipfile.BadZipFile(
'File name in directory "%s" and header "%s" differ.'
% (zinfo.orig_filename, fname_str)
)
Expand All @@ -58,7 +58,7 @@ def readfile(self, name):
elif zinfo.compress_type == zipfile.ZIP_DEFLATED:
return DeflatedZipFileEntry(self, zinfo.compress_size)
else:
raise zipfile.BadZipfile(
raise zipfile.BadZipFile(
"Unsupported compression method %d for file %s"
% (zinfo.compress_type, name)
)
Expand Down

0 comments on commit 22956b4

Please sign in to comment.