Close image on ImageFont exception (#9304)

This commit is contained in:
Hugo van Kemenade 2025-11-30 21:56:35 +02:00 committed by GitHub
commit da76f6d99b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

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