mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-09-24 04:56:57 +03:00
Raise mode error before reading
This commit is contained in:
parent
485d9884cf
commit
caacd38e1b
|
@ -30,6 +30,14 @@ def test_default_font(font: ImageFont.ImageFont) -> None:
|
|||
assert_image_equal_tofile(im, "Tests/images/default_font.png")
|
||||
|
||||
|
||||
def test_invalid_mode() -> None:
|
||||
font = ImageFont.ImageFont()
|
||||
fp = BytesIO()
|
||||
with Image.open("Tests/images/hopper.png") as im:
|
||||
with pytest.raises(TypeError, match="invalid font image mode"):
|
||||
font._load_pilfont_data(fp, im)
|
||||
|
||||
|
||||
def test_without_freetype() -> None:
|
||||
original_core = ImageFont.core
|
||||
if features.check_module("freetype2"):
|
||||
|
|
|
@ -125,6 +125,11 @@ class ImageFont:
|
|||
image.close()
|
||||
|
||||
def _load_pilfont_data(self, file: IO[bytes], image: Image.Image) -> None:
|
||||
# check image
|
||||
if image.mode not in ("1", "L"):
|
||||
msg = "invalid font image mode"
|
||||
raise TypeError(msg)
|
||||
|
||||
# read PILfont header
|
||||
if file.read(8) != b"PILfont\n":
|
||||
msg = "Not a PILfont file"
|
||||
|
@ -140,11 +145,6 @@ class ImageFont:
|
|||
# read PILfont metrics
|
||||
data = file.read(256 * 20)
|
||||
|
||||
# check image
|
||||
if image.mode not in ("1", "L"):
|
||||
msg = "invalid font image mode"
|
||||
raise TypeError(msg)
|
||||
|
||||
image.load()
|
||||
|
||||
self.font = Image.core.font(image.im, data)
|
||||
|
|
Loading…
Reference in New Issue
Block a user