[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] 2024-08-25 15:15:46 +00:00
parent 309c5e261a
commit 12c5ad070b
8 changed files with 364 additions and 370 deletions

View File

@ -1,19 +1,16 @@
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 from .helper import assert_deep_equal, hopper
from typing import Any # undone
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 +27,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",
( (
@ -69,34 +63,34 @@ 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,11 +61,11 @@ optional-dependencies.tests = [
"markdown2", "markdown2",
"olefile", "olefile",
"packaging", "packaging",
"pyarrow",
"pyroma", "pyroma",
"pytest", "pytest",
"pytest-cov", "pytest-cov",
"pytest-timeout", "pytest-timeout",
"pyarrow",
] ]
optional-dependencies.typing = [ optional-dependencies.typing = [
"typing-extensions; python_version<'3.10'", "typing-extensions; python_version<'3.10'",

View File

@ -744,12 +744,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__())

View File

@ -228,7 +228,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) {
@ -237,14 +238,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) {
@ -253,14 +256,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);
} }
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* 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

@ -713,9 +713,12 @@ _imaging_tell_pyFd(PyObject *fd);
/* Arrow */ /* Arrow */
#include "Arrow.h" #include "Arrow.h"
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