@@ -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,20 +725,19 @@ 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 :
738+ def test_write_icc (self , monkeypatch : pytest . MonkeyPatch , tmp_path : Path ) -> None :
748739 def check_write (libtiff : bool ) -> None :
749- TiffImagePlugin . WRITE_LIBTIFF = libtiff
740+ monkeypatch . setattr ( TiffImagePlugin , " WRITE_LIBTIFF" , libtiff )
750741
751742 with Image .open ("Tests/images/hopper.iccprofile.tif" ) as img :
752743 icc_profile = img .info ["icc_profile" ]
@@ -756,10 +747,9 @@ def check_write(libtiff: bool) -> None:
756747 with Image .open (out ) as reloaded :
757748 assert icc_profile == reloaded .info ["icc_profile" ]
758749
759- libtiffs = []
750+ libtiffs = [False ]
760751 if Image .core .libtiff_support_custom_tags :
761752 libtiffs .append (True )
762- libtiffs .append (False )
763753
764754 for libtiff in libtiffs :
765755 check_write (libtiff )
@@ -840,12 +830,13 @@ def test_sampleformat(self) -> None:
840830
841831 assert_image_equal_tofile (im , "Tests/images/copyleft.png" , mode = "RGB" )
842832
843- def test_sampleformat_write (self , tmp_path : Path ) -> None :
833+ def test_sampleformat_write (
834+ self , monkeypatch : pytest .MonkeyPatch , tmp_path : Path
835+ ) -> None :
844836 im = Image .new ("F" , (1 , 1 ))
845837 out = str (tmp_path / "temp.tif" )
846- TiffImagePlugin . WRITE_LIBTIFF = True
838+ monkeypatch . setattr ( TiffImagePlugin , " WRITE_LIBTIFF" , True )
847839 im .save (out )
848- TiffImagePlugin .WRITE_LIBTIFF = False
849840
850841 with Image .open (out ) as reloaded :
851842 assert reloaded .mode == "F"
@@ -1091,15 +1082,14 @@ def test_sampleformat_not_corrupted(self) -> None:
10911082 with Image .open (out ) as im :
10921083 im .load ()
10931084
1094- def test_realloc_overflow (self ) -> None :
1095- TiffImagePlugin . READ_LIBTIFF = True
1085+ def test_realloc_overflow (self , monkeypatch : pytest . MonkeyPatch ) -> None :
1086+ monkeypatch . setattr ( TiffImagePlugin , " READ_LIBTIFF" , True )
10961087 with Image .open ("Tests/images/tiff_overflow_rows_per_strip.tif" ) as im :
10971088 with pytest .raises (OSError ) as e :
10981089 im .load ()
10991090
11001091 # Assert that the error code is IMAGING_CODEC_MEMORY
11011092 assert str (e .value ) == "-9"
1102- TiffImagePlugin .READ_LIBTIFF = False
11031093
11041094 @pytest .mark .parametrize ("compression" , ("tiff_adobe_deflate" , "jpeg" ))
11051095 def test_save_multistrip (self , compression : str , tmp_path : Path ) -> None :
0 commit comments