mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-12 02:06:18 +03:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
309c5e261a
commit
12c5ad070b
|
@ -1,19 +1,16 @@
|
|||
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
|
||||
|
||||
from typing import Any # undone
|
||||
from .helper import assert_deep_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 +27,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",
|
||||
(
|
||||
|
@ -69,34 +63,34 @@ 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()
|
||||
|
|
|
@ -61,11 +61,11 @@ optional-dependencies.tests = [
|
|||
"markdown2",
|
||||
"olefile",
|
||||
"packaging",
|
||||
"pyarrow",
|
||||
"pyroma",
|
||||
"pytest",
|
||||
"pytest-cov",
|
||||
"pytest-timeout",
|
||||
"pyarrow",
|
||||
]
|
||||
optional-dependencies.typing = [
|
||||
"typing-extensions; python_version<'3.10'",
|
||||
|
|
|
@ -744,12 +744,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__())
|
||||
|
||||
|
|
|
@ -228,7 +228,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) {
|
||||
|
@ -237,14 +238,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) {
|
||||
|
@ -253,14 +256,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);
|
||||
}
|
||||
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/* EXCEPTION REROUTING */
|
||||
/* -------------------------------------------------------------------- */
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -713,9 +713,12 @@ _imaging_tell_pyFd(PyObject *fd);
|
|||
/* Arrow */
|
||||
|
||||
#include "Arrow.h"
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user