Skip to content

Commit e1b39f1

Browse files
committed
Use exception classes for constants
1 parent 619e34b commit e1b39f1

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

HeifImagePlugin.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import subprocess
22
import tempfile
33
from copy import copy
4+
from dataclasses import dataclass
45
from weakref import WeakKeyDictionary
56

67
import piexif
@@ -16,6 +17,22 @@
1617
Transformations = None
1718

1819

20+
@dataclass
21+
class LibheifError:
22+
code: int
23+
subcode: int
24+
25+
def __eq__(self, e):
26+
if not isinstance(e, HeifError): # pragma: no cover
27+
return False
28+
return e.code == self.code and e.subcode == self.subcode
29+
30+
31+
class Errors:
32+
end_of_file = LibheifError(7, 100)
33+
unsupported_color_conversion = LibheifError(4, 3003)
34+
35+
1936
ffi = FFI()
2037
_keep_refs = WeakKeyDictionary()
2138
HEIF_ENC_BIN = 'heif-enc'
@@ -27,7 +44,7 @@ def _crop_heif_file(heif):
2744
if crop == (0, 0) + heif.size:
2845
return heif
2946

30-
if heif.mode not in ("L", "RGB", "RGBA"):
47+
if heif.mode not in ("L", "RGB", "RGBA"): # pragma: no cover
3148
raise ValueError("Unknown mode")
3249
pixel_size = len(heif.mode)
3350

@@ -151,15 +168,15 @@ def load(self):
151168
try:
152169
heif_file = heif_file.load()
153170
except HeifError as e:
154-
if not (e.code == 4 and e.subcode == 3003):
171+
if e != Errors.unsupported_color_conversion:
155172
raise
156173
# Unsupported feature: Unsupported color conversion
157174
# https://github.com/strukturag/libheif/issues/1273
158175
self.fp.seek(0)
159176
heif_file = self._open_heif_file(True).load()
160177
except HeifError as e:
161178
# Ignore EOF error and return blank image otherwise
162-
cropped_file = e.code == 7 and e.subcode == 100
179+
cropped_file = e == Errors.end_of_file
163180
if not cropped_file or not ImageFile.LOAD_TRUNCATED_IMAGES:
164181
raise
165182

0 commit comments

Comments
 (0)