[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
import warnings
from typing import Any # undone
import pytest
from PIL import Image
from .helper import assert_deep_equal, assert_image, hopper, skip_unless_feature, assert_image_equal
from typing import Any # undone
from .helper import (
assert_deep_equal,
assert_image_equal,
hopper,
)
pyarrow = pytest.importorskip("pyarrow", reason="PyArrow not installed")
TEST_IMAGE_SIZE = (10, 10)
from numbers import Number
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
fl_uint8_4_type = pyarrow.field("_",
pyarrow.list_(
pyarrow.field("_",
pyarrow.uint8()
).with_nullable(False)
,4)
fl_uint8_4_type = pyarrow.field(
"_", pyarrow.list_(pyarrow.field("_", pyarrow.uint8()).with_nullable(False), 4)
).type
@pytest.mark.parametrize(
"mode, dtype, mask",
(
@ -70,38 +68,39 @@ def test_to_array(mode: str, dtype: Any, mask: Any ) -> None:
assert_image_equal(img, reloaded)
def test_lifetime():
# valgrind shouldn't error out here.
# arrays should be accessible after the image is deleted.
img = hopper('L')
img = hopper("L")
arr_1 = pyarrow.array(img)
arr_2 = pyarrow.array(img)
del(img)
del img
assert arr_1.sum().as_py() > 0
del(arr_1)
del arr_1
assert arr_2.sum().as_py() > 0
del(arr_2)
del arr_2
def test_lifetime2():
# valgrind shouldn't error out here.
# img should remain after the arrays are collected.
img = hopper('L')
img = hopper("L")
arr_1 = pyarrow.array(img)
arr_2 = pyarrow.array(img)
assert arr_1.sum().as_py() > 0
del(arr_1)
del arr_1
assert arr_2.sum().as_py() > 0
del(arr_2)
del arr_2
img2 = img.copy()
px = img2.load()

View File

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

View File

@ -748,12 +748,13 @@ class Image:
new["shape"], new["typestr"] = _conv_type_shape(self)
return new
def __arrow_c_schema__(self) -> object:
self.load()
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()
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]:
raise NotImplementedError()
class SupportsArrowArrayInterface(Protocol):
"""
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()
@ -3356,12 +3360,12 @@ def fromarray(obj: SupportsArrayInterface, mode: str | None = None) -> Image:
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")
(schema_capsule, array_capsule) = obj.__arrow_c_array__()
_im = core.new_arrow(mode, size, schema_capsule, array_capsule)
if (_im):
if _im:
return Image()._new(_im)
return None

View File

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

View File

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

View File

@ -117,9 +117,6 @@ struct ImagingMemoryInstance {
int blocks_count; /* Number of blocks that 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)])
@ -204,9 +201,13 @@ extern Imaging
ImagingNewBlock(const char *mode, int xsize, int ysize);
extern Imaging
ImagingNewArrow(const char *mode, int xsize, int ysize,
ImagingNewArrow(
const char *mode,
int xsize,
int ysize,
struct ArrowSchema *schema,
struct ArrowArray *external_array);
struct ArrowArray *external_array
);
extern Imaging
ImagingNewPrologue(const char *mode, int xsize, int ysize);
@ -725,9 +726,12 @@ _imaging_tell_pyFd(PyObject *fd);
/* Arrow */
extern int export_imaging_array(Imaging im, struct ArrowArray* array);
extern int export_imaging_schema(Imaging im, struct ArrowSchema* schema);
extern void export_uint32_type(struct ArrowSchema* schema);
extern int
export_imaging_array(Imaging im, struct ArrowArray *array);
extern int
export_imaging_schema(Imaging im, struct ArrowSchema *schema);
extern void
export_uint32_type(struct ArrowSchema *schema);
/* Errcodes */
#define IMAGING_CODEC_END 1

View File

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