Skip to content

Commit 5b06597

Browse files
committed
Use fixture to re-open image for each test
1 parent d59b169 commit 5b06597

1 file changed

Lines changed: 37 additions & 20 deletions

File tree

Tests/test_file_jpeg2k.py

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import re
5+
from collections.abc import Generator
56
from io import BytesIO
67
from pathlib import Path
78
from typing import Any
@@ -29,8 +30,16 @@
2930

3031
pytestmark = skip_unless_feature("jpg_2000")
3132

32-
test_card = Image.open("Tests/images/test-card.png")
33-
test_card.load()
33+
34+
@pytest.fixture
35+
def test_card() -> Generator[ImageFile.ImageFile, None, None]:
36+
with Image.open("Tests/images/test-card.png") as im:
37+
im.load()
38+
try:
39+
yield im
40+
finally:
41+
im.close()
42+
3443

3544
# OpenJPEG 2.0.0 outputs this debugging message sometimes; we should
3645
# ignore it---it doesn't represent a test failure.
@@ -74,7 +83,7 @@ def test_invalid_file() -> None:
7483
Jpeg2KImagePlugin.Jpeg2KImageFile(invalid_file)
7584

7685

77-
def test_bytesio() -> None:
86+
def test_bytesio(test_card: ImageFile.ImageFile) -> None:
7887
with open("Tests/images/test-card-lossless.jp2", "rb") as f:
7988
data = BytesIO(f.read())
8089
with Image.open(data) as im:
@@ -86,62 +95,64 @@ def test_bytesio() -> None:
8695
# PIL (they were made using Adobe Photoshop)
8796

8897

89-
def test_lossless(tmp_path: Path) -> None:
98+
def test_lossless(test_card: ImageFile.ImageFile, tmp_path: Path) -> None:
9099
with Image.open("Tests/images/test-card-lossless.jp2") as im:
91100
im.load()
92101
outfile = str(tmp_path / "temp_test-card.png")
93102
im.save(outfile)
94103
assert_image_similar(im, test_card, 1.0e-3)
95104

96105

97-
def test_lossy_tiled() -> None:
106+
def test_lossy_tiled(test_card: ImageFile.ImageFile) -> None:
98107
assert_image_similar_tofile(
99108
test_card, "Tests/images/test-card-lossy-tiled.jp2", 2.0
100109
)
101110

102111

103-
def test_lossless_rt() -> None:
112+
def test_lossless_rt(test_card: ImageFile.ImageFile) -> None:
104113
im = roundtrip(test_card)
105114
assert_image_equal(im, test_card)
106115

107116

108-
def test_lossy_rt() -> None:
117+
def test_lossy_rt(test_card: ImageFile.ImageFile) -> None:
109118
im = roundtrip(test_card, quality_layers=[20])
110119
assert_image_similar(im, test_card, 2.0)
111120

112121

113-
def test_tiled_rt() -> None:
122+
def test_tiled_rt(test_card: ImageFile.ImageFile) -> None:
114123
im = roundtrip(test_card, tile_size=(128, 128))
115124
assert_image_equal(im, test_card)
116125

117126

118-
def test_tiled_offset_rt() -> None:
127+
def test_tiled_offset_rt(test_card: ImageFile.ImageFile) -> None:
119128
im = roundtrip(test_card, tile_size=(128, 128), tile_offset=(0, 0), offset=(32, 32))
120129
assert_image_equal(im, test_card)
121130

122131

123-
def test_tiled_offset_too_small() -> None:
132+
def test_tiled_offset_too_small(test_card: ImageFile.ImageFile) -> None:
124133
with pytest.raises(ValueError):
125134
roundtrip(test_card, tile_size=(128, 128), tile_offset=(0, 0), offset=(128, 32))
126135

127136

128-
def test_irreversible_rt() -> None:
137+
def test_irreversible_rt(test_card: ImageFile.ImageFile) -> None:
129138
im = roundtrip(test_card, irreversible=True, quality_layers=[20])
130139
assert_image_similar(im, test_card, 2.0)
131140

132141

133-
def test_prog_qual_rt() -> None:
142+
def test_prog_qual_rt(test_card: ImageFile.ImageFile) -> None:
134143
im = roundtrip(test_card, quality_layers=[60, 40, 20], progression="LRCP")
135144
assert_image_similar(im, test_card, 2.0)
136145

137146

138-
def test_prog_res_rt() -> None:
147+
def test_prog_res_rt(test_card: ImageFile.ImageFile) -> None:
139148
im = roundtrip(test_card, num_resolutions=8, progression="RLCP")
140149
assert_image_equal(im, test_card)
141150

142151

143152
@pytest.mark.parametrize("num_resolutions", range(2, 6))
144-
def test_default_num_resolutions(num_resolutions: int) -> None:
153+
def test_default_num_resolutions(
154+
test_card: ImageFile.ImageFile, num_resolutions: int
155+
) -> None:
145156
d = 1 << (num_resolutions - 1)
146157
im = test_card.resize((d - 1, d - 1))
147158
with pytest.raises(OSError):
@@ -205,7 +216,7 @@ def test_header_errors() -> None:
205216
pass
206217

207218

208-
def test_layers_type(tmp_path: Path) -> None:
219+
def test_layers_type(test_card: ImageFile.ImageFile, tmp_path: Path) -> None:
209220
outfile = str(tmp_path / "temp_layers.jp2")
210221
for quality_layers in [[100, 50, 10], (100, 50, 10), None]:
211222
test_card.save(outfile, quality_layers=quality_layers)
@@ -215,7 +226,7 @@ def test_layers_type(tmp_path: Path) -> None:
215226
test_card.save(outfile, quality_layers=quality_layers_str)
216227

217228

218-
def test_layers() -> None:
229+
def test_layers(test_card: ImageFile.ImageFile) -> None:
219230
out = BytesIO()
220231
test_card.save(out, "JPEG2000", quality_layers=[100, 50, 10], progression="LRCP")
221232
out.seek(0)
@@ -245,7 +256,13 @@ def test_layers() -> None:
245256
(None, {"no_jp2": False}, 4, b"jP"),
246257
),
247258
)
248-
def test_no_jp2(name: str, args: dict[str, bool], offset: int, data: bytes) -> None:
259+
def test_no_jp2(
260+
test_card: ImageFile.ImageFile,
261+
name: str,
262+
args: dict[str, bool],
263+
offset: int,
264+
data: bytes,
265+
) -> None:
249266
out = BytesIO()
250267
if name:
251268
out.name = name
@@ -254,7 +271,7 @@ def test_no_jp2(name: str, args: dict[str, bool], offset: int, data: bytes) -> N
254271
assert out.read(2) == data
255272

256273

257-
def test_mct() -> None:
274+
def test_mct(test_card: ImageFile.ImageFile) -> None:
258275
# Three component
259276
for val in (0, 1):
260277
out = BytesIO()
@@ -419,7 +436,7 @@ def test_comment() -> None:
419436
pass
420437

421438

422-
def test_save_comment() -> None:
439+
def test_save_comment(test_card: ImageFile.ImageFile) -> None:
423440
for comment in ("Created by Pillow", b"Created by Pillow"):
424441
out = BytesIO()
425442
test_card.save(out, "JPEG2000", comment=comment)
@@ -457,7 +474,7 @@ def test_crashes(test_file: str) -> None:
457474

458475

459476
@skip_unless_feature_version("jpg_2000", "2.4.0")
460-
def test_plt_marker() -> None:
477+
def test_plt_marker(test_card: ImageFile.ImageFile) -> None:
461478
# Search the start of the codesteam for PLT
462479
out = BytesIO()
463480
test_card.save(out, "JPEG2000", no_jp2=True, plt=True)

0 commit comments

Comments
 (0)