Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Aug 25, 2024
1 parent b5a261e commit ceb14be
Show file tree
Hide file tree
Showing 8 changed files with 384 additions and 390 deletions.
48 changes: 21 additions & 27 deletions Tests/test_arrow.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -24,36 +21,33 @@ def _test_img_equals_pyarray(img: Image.Image, arr: Any, mask) -> None:
for y in range(0, img.size[1], int(img.size[1] / 10)):
if mask:
for ix, elt in enumerate(mask):
assert px[x,y][ix] == arr[y * img.width + x].as_py()[elt]
assert px[x, y][ix] == arr[y * img.width + x].as_py()[elt]
else:
assert_deep_equal(px[x, y], arr[y * img.width + x].as_py())


# 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)
).type
fl_uint8_4_type = pyarrow.field(
"_", pyarrow.list_(pyarrow.field("_", pyarrow.uint8()).with_nullable(False), 4)
).type


@pytest.mark.parametrize(
"mode, dtype, mask",
(
("L", pyarrow.uint8(), None),
("I", pyarrow.int32(), None),
("F", pyarrow.float32(), None),
("LA", fl_uint8_4_type, [0,3]),
("RGB", fl_uint8_4_type, [0,1,2]),
("LA", fl_uint8_4_type, [0, 3]),
("RGB", fl_uint8_4_type, [0, 1, 2]),
("RGBA", fl_uint8_4_type, None),
("RGBX", fl_uint8_4_type, None),
("CMYK", fl_uint8_4_type, None),
("YCbCr", fl_uint8_4_type, [0,1,2]),
("HSV", fl_uint8_4_type, [0,1,2]),
("YCbCr", fl_uint8_4_type, [0, 1, 2]),
("HSV", fl_uint8_4_type, [0, 1, 2]),
),
)
def test_to_array(mode: str, dtype: Any, mask: Any ) -> None:
def test_to_array(mode: str, dtype: Any, mask: Any) -> None:
img = hopper(mode)

# Resize to non-square
Expand All @@ -69,35 +63,35 @@ 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()
assert isinstance(px[0,0], int)
assert isinstance(px[0, 0], int)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
Expand Down
5 changes: 3 additions & 2 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,12 +744,13 @@ def __array_interface__(self) -> dict[str, str | bytes | int | tuple[int, ...]]:
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__())

Expand Down
29 changes: 16 additions & 13 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,39 +228,42 @@ PyImaging_GetBuffer(PyObject *buffer, Py_buffer *view) {
/* Arrow HANDLING */
/* -------------------------------------------------------------------- */

void ReleaseArrowSchemaPyCapsule(PyObject* capsule) {
struct ArrowSchema* schema =
(struct ArrowSchema*)PyCapsule_GetPointer(capsule, "arrow_schema");
void
ReleaseArrowSchemaPyCapsule(PyObject *capsule) {
struct ArrowSchema *schema =
(struct ArrowSchema *)PyCapsule_GetPointer(capsule, "arrow_schema");
if (schema->release != NULL) {
schema->release(schema);
}
free(schema);
}

PyObject* ExportArrowSchemaPyCapsule(ImagingObject *self) {
struct ArrowSchema* schema =
(struct ArrowSchema*)calloc(1, sizeof(struct ArrowSchema));
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) {
struct ArrowArray* array =
(struct ArrowArray*)PyCapsule_GetPointer(capsule, "arrow_array");
void
ReleaseArrowArrayPyCapsule(PyObject *capsule) {
struct ArrowArray *array =
(struct ArrowArray *)PyCapsule_GetPointer(capsule, "arrow_array");
if (array->release != NULL) {
array->release(array);
}
free(array);
}

PyObject* ExportArrowArrayPyCapsule(ImagingObject *self) {
struct ArrowArray* array =
(struct ArrowArray*)calloc(1, sizeof(struct ArrowArray));
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 */
/* -------------------------------------------------------------------- */
Expand Down
Loading

0 comments on commit ceb14be

Please sign in to comment.