[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2025-01-21 21:41:54 +00:00
parent 97eb7c09ba
commit f1349e973d
8 changed files with 413 additions and 414 deletions

View File

@ -1,19 +1,20 @@
from __future__ import annotations from __future__ import annotations
import warnings from typing import Any # undone
import pytest import pytest
from PIL import Image from PIL import Image
from .helper import assert_deep_equal, assert_image, hopper, skip_unless_feature, assert_image_equal from .helper import (
assert_deep_equal,
from typing import Any # undone assert_image_equal,
hopper,
)
pyarrow = pytest.importorskip("pyarrow", reason="PyArrow not installed") pyarrow = pytest.importorskip("pyarrow", reason="PyArrow not installed")
TEST_IMAGE_SIZE = (10, 10) TEST_IMAGE_SIZE = (10, 10)
from numbers import Number
def _test_img_equals_pyarray(img: Image.Image, arr: Any, mask) -> None: def _test_img_equals_pyarray(img: Image.Image, arr: Any, mask) -> None:
@ -30,14 +31,11 @@ def _test_img_equals_pyarray(img: Image.Image, arr: Any, mask) -> None:
# really hard to get a non-nullable list type # really hard to get a non-nullable list type
fl_uint8_4_type = pyarrow.field("_", fl_uint8_4_type = pyarrow.field(
pyarrow.list_( "_", pyarrow.list_(pyarrow.field("_", pyarrow.uint8()).with_nullable(False), 4)
pyarrow.field("_",
pyarrow.uint8()
).with_nullable(False)
,4)
).type ).type
@pytest.mark.parametrize( @pytest.mark.parametrize(
"mode, dtype, mask", "mode, dtype, mask",
( (
@ -70,38 +68,39 @@ def test_to_array(mode: str, dtype: Any, mask: Any ) -> None:
assert_image_equal(img, reloaded) assert_image_equal(img, reloaded)
def test_lifetime(): def test_lifetime():
# valgrind shouldn't error out here. # valgrind shouldn't error out here.
# arrays should be accessible after the image is deleted. # arrays should be accessible after the image is deleted.
img = hopper('L') img = hopper("L")
arr_1 = pyarrow.array(img) arr_1 = pyarrow.array(img)
arr_2 = pyarrow.array(img) arr_2 = pyarrow.array(img)
del(img) del img
assert arr_1.sum().as_py() > 0 assert arr_1.sum().as_py() > 0
del(arr_1) del arr_1
assert arr_2.sum().as_py() > 0 assert arr_2.sum().as_py() > 0
del(arr_2) del arr_2
def test_lifetime2(): def test_lifetime2():
# valgrind shouldn't error out here. # valgrind shouldn't error out here.
# img should remain after the arrays are collected. # img should remain after the arrays are collected.
img = hopper('L') img = hopper("L")
arr_1 = pyarrow.array(img) arr_1 = pyarrow.array(img)
arr_2 = pyarrow.array(img) arr_2 = pyarrow.array(img)
assert arr_1.sum().as_py() > 0 assert arr_1.sum().as_py() > 0
del(arr_1) del arr_1
assert arr_2.sum().as_py() > 0 assert arr_2.sum().as_py() > 0
del(arr_2) del arr_2
img2 = img.copy() img2 = img.copy()
px = img2.load() px = img2.load()

View File

@ -61,12 +61,12 @@ optional-dependencies.tests = [
"markdown2", "markdown2",
"olefile", "olefile",
"packaging", "packaging",
"pyarrow",
"pyroma", "pyroma",
"pytest", "pytest",
"pytest-cov", "pytest-cov",
"pytest-timeout", "pytest-timeout",
"trove-classifiers>=2024.10.12", "trove-classifiers>=2024.10.12",
"pyarrow",
] ]
optional-dependencies.typing = [ optional-dependencies.typing = [
"typing-extensions; python_version<'3.10'", "typing-extensions; python_version<'3.10'",

View File

@ -748,12 +748,13 @@ class Image:
new["shape"], new["typestr"] = _conv_type_shape(self) new["shape"], new["typestr"] = _conv_type_shape(self)
return new return new
def __arrow_c_schema__(self) -> object: def __arrow_c_schema__(self) -> object:
self.load() self.load()
return self.im.__arrow_c_schema__() return self.im.__arrow_c_schema__()
def __arrow_c_array__(self, requested_schema: object | None = None) -> Tuple[object, object]: def __arrow_c_array__(
self, requested_schema: object | None = None
) -> Tuple[object, object]:
self.load() self.load()
return (self.im.__arrow_c_schema__(), self.im.__arrow_c_array__()) return (self.im.__arrow_c_schema__(), self.im.__arrow_c_array__())
@ -3258,12 +3259,15 @@ class SupportsArrayInterface(Protocol):
def __array_interface__(self) -> dict[str, Any]: def __array_interface__(self) -> dict[str, Any]:
raise NotImplementedError() raise NotImplementedError()
class SupportsArrowArrayInterface(Protocol): class SupportsArrowArrayInterface(Protocol):
""" """
An object that has an ``__arrow_c_array__`` method corresponding to the arrow c data interface. An object that has an ``__arrow_c_array__`` method corresponding to the arrow c data interface.
""" """
def __arrow_c_array__(self, requested_schema:"PyCapsule"=None) -> tuple["PyCapsule", "PyCapsule"]: def __arrow_c_array__(
self, requested_schema: PyCapsule = None
) -> tuple[PyCapsule, PyCapsule]:
raise NotImplementedError() raise NotImplementedError()
@ -3356,12 +3360,12 @@ def fromarray(obj: SupportsArrayInterface, mode: str | None = None) -> Image:
def fromarrow(obj: SupportsArrowArrayIngerface, mode, size) -> ImageFile.ImageFile: def fromarrow(obj: SupportsArrowArrayIngerface, mode, size) -> ImageFile.ImageFile:
if not hasattr(obj, '__arrow_c_array__'): if not hasattr(obj, "__arrow_c_array__"):
raise ValueError("arrow_c_array interface not found") raise ValueError("arrow_c_array interface not found")
(schema_capsule, array_capsule) = obj.__arrow_c_array__() (schema_capsule, array_capsule) = obj.__arrow_c_array__()
_im = core.new_arrow(mode, size, schema_capsule, array_capsule) _im = core.new_arrow(mode, size, schema_capsule, array_capsule)
if (_im): if _im:
return Image()._new(_im) return Image()._new(_im)
return None return None

View File

@ -227,7 +227,8 @@ PyImaging_GetBuffer(PyObject *buffer, Py_buffer *view) {
/* Arrow HANDLING */ /* Arrow HANDLING */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
void ReleaseArrowSchemaPyCapsule(PyObject* capsule) { void
ReleaseArrowSchemaPyCapsule(PyObject *capsule) {
struct ArrowSchema *schema = struct ArrowSchema *schema =
(struct ArrowSchema *)PyCapsule_GetPointer(capsule, "arrow_schema"); (struct ArrowSchema *)PyCapsule_GetPointer(capsule, "arrow_schema");
if (schema->release != NULL) { if (schema->release != NULL) {
@ -236,14 +237,16 @@ void ReleaseArrowSchemaPyCapsule(PyObject* capsule) {
free(schema); free(schema);
} }
PyObject* ExportArrowSchemaPyCapsule(ImagingObject *self) { PyObject *
ExportArrowSchemaPyCapsule(ImagingObject *self) {
struct ArrowSchema *schema = struct ArrowSchema *schema =
(struct ArrowSchema *)calloc(1, sizeof(struct ArrowSchema)); (struct ArrowSchema *)calloc(1, sizeof(struct ArrowSchema));
export_imaging_schema(self->image, schema); export_imaging_schema(self->image, schema);
return PyCapsule_New(schema, "arrow_schema", ReleaseArrowSchemaPyCapsule); return PyCapsule_New(schema, "arrow_schema", ReleaseArrowSchemaPyCapsule);
} }
void ReleaseArrowArrayPyCapsule(PyObject* capsule) { void
ReleaseArrowArrayPyCapsule(PyObject *capsule) {
struct ArrowArray *array = struct ArrowArray *array =
(struct ArrowArray *)PyCapsule_GetPointer(capsule, "arrow_array"); (struct ArrowArray *)PyCapsule_GetPointer(capsule, "arrow_array");
if (array->release != NULL) { if (array->release != NULL) {
@ -252,14 +255,14 @@ void ReleaseArrowArrayPyCapsule(PyObject* capsule) {
free(array); free(array);
} }
PyObject* ExportArrowArrayPyCapsule(ImagingObject *self) { PyObject *
ExportArrowArrayPyCapsule(ImagingObject *self) {
struct ArrowArray *array = struct ArrowArray *array =
(struct ArrowArray *)calloc(1, sizeof(struct ArrowArray)); (struct ArrowArray *)calloc(1, sizeof(struct ArrowArray));
export_imaging_array(self->image, array); export_imaging_array(self->image, array);
return PyCapsule_New(array, "arrow_array", ReleaseArrowArrayPyCapsule); return PyCapsule_New(array, "arrow_array", ReleaseArrowArrayPyCapsule);
} }
static PyObject * static PyObject *
_new_arrow(PyObject *self, PyObject *args) { _new_arrow(PyObject *self, PyObject *args) {
char *mode; char *mode;
@ -267,8 +270,9 @@ _new_arrow(PyObject *self, PyObject *args) {
PyObject *schema_capsule, *array_capsule; PyObject *schema_capsule, *array_capsule;
PyObject *ret; PyObject *ret;
if (!PyArg_ParseTuple(args, "s(ii)OO", &mode, &xsize, &ysize, if (!PyArg_ParseTuple(
&schema_capsule, &array_capsule)) { args, "s(ii)OO", &mode, &xsize, &ysize, &schema_capsule, &array_capsule
)) {
return NULL; return NULL;
} }
@ -290,8 +294,6 @@ _new_arrow(PyObject *self, PyObject *args) {
return ret; return ret;
} }
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* EXCEPTION REROUTING */ /* EXCEPTION REROUTING */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */

View File

@ -48,12 +48,8 @@ ReleaseExportedSchema(struct ArrowSchema* array) {
array->release = NULL; array->release = NULL;
} }
int
export_named_type(struct ArrowSchema *schema, char *format, char *name) {
int export_named_type(struct ArrowSchema* schema,
char* format,
char* name) {
char *formatp; char *formatp;
char *namep; char *namep;
size_t format_len = strlen(format) + 1; size_t format_len = strlen(format) + 1;
@ -74,8 +70,7 @@ int export_named_type(struct ArrowSchema* schema,
strncpy(formatp, format, format_len); strncpy(formatp, format, format_len);
strncpy(namep, name, name_len); strncpy(namep, name, name_len);
*schema = (struct ArrowSchema) { *schema = (struct ArrowSchema){// Type description
// Type description
.format = formatp, .format = formatp,
.name = namep, .name = namep,
.metadata = NULL, .metadata = NULL,
@ -89,7 +84,8 @@ int export_named_type(struct ArrowSchema* schema,
return 0; return 0;
} }
int export_imaging_schema(Imaging im, struct ArrowSchema* schema) { int
export_imaging_schema(Imaging im, struct ArrowSchema *schema) {
int retval = 0; int retval = 0;
if (strcmp(im->arrow_band_format, "") == 0) { if (strcmp(im->arrow_band_format, "") == 0) {
@ -116,14 +112,15 @@ int export_imaging_schema(Imaging im, struct ArrowSchema* schema) {
return 0; return 0;
} }
static void release_simple_type(struct ArrowSchema* schema) { static void
release_simple_type(struct ArrowSchema *schema) {
// Mark released // Mark released
schema->release = NULL; schema->release = NULL;
} }
void export_uint32_type(struct ArrowSchema* schema) { void
*schema = (struct ArrowSchema) { export_uint32_type(struct ArrowSchema *schema) {
// Type description *schema = (struct ArrowSchema){// Type description
.format = "I", .format = "I",
.name = "", .name = "",
.metadata = NULL, .metadata = NULL,
@ -136,8 +133,8 @@ void export_uint32_type(struct ArrowSchema* schema) {
}; };
} }
static void
static void release_uint32_array(struct ArrowArray* array) { release_uint32_array(struct ArrowArray *array) {
// assert(array->n_buffers == 2); // assert(array->n_buffers == 2);
// Free the buffers and the buffers array // Free the buffers and the buffers array
free((void *)array->buffers[1]); free((void *)array->buffers[1]);
@ -146,11 +143,10 @@ static void release_uint32_array(struct ArrowArray* array) {
array->release = NULL; array->release = NULL;
} }
void export_uint32_array(const uint32_t* data, int64_t nitems, void
struct ArrowArray* array) { export_uint32_array(const uint32_t *data, int64_t nitems, struct ArrowArray *array) {
// Initialize primitive fields // Initialize primitive fields
*array = (struct ArrowArray) { *array = (struct ArrowArray){// Data description
// Data description
.length = nitems, .length = nitems,
.offset = 0, .offset = 0,
.null_count = 0, .null_count = 0,
@ -168,7 +164,8 @@ void export_uint32_array(const uint32_t* data, int64_t nitems,
array->buffers[1] = data; array->buffers[1] = data;
} }
static void release_const_array(struct ArrowArray* array) { static void
release_const_array(struct ArrowArray *array) {
Imaging im = (Imaging)array->private_data; Imaging im = (Imaging)array->private_data;
ImagingDelete(im); ImagingDelete(im);
@ -180,8 +177,8 @@ static void release_const_array(struct ArrowArray* array) {
array->release = NULL; array->release = NULL;
} }
int
int export_single_channel_array(Imaging im, struct ArrowArray* array){ export_single_channel_array(Imaging im, struct ArrowArray *array) {
int length = im->xsize * im->ysize; int length = im->xsize * im->ysize;
/* undone -- for now, single block images */ /* undone -- for now, single block images */
@ -193,8 +190,7 @@ int export_single_channel_array(Imaging im, struct ArrowArray* array){
im->arrow_borrow++; im->arrow_borrow++;
// Initialize primitive fields // Initialize primitive fields
*array = (struct ArrowArray) { *array = (struct ArrowArray){// Data description
// Data description
.length = length, .length = length,
.offset = 0, .offset = 0,
.null_count = 0, .null_count = 0,
@ -220,9 +216,8 @@ int export_single_channel_array(Imaging im, struct ArrowArray* array){
return 0; return 0;
} }
int
int export_fixed_pixel_array(Imaging im, struct ArrowArray* array) { export_fixed_pixel_array(Imaging im, struct ArrowArray *array) {
int length = im->xsize * im->ysize; int length = im->xsize * im->ysize;
/* undone -- for now, single block images */ /* undone -- for now, single block images */
@ -236,8 +231,7 @@ int export_fixed_pixel_array(Imaging im, struct ArrowArray* array) {
// Initialize primitive fields // Initialize primitive fields
// Fixed length arrays are 1 buffer of validity, and the length in pixels. // Fixed length arrays are 1 buffer of validity, and the length in pixels.
// Data is in a child array. // Data is in a child array.
*array = (struct ArrowArray) { *array = (struct ArrowArray){// Data description
// Data description
.length = length, .length = length,
.offset = 0, .offset = 0,
.null_count = 0, .null_count = 0,
@ -255,15 +249,13 @@ int export_fixed_pixel_array(Imaging im, struct ArrowArray* array) {
// assert(array->buffers != NULL); // assert(array->buffers != NULL);
array->buffers[0] = NULL; // no nulls, null bitmap can be omitted array->buffers[0] = NULL; // no nulls, null bitmap can be omitted
// if it's not 1 band, it's an int32 at the moment. 4 unint8 bands. // if it's not 1 band, it's an int32 at the moment. 4 unint8 bands.
array->n_children = 1; array->n_children = 1;
array->children = calloc(1, sizeof(struct ArrowArray *)); array->children = calloc(1, sizeof(struct ArrowArray *));
array->children[0] = (struct ArrowArray *)calloc(1, sizeof(struct ArrowArray)); array->children[0] = (struct ArrowArray *)calloc(1, sizeof(struct ArrowArray));
im->arrow_borrow++; im->arrow_borrow++;
*array->children[0] = (struct ArrowArray) { *array->children[0] = (struct ArrowArray){// Data description
// Data description
.length = length * 4, .length = length * 4,
.offset = 0, .offset = 0,
.null_count = 0, .null_count = 0,
@ -276,7 +268,8 @@ int export_fixed_pixel_array(Imaging im, struct ArrowArray* array) {
.private_data = im .private_data = im
}; };
array->children[0]->buffers = (const void**) calloc(2, sizeof(void*) * array->n_buffers); array->children[0]->buffers =
(const void **)calloc(2, sizeof(void *) * array->n_buffers);
if (im->block) { if (im->block) {
array->children[0]->buffers[1] = im->block; array->children[0]->buffers[1] = im->block;
@ -286,8 +279,8 @@ int export_fixed_pixel_array(Imaging im, struct ArrowArray* array) {
return 0; return 0;
} }
int
int export_imaging_array(Imaging im, struct ArrowArray* array) { export_imaging_array(Imaging im, struct ArrowArray *array) {
if (strcmp(im->arrow_band_format, "") == 0) { if (strcmp(im->arrow_band_format, "") == 0) {
return 1; return 1;
} }

View File

@ -117,9 +117,6 @@ struct ImagingMemoryInstance {
int blocks_count; /* Number of blocks that have been allocated */ int blocks_count; /* Number of blocks that have been allocated */
int lines_per_block; /* Number of lines in a block have been allocated */ int lines_per_block; /* Number of lines in a block have been allocated */
}; };
#define IMAGING_PIXEL_1(im, x, y) ((im)->image8[(y)][(x)]) #define IMAGING_PIXEL_1(im, x, y) ((im)->image8[(y)][(x)])
@ -204,9 +201,13 @@ extern Imaging
ImagingNewBlock(const char *mode, int xsize, int ysize); ImagingNewBlock(const char *mode, int xsize, int ysize);
extern Imaging extern Imaging
ImagingNewArrow(const char *mode, int xsize, int ysize, ImagingNewArrow(
const char *mode,
int xsize,
int ysize,
struct ArrowSchema *schema, struct ArrowSchema *schema,
struct ArrowArray *external_array); struct ArrowArray *external_array
);
extern Imaging extern Imaging
ImagingNewPrologue(const char *mode, int xsize, int ysize); ImagingNewPrologue(const char *mode, int xsize, int ysize);
@ -725,9 +726,12 @@ _imaging_tell_pyFd(PyObject *fd);
/* Arrow */ /* Arrow */
extern int export_imaging_array(Imaging im, struct ArrowArray* array); extern int
extern int export_imaging_schema(Imaging im, struct ArrowSchema* schema); export_imaging_array(Imaging im, struct ArrowArray *array);
extern void export_uint32_type(struct ArrowSchema* schema); extern int
export_imaging_schema(Imaging im, struct ArrowSchema *schema);
extern void
export_uint32_type(struct ArrowSchema *schema);
/* Errcodes */ /* Errcodes */
#define IMAGING_CODEC_END 1 #define IMAGING_CODEC_END 1

View File

@ -561,7 +561,6 @@ ImagingAllocateBlock(Imaging im) {
/* --------------------------- */ /* --------------------------- */
/* Don't allocate the image. */ /* Don't allocate the image. */
static void static void
ImagingDestroyArrow(Imaging im) { ImagingDestroyArrow(Imaging im) {
if (im->arrow_array_capsule && im->arrow_array_capsule->release) { if (im->arrow_array_capsule && im->arrow_array_capsule->release) {
@ -685,9 +684,13 @@ ImagingNewBlock(const char *mode, int xsize, int ysize) {
} }
Imaging Imaging
ImagingNewArrow(const char *mode, int xsize, int ysize, ImagingNewArrow(
const char *mode,
int xsize,
int ysize,
struct ArrowSchema *schema, struct ArrowSchema *schema,
struct ArrowArray *external_array) { struct ArrowArray *external_array
) {
/* A borrowed arrow array */ /* A borrowed arrow array */
Imaging im; Imaging im;
@ -702,25 +705,19 @@ ImagingNewArrow(const char *mode, int xsize, int ysize,
int64_t pixels = (int64_t)xsize * (int64_t)ysize; int64_t pixels = (int64_t)xsize * (int64_t)ysize;
if (((strcmp(schema->format, "i") == 0 && if (((strcmp(schema->format, "i") == 0 && im->pixelsize == 4) ||
im->pixelsize == 4) || (strcmp(schema->format, im->arrow_band_format) == 0 && im->bands == 1)) &&
(strcmp(schema->format, im->arrow_band_format) == 0 && pixels == external_array->length) {
im->bands == 1))
&& pixels == external_array->length) {
// one arrow element per, and it matches a pixelsize*char // one arrow element per, and it matches a pixelsize*char
if (ImagingAllocateArrow(im, external_array)) { if (ImagingAllocateArrow(im, external_array)) {
return im; return im;
} }
} }
if (strcmp(schema->format, "+w:4") == 0 if (strcmp(schema->format, "+w:4") == 0 && im->pixelsize == 4 &&
&& im->pixelsize == 4 schema->n_children > 0 && schema->children &&
&& schema->n_children > 0 strcmp(schema->children[0]->format, "C") == 0 &&
&& schema->children pixels == external_array->length && external_array->n_children == 1 &&
&& strcmp(schema->children[0]->format, "C") == 0 external_array->children && 4 * pixels == external_array->children[0]->length) {
&& pixels == external_array->length
&& external_array->n_children == 1
&& external_array->children
&& 4 * pixels == external_array->children[0]->length) {
// 4 up element of char into pixelsize == 4 // 4 up element of char into pixelsize == 4
if (ImagingAllocateArrow(im, external_array)) { if (ImagingAllocateArrow(im, external_array)) {
return im; return im;