Skip to content

Commit ceb14be

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent b5a261e commit ceb14be

File tree

8 files changed

+384
-390
lines changed

8 files changed

+384
-390
lines changed

Tests/test_arrow.py

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
from __future__ import annotations
22

3-
import warnings
3+
from typing import Any # undone
44

55
import pytest
66

77
from PIL import Image
88

9-
from .helper import assert_deep_equal, assert_image, hopper, skip_unless_feature
10-
11-
from typing import Any # undone
9+
from .helper import assert_deep_equal, hopper
1210

1311
pyarrow = pytest.importorskip("pyarrow", reason="PyArrow not installed")
1412

1513
TEST_IMAGE_SIZE = (10, 10)
16-
from numbers import Number
1714

1815

1916
def _test_img_equals_pyarray(img: Image.Image, arr: Any, mask) -> None:
@@ -24,36 +21,33 @@ def _test_img_equals_pyarray(img: Image.Image, arr: Any, mask) -> None:
2421
for y in range(0, img.size[1], int(img.size[1] / 10)):
2522
if mask:
2623
for ix, elt in enumerate(mask):
27-
assert px[x,y][ix] == arr[y * img.width + x].as_py()[elt]
24+
assert px[x, y][ix] == arr[y * img.width + x].as_py()[elt]
2825
else:
2926
assert_deep_equal(px[x, y], arr[y * img.width + x].as_py())
3027

3128

3229
# really hard to get a non-nullable list type
33-
fl_uint8_4_type = pyarrow.field("_",
34-
pyarrow.list_(
35-
pyarrow.field("_",
36-
pyarrow.uint8()
37-
).with_nullable(False)
38-
,4)
39-
).type
30+
fl_uint8_4_type = pyarrow.field(
31+
"_", pyarrow.list_(pyarrow.field("_", pyarrow.uint8()).with_nullable(False), 4)
32+
).type
33+
4034

4135
@pytest.mark.parametrize(
4236
"mode, dtype, mask",
4337
(
4438
("L", pyarrow.uint8(), None),
4539
("I", pyarrow.int32(), None),
4640
("F", pyarrow.float32(), None),
47-
("LA", fl_uint8_4_type, [0,3]),
48-
("RGB", fl_uint8_4_type, [0,1,2]),
41+
("LA", fl_uint8_4_type, [0, 3]),
42+
("RGB", fl_uint8_4_type, [0, 1, 2]),
4943
("RGBA", fl_uint8_4_type, None),
5044
("RGBX", fl_uint8_4_type, None),
5145
("CMYK", fl_uint8_4_type, None),
52-
("YCbCr", fl_uint8_4_type, [0,1,2]),
53-
("HSV", fl_uint8_4_type, [0,1,2]),
46+
("YCbCr", fl_uint8_4_type, [0, 1, 2]),
47+
("HSV", fl_uint8_4_type, [0, 1, 2]),
5448
),
5549
)
56-
def test_to_array(mode: str, dtype: Any, mask: Any ) -> None:
50+
def test_to_array(mode: str, dtype: Any, mask: Any) -> None:
5751
img = hopper(mode)
5852

5953
# Resize to non-square
@@ -69,35 +63,35 @@ def test_lifetime():
6963
# valgrind shouldn't error out here.
7064
# arrays should be accessible after the image is deleted.
7165

72-
img = hopper('L')
66+
img = hopper("L")
7367

7468
arr_1 = pyarrow.array(img)
7569
arr_2 = pyarrow.array(img)
7670

77-
del(img)
71+
del img
7872

7973
assert arr_1.sum().as_py() > 0
80-
del(arr_1)
74+
del arr_1
8175

8276
assert arr_2.sum().as_py() > 0
83-
del(arr_2)
77+
del arr_2
78+
8479

8580
def test_lifetime2():
8681
# valgrind shouldn't error out here.
8782
# img should remain after the arrays are collected.
8883

89-
img = hopper('L')
84+
img = hopper("L")
9085

9186
arr_1 = pyarrow.array(img)
9287
arr_2 = pyarrow.array(img)
9388

94-
9589
assert arr_1.sum().as_py() > 0
96-
del(arr_1)
90+
del arr_1
9791

9892
assert arr_2.sum().as_py() > 0
99-
del(arr_2)
93+
del arr_2
10094

10195
img2 = img.copy()
10296
px = img2.load()
103-
assert isinstance(px[0,0], int)
97+
assert isinstance(px[0, 0], int)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ optional-dependencies.tests = [
6161
"markdown2",
6262
"olefile",
6363
"packaging",
64+
"pyarrow",
6465
"pyroma",
6566
"pytest",
6667
"pytest-cov",
6768
"pytest-timeout",
68-
"pyarrow",
6969
]
7070
optional-dependencies.typing = [
7171
"typing-extensions; python_version<'3.10'",

src/PIL/Image.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,12 +744,13 @@ def __array_interface__(self) -> dict[str, str | bytes | int | tuple[int, ...]]:
744744
new["shape"], new["typestr"] = _conv_type_shape(self)
745745
return new
746746

747-
748747
def __arrow_c_schema__(self) -> object:
749748
self.load()
750749
return self.im.__arrow_c_schema__()
751750

752-
def __arrow_c_array__(self, requested_schema: object | None = None) -> Tuple[object, object]:
751+
def __arrow_c_array__(
752+
self, requested_schema: object | None = None
753+
) -> Tuple[object, object]:
753754
self.load()
754755
return (self.im.__arrow_c_schema__(), self.im.__arrow_c_array__())
755756

src/_imaging.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -228,39 +228,42 @@ PyImaging_GetBuffer(PyObject *buffer, Py_buffer *view) {
228228
/* Arrow HANDLING */
229229
/* -------------------------------------------------------------------- */
230230

231-
void ReleaseArrowSchemaPyCapsule(PyObject* capsule) {
232-
struct ArrowSchema* schema =
233-
(struct ArrowSchema*)PyCapsule_GetPointer(capsule, "arrow_schema");
231+
void
232+
ReleaseArrowSchemaPyCapsule(PyObject *capsule) {
233+
struct ArrowSchema *schema =
234+
(struct ArrowSchema *)PyCapsule_GetPointer(capsule, "arrow_schema");
234235
if (schema->release != NULL) {
235236
schema->release(schema);
236237
}
237238
free(schema);
238239
}
239240

240-
PyObject* ExportArrowSchemaPyCapsule(ImagingObject *self) {
241-
struct ArrowSchema* schema =
242-
(struct ArrowSchema*)calloc(1, sizeof(struct ArrowSchema));
241+
PyObject *
242+
ExportArrowSchemaPyCapsule(ImagingObject *self) {
243+
struct ArrowSchema *schema =
244+
(struct ArrowSchema *)calloc(1, sizeof(struct ArrowSchema));
243245
export_imaging_schema(self->image, schema);
244246
return PyCapsule_New(schema, "arrow_schema", ReleaseArrowSchemaPyCapsule);
245247
}
246248

247-
void ReleaseArrowArrayPyCapsule(PyObject* capsule) {
248-
struct ArrowArray* array =
249-
(struct ArrowArray*)PyCapsule_GetPointer(capsule, "arrow_array");
249+
void
250+
ReleaseArrowArrayPyCapsule(PyObject *capsule) {
251+
struct ArrowArray *array =
252+
(struct ArrowArray *)PyCapsule_GetPointer(capsule, "arrow_array");
250253
if (array->release != NULL) {
251254
array->release(array);
252255
}
253256
free(array);
254257
}
255258

256-
PyObject* ExportArrowArrayPyCapsule(ImagingObject *self) {
257-
struct ArrowArray* array =
258-
(struct ArrowArray*)calloc(1, sizeof(struct ArrowArray));
259+
PyObject *
260+
ExportArrowArrayPyCapsule(ImagingObject *self) {
261+
struct ArrowArray *array =
262+
(struct ArrowArray *)calloc(1, sizeof(struct ArrowArray));
259263
export_imaging_array(self->image, array);
260264
return PyCapsule_New(array, "arrow_array", ReleaseArrowArrayPyCapsule);
261265
}
262266

263-
264267
/* -------------------------------------------------------------------- */
265268
/* EXCEPTION REROUTING */
266269
/* -------------------------------------------------------------------- */

0 commit comments

Comments
 (0)