Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Tests/test_file_mpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def test_app(test_file: str) -> None:
with Image.open(test_file) as im:
assert im.applist[0][0] == "APP1"
assert im.applist[1][0] == "APP2"
assert (
im.applist[1][1][:16] == b"MPF\x00MM\x00*\x00\x00\x00\x08\x00\x03\xb0\x00"
assert im.applist[1][1].startswith(
b"MPF\x00MM\x00*\x00\x00\x00\x08\x00\x03\xb0\x00"
)
assert len(im.applist) == 2

Expand Down
2 changes: 1 addition & 1 deletion Tests/test_file_webp_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_read_exif_metadata() -> None:
def test_read_exif_metadata_without_prefix() -> None:
with Image.open("Tests/images/flower2.webp") as im:
# Assert prefix is not present
assert im.info["exif"][:6] != b"Exif\x00\x00"
assert not im.info["exif"].startswith(b"Exif\x00\x00")

exif = im.getexif()
assert exif[305] == "Adobe Photoshop CS6 (Macintosh)"
Expand Down
4 changes: 2 additions & 2 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ def test_exception_inheritance(self) -> None:

def test_sanity(self) -> None:
im = Image.new("L", (100, 100))
assert repr(im)[:45] == "<PIL.Image.Image image mode=L size=100x100 at"
assert repr(im).startswith("<PIL.Image.Image image mode=L size=100x100 at")
assert im.mode == "L"
assert im.size == (100, 100)

im = Image.new("RGB", (100, 100))
assert repr(im)[:45] == "<PIL.Image.Image image mode=RGB size=100x100 "
assert repr(im).startswith("<PIL.Image.Image image mode=RGB size=100x100 ")
assert im.mode == "RGB"
assert im.size == (100, 100)

Expand Down
2 changes: 1 addition & 1 deletion docs/example/DdsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int


def _accept(prefix: bytes) -> bool:
return prefix[:4] == b"DDS "
return prefix.startswith(b"DDS ")


Image.register_open(DdsImageFile.format, DdsImageFile, _accept)
Expand Down
2 changes: 1 addition & 1 deletion docs/handbook/writing-your-own-image-plugin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ true color.


def _accept(prefix: bytes) -> bool:
return prefix[:4] == b"SPAM"
return prefix.startswith(b"SPAM")


class SpamImageFile(ImageFile.ImageFile):
Expand Down
10 changes: 5 additions & 5 deletions src/PIL/BdfFontFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ def bdf_char(
s = f.readline()
if not s:
return None
if s[:9] == b"STARTCHAR":
if s.startswith(b"STARTCHAR"):
break
id = s[9:].strip().decode("ascii")

# load symbol properties
props = {}
while True:
s = f.readline()
if not s or s[:6] == b"BITMAP":
if not s or s.startswith(b"BITMAP"):
break
i = s.find(b" ")
props[s[:i].decode("ascii")] = s[i + 1 : -1].decode("ascii")
Expand All @@ -60,7 +60,7 @@ def bdf_char(
bitmap = bytearray()
while True:
s = f.readline()
if not s or s[:7] == b"ENDCHAR":
if not s or s.startswith(b"ENDCHAR"):
break
bitmap += s[:-1]

Expand Down Expand Up @@ -96,7 +96,7 @@ def __init__(self, fp: BinaryIO) -> None:
super().__init__()

s = fp.readline()
if s[:13] != b"STARTFONT 2.1":
if not s.startswith(b"STARTFONT 2.1"):
msg = "not a valid BDF file"
raise SyntaxError(msg)

Expand All @@ -105,7 +105,7 @@ def __init__(self, fp: BinaryIO) -> None:

while True:
s = fp.readline()
if not s or s[:13] == b"ENDPROPERTIES":
if not s or s.startswith(b"ENDPROPERTIES"):
break
i = s.find(b" ")
props[s[:i].decode("ascii")] = s[i + 1 : -1].decode("ascii")
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/BlpImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class BLPFormatError(NotImplementedError):


def _accept(prefix: bytes) -> bool:
return prefix[:4] in (b"BLP1", b"BLP2")
return prefix.startswith((b"BLP1", b"BLP2"))


class BlpImageFile(ImageFile.ImageFile):
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/BmpImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@


def _accept(prefix: bytes) -> bool:
return prefix[:2] == b"BM"
return prefix.startswith(b"BM")


def _dib_accept(prefix: bytes) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/BufrStubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def register_handler(handler: ImageFile.StubHandler | None) -> None:


def _accept(prefix: bytes) -> bool:
return prefix[:4] == b"BUFR" or prefix[:4] == b"ZCZC"
return prefix.startswith((b"BUFR", b"ZCZC"))


class BufrStubImageFile(ImageFile.StubImageFile):
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/CurImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


def _accept(prefix: bytes) -> bool:
return prefix[:4] == b"\0\0\2\0"
return prefix.startswith(b"\0\0\2\0")


##
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/DdsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:


def _accept(prefix: bytes) -> bool:
return prefix[:4] == b"DDS "
return prefix.startswith(b"DDS ")


Image.register_open(DdsImageFile.format, DdsImageFile, _accept)
Expand Down
6 changes: 4 additions & 2 deletions src/PIL/EpsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ def Ghostscript(


def _accept(prefix: bytes) -> bool:
return prefix[:4] == b"%!PS" or (len(prefix) >= 4 and i32(prefix) == 0xC6D3D0C5)
return prefix.startswith(b"%!PS") or (
len(prefix) >= 4 and i32(prefix) == 0xC6D3D0C5
)


##
Expand Down Expand Up @@ -295,7 +297,7 @@ def read_comment(s: str) -> bool:
m = field.match(s)
if m:
k = m.group(1)
if k[:8] == "PS-Adobe":
if k.startswith("PS-Adobe"):
self.info["PS-Adobe"] = k[9:]
else:
self.info[k] = ""
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/FitsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


def _accept(prefix: bytes) -> bool:
return prefix[:6] == b"SIMPLE"
return prefix.startswith(b"SIMPLE")


class FitsImageFile(ImageFile.ImageFile):
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/FpxImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@


def _accept(prefix: bytes) -> bool:
return prefix[:8] == olefile.MAGIC
return prefix.startswith(olefile.MAGIC)


##
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/FtexImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def load_seek(self, pos: int) -> None:


def _accept(prefix: bytes) -> bool:
return prefix[:4] == MAGIC
return prefix.startswith(MAGIC)


Image.register_open(FtexImageFile.format, FtexImageFile, _accept)
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/GifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class LoadingStrategy(IntEnum):


def _accept(prefix: bytes) -> bool:
return prefix[:6] in [b"GIF87a", b"GIF89a"]
return prefix.startswith((b"GIF87a", b"GIF89a"))


##
Expand Down Expand Up @@ -257,7 +257,7 @@ def _seek(self, frame: int, update_image: bool = True) -> None:
# application extension
#
info["extension"] = block, self.fp.tell()
if block[:11] == b"NETSCAPE2.0":
if block.startswith(b"NETSCAPE2.0"):
block = self.data()
if block and len(block) >= 3 and block[0] == 1:
self.info["loop"] = i16(block, 1)
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/GimpGradientFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class GimpGradientFile(GradientFile):
"""File handler for GIMP's gradient format."""

def __init__(self, fp: IO[bytes]) -> None:
if fp.readline()[:13] != b"GIMP Gradient":
if not fp.readline().startswith(b"GIMP Gradient"):
msg = "not a GIMP gradient file"
raise SyntaxError(msg)

Expand Down
2 changes: 1 addition & 1 deletion src/PIL/GimpPaletteFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class GimpPaletteFile:
def __init__(self, fp: IO[bytes]) -> None:
palette = [o8(i) * 3 for i in range(256)]

if fp.readline()[:12] != b"GIMP Palette":
if not fp.readline().startswith(b"GIMP Palette"):
msg = "not a GIMP palette file"
raise SyntaxError(msg)

Expand Down
2 changes: 1 addition & 1 deletion src/PIL/GribStubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def register_handler(handler: ImageFile.StubHandler | None) -> None:


def _accept(prefix: bytes) -> bool:
return prefix[:4] == b"GRIB" and prefix[7] == 1
return prefix.startswith(b"GRIB") and prefix[7] == 1


class GribStubImageFile(ImageFile.StubImageFile):
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/Hdf5StubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def register_handler(handler: ImageFile.StubHandler | None) -> None:


def _accept(prefix: bytes) -> bool:
return prefix[:8] == b"\x89HDF\r\n\x1a\n"
return prefix.startswith(b"\x89HDF\r\n\x1a\n")


class HDF5StubImageFile(ImageFile.StubImageFile):
Expand Down
8 changes: 4 additions & 4 deletions src/PIL/IcnsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ def read_png_or_jpeg2000(
sig = fobj.read(12)

im: Image.Image
if sig[:8] == b"\x89PNG\x0d\x0a\x1a\x0a":
if sig.startswith(b"\x89PNG\x0d\x0a\x1a\x0a"):
fobj.seek(start)
im = PngImagePlugin.PngImageFile(fobj)
Image._decompression_bomb_check(im.size)
return {"RGBA": im}
elif (
sig[:4] == b"\xff\x4f\xff\x51"
or sig[:4] == b"\x0d\x0a\x87\x0a"
sig.startswith(b"\xff\x4f\xff\x51")
or sig.startswith(b"\x0d\x0a\x87\x0a")
or sig == b"\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a"
):
if not enable_jpeg2k:
Expand Down Expand Up @@ -387,7 +387,7 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:


def _accept(prefix: bytes) -> bool:
return prefix[:4] == MAGIC
return prefix.startswith(MAGIC)


Image.register_open(IcnsImageFile.format, IcnsImageFile, _accept)
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/IcoImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:


def _accept(prefix: bytes) -> bool:
return prefix[:4] == _MAGIC
return prefix.startswith(_MAGIC)


class IconHeader(NamedTuple):
Expand Down
8 changes: 4 additions & 4 deletions src/PIL/ImImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ def _open(self) -> None:
msg = "not an IM file"
raise SyntaxError(msg)

if s[-2:] == b"\r\n":
if s.endswith(b"\r\n"):
s = s[:-2]
elif s[-1:] == b"\n":
elif s.endswith(b"\n"):
s = s[:-1]

try:
Expand Down Expand Up @@ -209,7 +209,7 @@ def _open(self) -> None:
self._mode = self.info[MODE]

# Skip forward to start of image data
while s and s[:1] != b"\x1a":
while s and not s.startswith(b"\x1a"):
s = self.fp.read(1)
if not s:
msg = "File truncated"
Expand Down Expand Up @@ -247,7 +247,7 @@ def _open(self) -> None:

self._fp = self.fp # FIXME: hack

if self.rawmode[:2] == "F;":
if self.rawmode.startswith("F;"):
# ifunc95 formats
try:
# use bit decoder (if necessary)
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3998,7 +3998,7 @@ def get_ifd(self, tag: int) -> dict[int, Any]:
if tag == ExifTags.IFD.MakerNote:
from .TiffImagePlugin import ImageFileDirectory_v2

if tag_data[:8] == b"FUJIFILM":
if tag_data.startswith(b"FUJIFILM"):
ifd_offset = i32le(tag_data, 8)
ifd_data = tag_data[ifd_offset:]

Expand Down
5 changes: 2 additions & 3 deletions src/PIL/Jpeg2KImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,8 @@ def load(self) -> Image.core.PixelAccess | None:


def _accept(prefix: bytes) -> bool:
return (
prefix[:4] == b"\xff\x4f\xff\x51"
or prefix[:12] == b"\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a"
return prefix.startswith(
(b"\xff\x4f\xff\x51", b"\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a")
)


Expand Down
Loading