Raise SyntaxError if data is not as expected

This commit is contained in:
Andrew Murray 2022-02-26 17:29:21 +11:00
parent fbaaf3c19b
commit efb9d503a7
2 changed files with 17 additions and 9 deletions

View File

@ -2,7 +2,7 @@ from io import BytesIO
import pytest
from PIL import Image
from PIL import Image, XbmImagePlugin
from .helper import hopper
@ -63,6 +63,13 @@ def test_open_filename_with_underscore():
assert im.size == (128, 128)
def test_invalid_file():
invalid_file = "Tests/images/flower.jpg"
with pytest.raises(SyntaxError):
XbmImagePlugin.XbmImageFile(invalid_file)
def test_save_wrong_mode(tmp_path):
im = hopper()
out = str(tmp_path / "temp.xbm")

View File

@ -52,18 +52,19 @@ class XbmImageFile(ImageFile.ImageFile):
m = xbm_head.match(self.fp.read(512))
if m:
if not m:
raise SyntaxError("not a XBM file")
xsize = int(m.group("width"))
ysize = int(m.group("height"))
xsize = int(m.group("width"))
ysize = int(m.group("height"))
if m.group("hotspot"):
self.info["hotspot"] = (int(m.group("xhot")), int(m.group("yhot")))
if m.group("hotspot"):
self.info["hotspot"] = (int(m.group("xhot")), int(m.group("yhot")))
self.mode = "1"
self._size = xsize, ysize
self.mode = "1"
self._size = xsize, ysize
self.tile = [("xbm", (0, 0) + self.size, m.end(), None)]
self.tile = [("xbm", (0, 0) + self.size, m.end(), None)]
def _save(im, fp, filename):