Skip to content

Commit b667fd8

Browse files
authored
Merge pull request #4720 from radarhere/accept
Call _accept instead of duplicating code
2 parents 2456601 + 6c2d575 commit b667fd8

11 files changed

Lines changed: 12 additions & 13 deletions

src/PIL/BmpImagePlugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def _open(self):
263263
# read 14 bytes: magic number, filesize, reserved, header final offset
264264
head_data = self.fp.read(14)
265265
# choke if the file does not have the required magic bytes
266-
if head_data[0:2] != b"BM":
266+
if not _accept(head_data):
267267
raise SyntaxError("Not a BMP file")
268268
# read the start position of the BMP image data (u32)
269269
offset = i32(head_data[10:14])

src/PIL/DcxImagePlugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def _open(self):
4646

4747
# Header
4848
s = self.fp.read(4)
49-
if i32(s) != MAGIC:
49+
if not _accept(s):
5050
raise SyntaxError("not a DCX file")
5151

5252
# Component directory

src/PIL/FliImagePlugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ def _open(self):
4242

4343
# HEAD
4444
s = self.fp.read(128)
45-
magic = i16(s[4:6])
4645
if not (
47-
magic in [0xAF11, 0xAF12]
46+
_accept(s)
4847
and i16(s[14:16]) in [0, 3] # flags
4948
and s[20:22] == b"\x00\x00" # reserved
5049
):
@@ -60,6 +59,7 @@ def _open(self):
6059

6160
# animation speed
6261
duration = i32(s[16:20])
62+
magic = i16(s[4:6])
6363
if magic == 0xAF11:
6464
duration = (duration * 1000) // 70
6565
self.info["duration"] = duration

src/PIL/GifImagePlugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def _open(self):
6363

6464
# Screen
6565
s = self.fp.read(13)
66-
if s[:6] not in [b"GIF87a", b"GIF89a"]:
66+
if not _accept(s):
6767
raise SyntaxError("not a GIF file")
6868

6969
self.info["version"] = s[:6]

src/PIL/JpegImagePlugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def _open(self):
340340

341341
s = self.fp.read(3)
342342

343-
if s != b"\xFF\xD8\xFF":
343+
if not _accept(s):
344344
raise SyntaxError("not a JPEG file")
345345
s = b"\xFF"
346346

src/PIL/MspImagePlugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def _open(self):
5151

5252
# Header
5353
s = self.fp.read(32)
54-
if s[:4] not in [b"DanM", b"LinS"]:
54+
if not _accept(s):
5555
raise SyntaxError("not an MSP file")
5656

5757
# Header checksum

src/PIL/PixarImagePlugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def _open(self):
4343

4444
# assuming a 4-byte magic label
4545
s = self.fp.read(4)
46-
if s != b"\200\350\000\000":
46+
if not _accept(s):
4747
raise SyntaxError("not a PIXAR file")
4848

4949
# read rest of header

src/PIL/PngImagePlugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ class PngImageFile(ImageFile.ImageFile):
633633

634634
def _open(self):
635635

636-
if self.fp.read(8) != _MAGIC:
636+
if not _accept(self.fp.read(8)):
637637
raise SyntaxError("not a PNG file")
638638
self.__fp = self.fp
639639
self.__frame = 0

src/PIL/PsdImagePlugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def _open(self):
6161
# header
6262

6363
s = read(26)
64-
if s[:4] != b"8BPS" or i16(s[4:]) != 1:
64+
if not _accept(s) or i16(s[4:]) != 1:
6565
raise SyntaxError("not a PSD file")
6666

6767
psd_bits = i16(s[22:])

src/PIL/SgiImagePlugin.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ def _open(self):
5858
headlen = 512
5959
s = self.fp.read(headlen)
6060

61-
# magic number : 474
62-
if i16(s) != 474:
61+
if not _accept(s):
6362
raise ValueError("Not an SGI image file")
6463

6564
# compression : verbatim or RLE

0 commit comments

Comments
 (0)