Merge pull request #4720 from radarhere/accept

Call _accept instead of duplicating code
This commit is contained in:
Andrew Murray 2020-06-23 18:29:42 +10:00 committed by GitHub
commit b667fd83cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 12 additions and 13 deletions

View File

@ -263,7 +263,7 @@ class BmpImageFile(ImageFile.ImageFile):
# read 14 bytes: magic number, filesize, reserved, header final offset
head_data = self.fp.read(14)
# choke if the file does not have the required magic bytes
if head_data[0:2] != b"BM":
if not _accept(head_data):
raise SyntaxError("Not a BMP file")
# read the start position of the BMP image data (u32)
offset = i32(head_data[10:14])

View File

@ -46,7 +46,7 @@ class DcxImageFile(PcxImageFile):
# Header
s = self.fp.read(4)
if i32(s) != MAGIC:
if not _accept(s):
raise SyntaxError("not a DCX file")
# Component directory

View File

@ -42,9 +42,8 @@ class FliImageFile(ImageFile.ImageFile):
# HEAD
s = self.fp.read(128)
magic = i16(s[4:6])
if not (
magic in [0xAF11, 0xAF12]
_accept(s)
and i16(s[14:16]) in [0, 3] # flags
and s[20:22] == b"\x00\x00" # reserved
):
@ -60,6 +59,7 @@ class FliImageFile(ImageFile.ImageFile):
# animation speed
duration = i32(s[16:20])
magic = i16(s[4:6])
if magic == 0xAF11:
duration = (duration * 1000) // 70
self.info["duration"] = duration

View File

@ -63,7 +63,7 @@ class GifImageFile(ImageFile.ImageFile):
# Screen
s = self.fp.read(13)
if s[:6] not in [b"GIF87a", b"GIF89a"]:
if not _accept(s):
raise SyntaxError("not a GIF file")
self.info["version"] = s[:6]

View File

@ -340,7 +340,7 @@ class JpegImageFile(ImageFile.ImageFile):
s = self.fp.read(3)
if s != b"\xFF\xD8\xFF":
if not _accept(s):
raise SyntaxError("not a JPEG file")
s = b"\xFF"

View File

@ -51,7 +51,7 @@ class MspImageFile(ImageFile.ImageFile):
# Header
s = self.fp.read(32)
if s[:4] not in [b"DanM", b"LinS"]:
if not _accept(s):
raise SyntaxError("not an MSP file")
# Header checksum

View File

@ -43,7 +43,7 @@ class PixarImageFile(ImageFile.ImageFile):
# assuming a 4-byte magic label
s = self.fp.read(4)
if s != b"\200\350\000\000":
if not _accept(s):
raise SyntaxError("not a PIXAR file")
# read rest of header

View File

@ -633,7 +633,7 @@ class PngImageFile(ImageFile.ImageFile):
def _open(self):
if self.fp.read(8) != _MAGIC:
if not _accept(self.fp.read(8)):
raise SyntaxError("not a PNG file")
self.__fp = self.fp
self.__frame = 0

View File

@ -61,7 +61,7 @@ class PsdImageFile(ImageFile.ImageFile):
# header
s = read(26)
if s[:4] != b"8BPS" or i16(s[4:]) != 1:
if not _accept(s) or i16(s[4:]) != 1:
raise SyntaxError("not a PSD file")
psd_bits = i16(s[22:])

View File

@ -58,8 +58,7 @@ class SgiImageFile(ImageFile.ImageFile):
headlen = 512
s = self.fp.read(headlen)
# magic number : 474
if i16(s) != 474:
if not _accept(s):
raise ValueError("Not an SGI image file")
# compression : verbatim or RLE

View File

@ -53,7 +53,7 @@ class SunImageFile(ImageFile.ImageFile):
# HEAD
s = self.fp.read(32)
if i32(s) != 0x59A66A95:
if not _accept(s):
raise SyntaxError("not an SUN raster file")
offset = 32