Skip to content

Commit

Permalink
correct error handling of aux methods
Browse files Browse the repository at this point in the history
  • Loading branch information
johncf committed Oct 10, 2024
1 parent 6259064 commit a5a6ee7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
11 changes: 6 additions & 5 deletions pillow_heif/_pillow_heif.c
Original file line number Diff line number Diff line change
Expand Up @@ -738,12 +738,13 @@ PyObject* _CtxAuxImage(struct heif_image_handle* main_handle, heif_item_id aux_i
int remove_stride, int hdr_to_16bit, PyObject* file_bytes) {
struct heif_image_handle* aux_handle;
if (check_error(heif_image_handle_get_auxiliary_image_handle(main_handle, aux_image_id, &aux_handle))) {
Py_RETURN_NONE;
return NULL;
}
CtxImageObject *ctx_image = PyObject_New(CtxImageObject, &CtxImage_Type);
if (!ctx_image) {
heif_image_handle_release(aux_handle);
Py_RETURN_NONE;
PyErr_SetString(PyExc_RuntimeError, "Could not create CtxImage object");
return NULL;
}
ctx_image->depth_metadata = NULL;
ctx_image->image_type = PhHeifImage;
Expand Down Expand Up @@ -1228,13 +1229,13 @@ static PyObject* _CtxImage_aux_image_ids(CtxImageObject* self, void* closure) {
return PyList_New(0);
heif_item_id* images_ids = (heif_item_id*)malloc(n_images * sizeof(heif_item_id));
if (!images_ids)
return PyList_New(0);
return PyErr_NoMemory();

n_images = heif_image_handle_get_list_of_auxiliary_image_IDs(self->handle, aux_filter, images_ids, n_images);
PyObject* images_list = PyList_New(n_images);
if (!images_list) {
free(images_ids);
return PyList_New(0);
return PyErr_NoMemory();
}

for (int i = 0; i < n_images; i++) {
Expand Down Expand Up @@ -1299,7 +1300,7 @@ static PyObject* _CtxImage_get_aux_metadata(CtxImageObject* self, PyObject* arg_
heif_item_id aux_image_id = (heif_item_id)PyLong_AsUnsignedLong(arg_image_id);
struct heif_image_handle* aux_handle;
if (check_error(heif_image_handle_get_auxiliary_image_handle(self->handle, aux_image_id, &aux_handle))) {
Py_RETURN_NONE;
return NULL;
}
PyObject* metadata = PyDict_New();
PyObject* aux_type = _get_aux_type(aux_handle);
Expand Down
4 changes: 1 addition & 3 deletions pillow_heif/heif.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def get_aux_image(self, aux_id: int) -> HeifAuxImage:
:returns: a :py:class:`~pillow_heif.HeifAuxImage` class instance.
"""
aux_info = self._c_image.get_aux_metadata(aux_id)
if aux_info is None or aux_info["colorspace"] is None:
if aux_info["colorspace"] is None:
raise RuntimeError("Error while getting auxiliary information.")
colorspace, bit_depth = aux_info["colorspace"], aux_info["bit_depth"]
if colorspace != "monochrome":
Expand All @@ -230,8 +230,6 @@ def get_aux_image(self, aux_id: int) -> HeifAuxImage:
"Please consider filing an issue with an example HEIF file."
)
aux_image = self._c_image.get_aux_image(aux_id)
if aux_image is None:
raise RuntimeError("Error while decoding the auxiliary image.")
return HeifAuxImage(aux_image, aux_info)


Expand Down

0 comments on commit a5a6ee7

Please sign in to comment.