File: test_arrow.py

package info (click to toggle)
pillow 12.1.1-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 72,624 kB
  • sloc: python: 49,768; ansic: 38,750; makefile: 302; sh: 169; javascript: 85
file content (164 lines) | stat: -rw-r--r-- 4,428 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from __future__ import annotations

import pytest

from PIL import Image

from .helper import hopper


@pytest.mark.parametrize(
    "mode, dest_modes",
    (
        ("L", ["I", "F", "LA", "RGB", "RGBA", "RGBX", "CMYK", "YCbCr", "HSV"]),
        ("I", ["L", "F"]),  # Technically I;32 can work for any 4x8bit storage.
        ("F", ["I", "L", "LA", "RGB", "RGBA", "RGBX", "CMYK", "YCbCr", "HSV"]),
        ("LA", ["L", "F"]),
        ("RGB", ["L", "F"]),
        ("RGBA", ["L", "F"]),
        ("RGBX", ["L", "F"]),
        ("CMYK", ["L", "F"]),
        ("YCbCr", ["L", "F"]),
        ("HSV", ["L", "F"]),
    ),
)
def test_invalid_array_type(mode: str, dest_modes: list[str]) -> None:
    img = hopper(mode)
    for dest_mode in dest_modes:
        with pytest.raises(ValueError):
            Image.fromarrow(img, dest_mode, img.size)


def test_invalid_array_size() -> None:
    img = hopper("RGB")

    assert img.size != (10, 10)
    with pytest.raises(ValueError):
        Image.fromarrow(img, "RGB", (10, 10))


def test_release_schema() -> None:
    # these should not error out, valgrind should be clean
    img = hopper("L")
    schema = img.__arrow_c_schema__()
    del schema


def test_release_array() -> None:
    # these should not error out, valgrind should be clean
    img = hopper("L")
    array, schema = img.__arrow_c_array__()
    del array
    del schema


def test_readonly() -> None:
    img = hopper("L")
    reloaded = Image.fromarrow(img, img.mode, img.size)
    assert reloaded.readonly == 1
    reloaded._readonly = 0
    assert reloaded.readonly == 1


def test_multiblock_l_image() -> None:
    block_size = Image.core.get_block_size()

    # check a 2 block image in single channel mode
    size = (4096, 2 * block_size // 4096)
    img = Image.new("L", size, 128)

    with pytest.raises(ValueError):
        (schema, arr) = img.__arrow_c_array__()


def test_multiblock_rgba_image() -> None:
    block_size = Image.core.get_block_size()

    # check a 2 block image in 4 channel mode
    size = (4096, (block_size // 4096) // 2)
    img = Image.new("RGBA", size, (128, 127, 126, 125))

    with pytest.raises(ValueError):
        (schema, arr) = img.__arrow_c_array__()


def test_multiblock_l_schema() -> None:
    block_size = Image.core.get_block_size()

    # check a 2 block image in single channel mode
    size = (4096, 2 * block_size // 4096)
    img = Image.new("L", size, 128)

    with pytest.raises(ValueError):
        img.__arrow_c_schema__()


def test_multiblock_rgba_schema() -> None:
    block_size = Image.core.get_block_size()

    # check a 2 block image in 4 channel mode
    size = (4096, (block_size // 4096) // 2)
    img = Image.new("RGBA", size, (128, 127, 126, 125))

    with pytest.raises(ValueError):
        img.__arrow_c_schema__()


def test_singleblock_l_image() -> None:
    Image.core.set_use_block_allocator(1)

    block_size = Image.core.get_block_size()

    # check a 2 block image in 4 channel mode
    size = (4096, 2 * (block_size // 4096))
    img = Image.new("L", size, 128)
    assert img.im.isblock()

    (schema, arr) = img.__arrow_c_array__()
    assert schema
    assert arr

    Image.core.set_use_block_allocator(0)


def test_singleblock_rgba_image() -> None:
    Image.core.set_use_block_allocator(1)
    block_size = Image.core.get_block_size()

    # check a 2 block image in 4 channel mode
    size = (4096, (block_size // 4096) // 2)
    img = Image.new("RGBA", size, (128, 127, 126, 125))
    assert img.im.isblock()

    (schema, arr) = img.__arrow_c_array__()
    assert schema
    assert arr
    Image.core.set_use_block_allocator(0)


def test_singleblock_l_schema() -> None:
    Image.core.set_use_block_allocator(1)
    block_size = Image.core.get_block_size()

    # check a 2 block image in single channel mode
    size = (4096, 2 * block_size // 4096)
    img = Image.new("L", size, 128)
    assert img.im.isblock()

    schema = img.__arrow_c_schema__()
    assert schema
    Image.core.set_use_block_allocator(0)


def test_singleblock_rgba_schema() -> None:
    Image.core.set_use_block_allocator(1)
    block_size = Image.core.get_block_size()

    # check a 2 block image in 4 channel mode
    size = (4096, (block_size // 4096) // 2)
    img = Image.new("RGBA", size, (128, 127, 126, 125))
    assert img.im.isblock()

    schema = img.__arrow_c_schema__()
    assert schema
    Image.core.set_use_block_allocator(0)