Skip to content

Commit efb9d50

Browse files
committed
Raise SyntaxError if data is not as expected
1 parent fbaaf3c commit efb9d50

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

Tests/test_file_xbm.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from PIL import Image
5+
from PIL import Image, XbmImagePlugin
66

77
from .helper import hopper
88

@@ -63,6 +63,13 @@ def test_open_filename_with_underscore():
6363
assert im.size == (128, 128)
6464

6565

66+
def test_invalid_file():
67+
invalid_file = "Tests/images/flower.jpg"
68+
69+
with pytest.raises(SyntaxError):
70+
XbmImagePlugin.XbmImageFile(invalid_file)
71+
72+
6673
def test_save_wrong_mode(tmp_path):
6774
im = hopper()
6875
out = str(tmp_path / "temp.xbm")

src/PIL/XbmImagePlugin.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,19 @@ def _open(self):
5252

5353
m = xbm_head.match(self.fp.read(512))
5454

55-
if m:
55+
if not m:
56+
raise SyntaxError("not a XBM file")
5657

57-
xsize = int(m.group("width"))
58-
ysize = int(m.group("height"))
58+
xsize = int(m.group("width"))
59+
ysize = int(m.group("height"))
5960

60-
if m.group("hotspot"):
61-
self.info["hotspot"] = (int(m.group("xhot")), int(m.group("yhot")))
61+
if m.group("hotspot"):
62+
self.info["hotspot"] = (int(m.group("xhot")), int(m.group("yhot")))
6263

63-
self.mode = "1"
64-
self._size = xsize, ysize
64+
self.mode = "1"
65+
self._size = xsize, ysize
6566

66-
self.tile = [("xbm", (0, 0) + self.size, m.end(), None)]
67+
self.tile = [("xbm", (0, 0) + self.size, m.end(), None)]
6768

6869

6970
def _save(im, fp, filename):

0 commit comments

Comments
 (0)