Skip to content

Commit eee633c

Browse files
authored
Merge pull request #7975 from radarhere/libtiff
Corrected check for libtiff feature
2 parents f8160b8 + 11ac0c1 commit eee633c

File tree

3 files changed

+87
-95
lines changed

3 files changed

+87
-95
lines changed

Tests/test_file_libtiff.py

Lines changed: 65 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ def test_write_metadata(self, legacy_api: bool, tmp_path: Path) -> None:
185185
assert field in reloaded, f"{field} not in metadata"
186186

187187
@pytest.mark.valgrind_known_error(reason="Known invalid metadata")
188-
def test_additional_metadata(self, tmp_path: Path) -> None:
188+
def test_additional_metadata(
189+
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
190+
) -> None:
189191
# these should not crash. Seriously dummy data, most of it doesn't make
190192
# any sense, so we're running up against limits where we're asking
191193
# libtiff to do stupid things.
@@ -236,12 +238,10 @@ def test_additional_metadata(self, tmp_path: Path) -> None:
236238
del new_ifd[338]
237239

238240
out = str(tmp_path / "temp.tif")
239-
TiffImagePlugin.WRITE_LIBTIFF = True
241+
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", True)
240242

241243
im.save(out, tiffinfo=new_ifd)
242244

243-
TiffImagePlugin.WRITE_LIBTIFF = False
244-
245245
def test_custom_metadata(self, tmp_path: Path) -> None:
246246
class Tc(NamedTuple):
247247
value: Any
@@ -343,24 +343,24 @@ def test_subifd(self, tmp_path: Path) -> None:
343343
# Should not segfault
344344
im.save(outfile)
345345

346-
def test_xmlpacket_tag(self, tmp_path: Path) -> None:
347-
TiffImagePlugin.WRITE_LIBTIFF = True
346+
def test_xmlpacket_tag(
347+
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
348+
) -> None:
349+
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", True)
348350

349351
out = str(tmp_path / "temp.tif")
350352
hopper().save(out, tiffinfo={700: b"xmlpacket tag"})
351-
TiffImagePlugin.WRITE_LIBTIFF = False
352353

353354
with Image.open(out) as reloaded:
354355
if 700 in reloaded.tag_v2:
355356
assert reloaded.tag_v2[700] == b"xmlpacket tag"
356357

357-
def test_int_dpi(self, tmp_path: Path) -> None:
358+
def test_int_dpi(self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
358359
# issue #1765
359360
im = hopper("RGB")
360361
out = str(tmp_path / "temp.tif")
361-
TiffImagePlugin.WRITE_LIBTIFF = True
362+
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", True)
362363
im.save(out, dpi=(72, 72))
363-
TiffImagePlugin.WRITE_LIBTIFF = False
364364
with Image.open(out) as reloaded:
365365
assert reloaded.info["dpi"] == (72.0, 72.0)
366366

@@ -422,13 +422,13 @@ def test_g4_string_info(self, tmp_path: Path) -> None:
422422
assert "temp.tif" == reread.tag_v2[269]
423423
assert "temp.tif" == reread.tag[269][0]
424424

425-
def test_12bit_rawmode(self) -> None:
425+
def test_12bit_rawmode(self, monkeypatch: pytest.MonkeyPatch) -> None:
426426
"""Are we generating the same interpretation
427427
of the image as Imagemagick is?"""
428-
TiffImagePlugin.READ_LIBTIFF = True
428+
monkeypatch.setattr(TiffImagePlugin, "READ_LIBTIFF", True)
429429
with Image.open("Tests/images/12bit.cropped.tif") as im:
430430
im.load()
431-
TiffImagePlugin.READ_LIBTIFF = False
431+
monkeypatch.setattr(TiffImagePlugin, "READ_LIBTIFF", False)
432432
# to make the target --
433433
# convert 12bit.cropped.tif -depth 16 tmp.tif
434434
# convert tmp.tif -evaluate RightShift 4 12in16bit2.tif
@@ -514,12 +514,13 @@ def test_cmyk_save(self, tmp_path: Path) -> None:
514514
assert_image_equal_tofile(im, out)
515515

516516
@pytest.mark.parametrize("im", (hopper("P"), Image.new("P", (1, 1), "#000")))
517-
def test_palette_save(self, im: Image.Image, tmp_path: Path) -> None:
517+
def test_palette_save(
518+
self, im: Image.Image, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
519+
) -> None:
518520
out = str(tmp_path / "temp.tif")
519521

520-
TiffImagePlugin.WRITE_LIBTIFF = True
522+
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", True)
521523
im.save(out)
522-
TiffImagePlugin.WRITE_LIBTIFF = False
523524

524525
with Image.open(out) as reloaded:
525526
# colormap/palette tag
@@ -548,9 +549,9 @@ def test_fp_leak(self) -> None:
548549
with pytest.raises(OSError):
549550
os.close(fn)
550551

551-
def test_multipage(self) -> None:
552+
def test_multipage(self, monkeypatch: pytest.MonkeyPatch) -> None:
552553
# issue #862
553-
TiffImagePlugin.READ_LIBTIFF = True
554+
monkeypatch.setattr(TiffImagePlugin, "READ_LIBTIFF", True)
554555
with Image.open("Tests/images/multipage.tiff") as im:
555556
# file is a multipage tiff, 10x10 green, 10x10 red, 20x20 blue
556557

@@ -569,11 +570,9 @@ def test_multipage(self) -> None:
569570
assert im.size == (20, 20)
570571
assert im.convert("RGB").getpixel((0, 0)) == (0, 0, 255)
571572

572-
TiffImagePlugin.READ_LIBTIFF = False
573-
574-
def test_multipage_nframes(self) -> None:
573+
def test_multipage_nframes(self, monkeypatch: pytest.MonkeyPatch) -> None:
575574
# issue #862
576-
TiffImagePlugin.READ_LIBTIFF = True
575+
monkeypatch.setattr(TiffImagePlugin, "READ_LIBTIFF", True)
577576
with Image.open("Tests/images/multipage.tiff") as im:
578577
frames = im.n_frames
579578
assert frames == 3
@@ -582,35 +581,30 @@ def test_multipage_nframes(self) -> None:
582581
# Should not raise ValueError: I/O operation on closed file
583582
im.load()
584583

585-
TiffImagePlugin.READ_LIBTIFF = False
586-
587-
def test_multipage_seek_backwards(self) -> None:
588-
TiffImagePlugin.READ_LIBTIFF = True
584+
def test_multipage_seek_backwards(self, monkeypatch: pytest.MonkeyPatch) -> None:
585+
monkeypatch.setattr(TiffImagePlugin, "READ_LIBTIFF", True)
589586
with Image.open("Tests/images/multipage.tiff") as im:
590587
im.seek(1)
591588
im.load()
592589

593590
im.seek(0)
594591
assert im.convert("RGB").getpixel((0, 0)) == (0, 128, 0)
595592

596-
TiffImagePlugin.READ_LIBTIFF = False
597-
598-
def test__next(self) -> None:
599-
TiffImagePlugin.READ_LIBTIFF = True
593+
def test__next(self, monkeypatch: pytest.MonkeyPatch) -> None:
594+
monkeypatch.setattr(TiffImagePlugin, "READ_LIBTIFF", True)
600595
with Image.open("Tests/images/hopper.tif") as im:
601596
assert not im.tag.next
602597
im.load()
603598
assert not im.tag.next
604599

605-
def test_4bit(self) -> None:
600+
def test_4bit(self, monkeypatch: pytest.MonkeyPatch) -> None:
606601
# Arrange
607602
test_file = "Tests/images/hopper_gray_4bpp.tif"
608603
original = hopper("L")
609604

610605
# Act
611-
TiffImagePlugin.READ_LIBTIFF = True
606+
monkeypatch.setattr(TiffImagePlugin, "READ_LIBTIFF", True)
612607
with Image.open(test_file) as im:
613-
TiffImagePlugin.READ_LIBTIFF = False
614608

615609
# Assert
616610
assert im.size == (128, 128)
@@ -650,12 +644,12 @@ def test_gray_semibyte_per_pixel(self) -> None:
650644
assert im2.mode == "L"
651645
assert_image_equal(im, im2)
652646

653-
def test_save_bytesio(self) -> None:
647+
def test_save_bytesio(self, monkeypatch: pytest.MonkeyPatch) -> None:
654648
# PR 1011
655649
# Test TIFF saving to io.BytesIO() object.
656650

657-
TiffImagePlugin.WRITE_LIBTIFF = True
658-
TiffImagePlugin.READ_LIBTIFF = True
651+
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", True)
652+
monkeypatch.setattr(TiffImagePlugin, "READ_LIBTIFF", True)
659653

660654
# Generate test image
661655
pilim = hopper()
@@ -672,9 +666,6 @@ def save_bytesio(compression: str | None = None) -> None:
672666
save_bytesio("packbits")
673667
save_bytesio("tiff_lzw")
674668

675-
TiffImagePlugin.WRITE_LIBTIFF = False
676-
TiffImagePlugin.READ_LIBTIFF = False
677-
678669
def test_save_ycbcr(self, tmp_path: Path) -> None:
679670
im = hopper("YCbCr")
680671
outfile = str(tmp_path / "temp.tif")
@@ -694,15 +685,16 @@ def test_exif_ifd(self, tmp_path: Path) -> None:
694685
if Image.core.libtiff_support_custom_tags:
695686
assert reloaded.tag_v2[34665] == 125456
696687

697-
def test_crashing_metadata(self, tmp_path: Path) -> None:
688+
def test_crashing_metadata(
689+
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
690+
) -> None:
698691
# issue 1597
699692
with Image.open("Tests/images/rdf.tif") as im:
700693
out = str(tmp_path / "temp.tif")
701694

702-
TiffImagePlugin.WRITE_LIBTIFF = True
695+
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", True)
703696
# this shouldn't crash
704697
im.save(out, format="TIFF")
705-
TiffImagePlugin.WRITE_LIBTIFF = False
706698

707699
def test_page_number_x_0(self, tmp_path: Path) -> None:
708700
# Issue 973
@@ -733,36 +725,41 @@ def test_fd_duplication(self, tmp_path: Path) -> None:
733725
# Should not raise PermissionError.
734726
os.remove(tmpfile)
735727

736-
def test_read_icc(self) -> None:
728+
def test_read_icc(self, monkeypatch: pytest.MonkeyPatch) -> None:
737729
with Image.open("Tests/images/hopper.iccprofile.tif") as img:
738730
icc = img.info.get("icc_profile")
739731
assert icc is not None
740-
TiffImagePlugin.READ_LIBTIFF = True
732+
monkeypatch.setattr(TiffImagePlugin, "READ_LIBTIFF", True)
741733
with Image.open("Tests/images/hopper.iccprofile.tif") as img:
742734
icc_libtiff = img.info.get("icc_profile")
743735
assert icc_libtiff is not None
744-
TiffImagePlugin.READ_LIBTIFF = False
745736
assert icc == icc_libtiff
746737

747-
def test_write_icc(self, tmp_path: Path) -> None:
748-
def check_write(libtiff: bool) -> None:
749-
TiffImagePlugin.WRITE_LIBTIFF = libtiff
750-
751-
with Image.open("Tests/images/hopper.iccprofile.tif") as img:
752-
icc_profile = img.info["icc_profile"]
753-
754-
out = str(tmp_path / "temp.tif")
755-
img.save(out, icc_profile=icc_profile)
756-
with Image.open(out) as reloaded:
757-
assert icc_profile == reloaded.info["icc_profile"]
738+
@pytest.mark.parametrize(
739+
"libtiff",
740+
(
741+
pytest.param(
742+
True,
743+
marks=pytest.mark.skipif(
744+
not Image.core.libtiff_support_custom_tags,
745+
reason="Custom tags not supported by older libtiff",
746+
),
747+
),
748+
False,
749+
),
750+
)
751+
def test_write_icc(
752+
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path, libtiff: bool
753+
) -> None:
754+
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", libtiff)
758755

759-
libtiffs = []
760-
if Image.core.libtiff_support_custom_tags:
761-
libtiffs.append(True)
762-
libtiffs.append(False)
756+
with Image.open("Tests/images/hopper.iccprofile.tif") as img:
757+
icc_profile = img.info["icc_profile"]
763758

764-
for libtiff in libtiffs:
765-
check_write(libtiff)
759+
out = str(tmp_path / "temp.tif")
760+
img.save(out, icc_profile=icc_profile)
761+
with Image.open(out) as reloaded:
762+
assert icc_profile == reloaded.info["icc_profile"]
766763

767764
def test_multipage_compression(self) -> None:
768765
with Image.open("Tests/images/compression.tif") as im:
@@ -840,12 +837,13 @@ def test_sampleformat(self) -> None:
840837

841838
assert_image_equal_tofile(im, "Tests/images/copyleft.png", mode="RGB")
842839

843-
def test_sampleformat_write(self, tmp_path: Path) -> None:
840+
def test_sampleformat_write(
841+
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
842+
) -> None:
844843
im = Image.new("F", (1, 1))
845844
out = str(tmp_path / "temp.tif")
846-
TiffImagePlugin.WRITE_LIBTIFF = True
845+
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", True)
847846
im.save(out)
848-
TiffImagePlugin.WRITE_LIBTIFF = False
849847

850848
with Image.open(out) as reloaded:
851849
assert reloaded.mode == "F"
@@ -1091,15 +1089,14 @@ def test_sampleformat_not_corrupted(self) -> None:
10911089
with Image.open(out) as im:
10921090
im.load()
10931091

1094-
def test_realloc_overflow(self) -> None:
1095-
TiffImagePlugin.READ_LIBTIFF = True
1092+
def test_realloc_overflow(self, monkeypatch: pytest.MonkeyPatch) -> None:
1093+
monkeypatch.setattr(TiffImagePlugin, "READ_LIBTIFF", True)
10961094
with Image.open("Tests/images/tiff_overflow_rows_per_strip.tif") as im:
10971095
with pytest.raises(OSError) as e:
10981096
im.load()
10991097

11001098
# Assert that the error code is IMAGING_CODEC_MEMORY
11011099
assert str(e.value) == "-9"
1102-
TiffImagePlugin.READ_LIBTIFF = False
11031100

11041101
@pytest.mark.parametrize("compression", ("tiff_adobe_deflate", "jpeg"))
11051102
def test_save_multistrip(self, compression: str, tmp_path: Path) -> None:

Tests/test_imagesequence.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,18 @@ def test_iterator_min_frame() -> None:
4747
assert i[index] == next(i)
4848

4949

50-
def _test_multipage_tiff() -> None:
50+
@pytest.mark.parametrize(
51+
"libtiff", (pytest.param(True, marks=skip_unless_feature("libtiff")), False)
52+
)
53+
def test_multipage_tiff(monkeypatch: pytest.MonkeyPatch, libtiff: bool) -> None:
54+
monkeypatch.setattr(TiffImagePlugin, "READ_LIBTIFF", libtiff)
5155
with Image.open("Tests/images/multipage.tiff") as im:
5256
for index, frame in enumerate(ImageSequence.Iterator(im)):
5357
frame.load()
5458
assert index == im.tell()
5559
frame.convert("RGB")
5660

5761

58-
def test_tiff() -> None:
59-
_test_multipage_tiff()
60-
61-
62-
@skip_unless_feature("libtiff")
63-
def test_libtiff() -> None:
64-
TiffImagePlugin.READ_LIBTIFF = True
65-
_test_multipage_tiff()
66-
TiffImagePlugin.READ_LIBTIFF = False
67-
68-
6962
def test_consecutive() -> None:
7063
with Image.open("Tests/images/multipage.tiff") as im:
7164
first_frame = None

Tests/test_tiff_ifdrational.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
from fractions import Fraction
44
from pathlib import Path
55

6-
from PIL import Image, TiffImagePlugin, features
6+
import pytest
7+
8+
from PIL import Image, TiffImagePlugin
79
from PIL.TiffImagePlugin import IFDRational
810

9-
from .helper import hopper
11+
from .helper import hopper, skip_unless_feature
1012

1113

1214
def _test_equal(num, denom, target) -> None:
@@ -52,18 +54,18 @@ def test_nonetype() -> None:
5254
assert xres and yres
5355

5456

55-
def test_ifd_rational_save(tmp_path: Path) -> None:
56-
methods = [True]
57-
if features.check("libtiff"):
58-
methods.append(False)
59-
60-
for libtiff in methods:
61-
TiffImagePlugin.WRITE_LIBTIFF = libtiff
57+
@pytest.mark.parametrize(
58+
"libtiff", (pytest.param(True, marks=skip_unless_feature("libtiff")), False)
59+
)
60+
def test_ifd_rational_save(
61+
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, libtiff: bool
62+
) -> None:
63+
im = hopper()
64+
out = str(tmp_path / "temp.tiff")
65+
res = IFDRational(301, 1)
6266

63-
im = hopper()
64-
out = str(tmp_path / "temp.tiff")
65-
res = IFDRational(301, 1)
66-
im.save(out, dpi=(res, res), compression="raw")
67+
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", libtiff)
68+
im.save(out, dpi=(res, res), compression="raw")
6769

68-
with Image.open(out) as reloaded:
69-
assert float(IFDRational(301, 1)) == float(reloaded.tag_v2[282])
70+
with Image.open(out) as reloaded:
71+
assert float(IFDRational(301, 1)) == float(reloaded.tag_v2[282])

0 commit comments

Comments
 (0)