From 7b114b9ca6c046158aa952c6ce5c5b1ef486bde9 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 18 Apr 2026 18:34:24 +1000 Subject: [PATCH 1/2] Simplify code by only returning int --- src/libImaging/Storage.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/libImaging/Storage.c b/src/libImaging/Storage.c index c8175612e35..9cde9a30b75 100644 --- a/src/libImaging/Storage.c +++ b/src/libImaging/Storage.c @@ -548,7 +548,7 @@ ImagingDestroyArrow(Imaging im) { } } -Imaging +int ImagingBorrowArrow( Imaging im, struct ArrowArray *external_array, @@ -571,9 +571,8 @@ ImagingBorrowArrow( } if (!borrowed_buffer) { - return (Imaging)ImagingError_ValueError( - "Arrow Array, exactly 2 buffers required" - ); + ImagingError_ValueError("Arrow Array, exactly 2 buffers required"); + return 0; } for (y = i = 0; y < im->ysize; y++) { @@ -585,7 +584,7 @@ ImagingBorrowArrow( im->arrow_array_capsule = arrow_capsule; im->destroy = ImagingDestroyArrow; - return im; + return 1; } /* -------------------------------------------------------------------- From f110e99ed2c74a6e50be38274e27c15479dd56b1 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 18 Apr 2026 18:26:47 +1000 Subject: [PATCH 2/2] Raise error consistently from inside ImagingNewArrow --- src/_imaging.c | 6 +----- src/libImaging/Storage.c | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/_imaging.c b/src/_imaging.c index 980f827ae78..101d2e61255 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -323,13 +323,9 @@ _new_arrow(PyObject *self, PyObject *args) { mode_id = findModeID(mode); // ImagingBorrowArrow is responsible for retaining the array_capsule - ret = PyImagingNew( + return PyImagingNew( ImagingNewArrow(mode_id, xsize, ysize, schema_capsule, array_capsule) ); - if (!ret) { - return ImagingError_ValueError("Invalid Arrow array mode or size mismatch"); - } - return ret; } /* -------------------------------------------------------------------- */ diff --git a/src/libImaging/Storage.c b/src/libImaging/Storage.c index 9cde9a30b75..76330277e9d 100644 --- a/src/libImaging/Storage.c +++ b/src/libImaging/Storage.c @@ -708,9 +708,11 @@ ImagingNewArrow( && im->bands == 1)) // Single band match && pixels == external_array->length) { // one arrow element per, and it matches a pixelsize*char - if (ImagingBorrowArrow(im, external_array, im->pixelsize, array_capsule)) { - return im; + if (!ImagingBorrowArrow(im, external_array, im->pixelsize, array_capsule)) { + ImagingDelete(im); + return NULL; } + return im; } // Stored as [[r,g,b,a],...] if (strcmp(schema->format, "+w:4") == 0 // 4 up array @@ -724,9 +726,11 @@ ImagingNewArrow( && external_array->children // array is well formed && 4 * pixels == external_array->children[0]->length) { // 4 up element of char into pixelsize == 4 - if (ImagingBorrowArrow(im, external_array, 1, array_capsule)) { - return im; + if (!ImagingBorrowArrow(im, external_array, 1, array_capsule)) { + ImagingDelete(im); + return NULL; } + return im; } // Stored as [r,g,b,a,r,g,b,a,...] if (strcmp(schema->format, "C") == 0 // uint8 @@ -735,13 +739,17 @@ ImagingNewArrow( && strcmp(im->arrow_band_format, "C") == 0 // expected format && 4 * pixels == external_array->length) { // expected length // single flat array, interleaved storage. - if (ImagingBorrowArrow(im, external_array, 1, array_capsule)) { - return im; + if (!ImagingBorrowArrow(im, external_array, 1, array_capsule)) { + ImagingDelete(im); + return NULL; } + return im; } // fmt: on ImagingDelete(im); - return NULL; + return (Imaging)ImagingError_ValueError( + "Invalid Arrow array mode or size mismatch" + ); } Imaging