Close image on ImageFont exception

This commit is contained in:
Andrew Murray 2025-11-19 21:52:21 +11:00
parent 71b3e5c015
commit cce73b1e89

View File

@ -127,11 +127,15 @@ class ImageFont:
def _load_pilfont_data(self, file: IO[bytes], image: Image.Image) -> None: def _load_pilfont_data(self, file: IO[bytes], image: Image.Image) -> None:
# check image # check image
if image.mode not in ("1", "L"): if image.mode not in ("1", "L"):
image.close()
msg = "invalid font image mode" msg = "invalid font image mode"
raise TypeError(msg) raise TypeError(msg)
# read PILfont header # read PILfont header
if file.read(8) != b"PILfont\n": if file.read(8) != b"PILfont\n":
image.close()
msg = "Not a PILfont file" msg = "Not a PILfont file"
raise SyntaxError(msg) raise SyntaxError(msg)
file.readline() file.readline()