Skip to content

Commit

Permalink
accept returns bool or str
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere authored and nulano committed Apr 6, 2024
1 parent 819e1b9 commit 48b2705
Show file tree
Hide file tree
Showing 27 changed files with 33 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/PIL/BlpImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class BLPFormatError(NotImplementedError):
pass


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


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/BmpImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
}


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


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/BufrStubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def register_handler(handler):
# Image adapter


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


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/CurImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# --------------------------------------------------------------------


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


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/DcxImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
MAGIC = 0x3ADE68B1 # QUIZ: what's this value, then?


def _accept(prefix):
def _accept(prefix: bytes) -> bool:
return len(prefix) >= 4 and i32(prefix) == MAGIC


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/DdsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ def _save(im, fp, filename):
)


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


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/EpsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def readline(self):
return b"".join(s).decode("latin-1")


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


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/FliImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# decoder


def _accept(prefix):
def _accept(prefix: bytes) -> bool:
return (
len(prefix) >= 6
and i16(prefix, 4) in [0xAF11, 0xAF12]
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/FpxImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
# --------------------------------------------------------------------


def _accept(prefix):
def _accept(prefix: bytes) -> bool:
return prefix[:8] == 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 @@ -107,7 +107,7 @@ def load_seek(self, pos):
pass


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


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/GbrImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from ._binary import i32be as i32


def _accept(prefix):
def _accept(prefix: bytes) -> bool:
return len(prefix) >= 8 and i32(prefix, 0) >= 20 and i32(prefix, 4) in (1, 2)


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/GifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class LoadingStrategy(IntEnum):
# Identify/read GIF files


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


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/GribStubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def register_handler(handler):
# Image adapter


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


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/Hdf5StubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def register_handler(handler):
# Image adapter


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


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/IcnsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def _save(im, fp, filename):
fp.flush()


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


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/IcoImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def _save(im, fp, filename):
fp.seek(current)


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


Expand Down
12 changes: 6 additions & 6 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class Quantize(IntEnum):
str,
tuple[
Callable[[IO[bytes], str | bytes], ImageFile.ImageFile],
Callable[[bytes], bool] | None,
Callable[[bytes], bool | str] | None,
],
] = {}
MIME: dict[str, str] = {}
Expand Down Expand Up @@ -3298,7 +3298,7 @@ def open(

preinit()

accept_warnings: list[str | bytes] = []
accept_warnings: list[str] = []

def _open_core(
fp: IO[bytes],
Expand All @@ -3313,8 +3313,8 @@ def _open_core(
try:
factory, accept = OPEN[i]
result = not accept or accept(prefix)
if type(result) in [str, bytes]:
accept_warnings.append(result) # type: ignore[arg-type]
if isinstance(result, str):
accept_warnings.append(result)
elif result:
fp.seek(0)
im = factory(fp, filename)
Expand Down Expand Up @@ -3350,7 +3350,7 @@ def _open_core(
if exclusive_fp:
fp.close()
for message in accept_warnings:
warnings.warn(message) # type: ignore[arg-type]
warnings.warn(message)
msg = "cannot identify image file %r" % (filename if filename else fp)
raise UnidentifiedImageError(msg)

Expand Down Expand Up @@ -3464,7 +3464,7 @@ def merge(mode, bands):
def register_open(
id,
factory: Callable[[IO[bytes], str | bytes], ImageFile.ImageFile],
accept: Callable[[bytes], bool] | None = None,
accept: Callable[[bytes], bool | str] | None = None,
) -> None:
"""
Register an image file plugin. This function should not be used
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/Jpeg2KImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def load(self):
return ImageFile.ImageFile.load(self)


def _accept(prefix):
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"
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/JpegImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def DQT(self, marker):
}


def _accept(prefix):
def _accept(prefix: bytes) -> bool:
# Magic number was taken from https://en.wikipedia.org/wiki/JPEG
return prefix[:3] == b"\xFF\xD8\xFF"

Expand Down
2 changes: 1 addition & 1 deletion src/PIL/MicImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# --------------------------------------------------------------------


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


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/PngImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ def chunk_fdAT(self, pos, length):
# PNG reader


def _accept(prefix):
def _accept(prefix: bytes) -> bool:
return prefix[:8] == _MAGIC


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/PsdImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
# read PSD images


def _accept(prefix):
def _accept(prefix: bytes) -> bool:
return prefix[:4] == b"8BPS"


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/QoiImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ._binary import i32be as i32


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


Expand Down
2 changes: 1 addition & 1 deletion src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@
]


def _accept(prefix):
def _accept(prefix: bytes) -> bool:
return prefix[:4] in PREFIXES


Expand Down
3 changes: 2 additions & 1 deletion src/PIL/WebPImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}


def _accept(prefix):
def _accept(prefix: bytes) -> bool | str:
is_riff_file_format = prefix[:4] == b"RIFF"
is_webp_file = prefix[8:12] == b"WEBP"
is_valid_vp8_mode = prefix[12:16] in _VP8_MODES_BY_IDENTIFIER
Expand All @@ -34,6 +34,7 @@ def _accept(prefix):
"image file could not be identified because WEBP support not installed"
)
return True
return False


class WebPImageFile(ImageFile.ImageFile):
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/WmfImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def load(self, im):
# Read WMF file


def _accept(prefix):
def _accept(prefix: bytes) -> bool:
return (
prefix[:6] == b"\xd7\xcd\xc6\x9a\x00\x00" or prefix[:4] == b"\x01\x00\x00\x00"
)
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/XpmImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
xpm_head = re.compile(b'"([0-9]*) ([0-9]*) ([0-9]*) ([0-9]*)')


def _accept(prefix):
def _accept(prefix: bytes) -> bool:
return prefix[:9] == b"/* XPM */"


Expand Down

0 comments on commit 48b2705

Please sign in to comment.