Skip to content

Commit 577d536

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

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

HeifImagePlugin.py

Lines changed: 19 additions & 2 deletions
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):
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'
@@ -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)