Skip to content

Commit

Permalink
Use exception classes for constants
Browse files Browse the repository at this point in the history
  • Loading branch information
homm committed Aug 19, 2024
1 parent 619e34b commit e2144b4
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions HeifImagePlugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import subprocess
import tempfile
from copy import copy
from dataclasses import dataclass
from weakref import WeakKeyDictionary

import piexif
Expand All @@ -16,6 +17,22 @@
Transformations = None


@dataclass
class LibheifError:
code: int
subcode: int

def __eq__(self, e):
if not isinstance(e, HeifError):
return False

Check warning on line 27 in HeifImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

HeifImagePlugin.py#L27

Added line #L27 was not covered by tests
return e.code == self.code and e.subcode == self.subcode


class Errors:
end_of_file = LibheifError(7, 100)
unsupported_color_conversion = LibheifError(4, 303)


ffi = FFI()
_keep_refs = WeakKeyDictionary()
HEIF_ENC_BIN = 'heif-enc'
Expand Down Expand Up @@ -151,15 +168,15 @@ def load(self):
try:
heif_file = heif_file.load()
except HeifError as e:
if not (e.code == 4 and e.subcode == 3003):
if e == Errors.unsupported_color_conversion:
raise

Check warning on line 172 in HeifImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

HeifImagePlugin.py#L172

Added line #L172 was not covered by tests
# Unsupported feature: Unsupported color conversion
# https://github.com/strukturag/libheif/issues/1273
self.fp.seek(0)
heif_file = self._open_heif_file(True).load()
except HeifError as e:
# Ignore EOF error and return blank image otherwise
cropped_file = e.code == 7 and e.subcode == 100
cropped_file = e == Errors.end_of_file
if not cropped_file or not ImageFile.LOAD_TRUNCATED_IMAGES:
raise

Expand Down

0 comments on commit e2144b4

Please sign in to comment.